Skip to content

[Python] Upgrade Pytype (Part - 1)#39816

Closed
asheshvidyut wants to merge 27 commits intogrpc:masterfrom
asheshvidyut:upgrade-pytype-part-01
Closed

[Python] Upgrade Pytype (Part - 1)#39816
asheshvidyut wants to merge 27 commits intogrpc:masterfrom
asheshvidyut:upgrade-pytype-part-01

Conversation

@asheshvidyut
Copy link
Copy Markdown
Member

@asheshvidyut asheshvidyut commented Jun 10, 2025

Description

  • Add a new script to run an upgraded version of PyType
  • Keeping the old script / version running as is.
  • This is Part 1 PR of upgrading PyType in which only one folder is added as input. Final inputs in the second PR will be similar to -

    grpc/setup.cfg

    Lines 21 to 28 in 8342a10

    [pytype]
    inputs =
    src/python/grpcio/grpc/experimental
    src/python/grpcio/grpc
    src/python/grpcio_tests/tests_aio
    src/python/grpcio_observability/grpc_observability
    examples/python/auth
    examples/python/helloworld
  • Follow up PR will have other folders added.

Testing

  • Script runs in CI. Hence testing included in CI.

@asheshvidyut asheshvidyut force-pushed the upgrade-pytype-part-01 branch from cf70568 to 5e28b3e Compare June 11, 2025 02:24
src/python/grpcio/grpc/beta
src/python/grpcio/grpc/__init__.py
src/python/grpcio/grpc/_simple_stubs.py
output = ~/.cache/pytype
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity tests run in parallel. Hence this is required.

- script: tools/distrib/check_pytype.sh
- script: tools/distrib/check_pytype_updated.sh

@asheshvidyut asheshvidyut marked this pull request as ready for review June 11, 2025 15:55
source $VIRTUALENV/bin/activate

python3 -m pip install pytype==2024.10.11
python3 -m pytype --keep-going -j "$JOBS" --strict-import --config "setup_updated.cfg"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
python3 -m pytype --keep-going -j "$JOBS" --strict-import --config "setup_updated.cfg"
pytype --keep-going -j auto --strict-import --config "setup_updated.cfg"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upd: -j auto eliminates the need for nproc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also - we should just set it in the config

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think strict-import is strict_import in the config too, and keep-going is keep_going


JOBS=$(nproc) || JOBS=4

mkdir -p ~/.cache/pytype
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to create it on its own, do we need this?

$ python3 -m pytype --keep-going -j "$JOBS" --strict-import --config "setup_updated.cfg"
Computing dependencies
Analyzing 18 sources with 13 local dependencies
ninja: Entering directory `../../.cache/pytype'
...

$ realpath ../../.cache/pytype
/Users/sergiitk/.cache/pytype

# NOTE(lidiz) Adding examples one by one due to pytype aggressive error:
# ninja: error: build.ninja:178: multiple rules generate helloworld_pb2.pyi [-w dupbuild=err]
# TODO(xuanwn): include all files in src/python/grpcio/grpc
[pytype]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like pytype supports toml! https://github.com/google/pytype/tree/main?tab=readme-ov-file#config-file

I've converted this with (minor changes aside)

$ uvx 'ini2toml[lite]' setup_updated.cfg -o setup_updated.toml
Profile not explicitly set, using 'best_effort'.

Result:

[tool.pytype]
inputs = [
    "src/python/grpcio/grpc",
]
exclude = [
    "**/*_pb2.py",
    "src/python/grpcio/grpc/framework",
    "src/python/grpcio/grpc/aio",
    "src/python/grpcio/grpc/beta",
    "src/python/grpcio/grpc/__init__.py",
    "src/python/grpcio/grpc/_simple_stubs.py",
]
output = "~/.cache/pytype"
disable = [
    "import-error",
    "module-attr",
    "attribute-error",
]

We can place it to black.toml (which we should probably rename at some point). Works just fine!

$ pytype --config=black.toml --keep-going -j "$JOBS" --strict-import
Computing dependencies
Analyzing 18 sources with 13 local dependencies
ninja: Entering directory `../../.cache/pytype'
[48/48] check grpc.experimental.aio.__init__
Leaving directory '../../.cache/pytype'
Success: no errors found

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have converted setup_updated.cfg to pytype.toml. I would prefer not merging it with black.toml as both a different tools and have seperate configuration, might give better readability and maintainability

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reason not to use pytype.toml is because it's what picked automatically - I'd like to avoid that for period we're migrating to the updated tooling.

A reason to use black.toml is that it's already contains settings for other tools (like isort). It's a normal pattern for all tolling setting to be in one pyproject.toml file - but we can't do it right now without breaking setup.py.

But I see why black.toml may be very confusing. I mentioned that " (which we should probably rename at some point)."

Should we rename it now then? Like grpc-style-config.toml?

def create_server_call_tracer_factory_option(xds: bool) -> ChannelArgumentType:
def create_server_call_tracer_factory_option(
xds: bool,
) -> ServerCallTracerFactoryOption:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just Tuple[ChannelArgumentType, ...]?

I think the main problem was that we declared return as ChannelArgumentType (which is tuple[str, Any]), but ended up returning a tuple containing ChannelArgumentType.

And , ... covers an empty tuple, thought it may imply there may be multiple options.
Alternatively we could do Union[Tuple[ChannelArgumentType], Tuple[()]] - if we want to be VERY clear that it's either a single-element tuple with an option or an empty one.

I'm fine either way.

Copy link
Copy Markdown
Member Author

@asheshvidyut asheshvidyut Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

asheshvidyut and others added 6 commits June 18, 2025 09:45
Co-authored-by: Sergii Tkachenko <sergiitk@google.com>
Co-authored-by: Sergii Tkachenko <sergiitk@google.com>
Co-authored-by: Sergii Tkachenko <sergiitk@google.com>
Co-authored-by: Sergii Tkachenko <sergiitk@google.com>
@asheshvidyut asheshvidyut requested a review from sergiitk June 18, 2025 15:00
Comment on lines +16 to +17
JOBS=$(nproc) || JOBS=4

Copy link
Copy Markdown
Member

@sergiitk sergiitk Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JOBS=$(nproc) || JOBS=4

can be removed now

source $VIRTUALENV/bin/activate

pip install pytype==2024.10.11
pytype -j auto --config=pytype.toml
Copy link
Copy Markdown
Member

@sergiitk sergiitk Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move auto to the config, but provide --output=~/.cache/pytype as an argument.

jobs = 'auto'

Reasons:

  1. We do want to autodetect cpu number even when run pytype locally
  2. No reason to write grpc-only output to ~/.cache/pytype when running pytype locally

@asheshvidyut asheshvidyut requested a review from sergiitk June 19, 2025 00:24
@sreenithi
Copy link
Copy Markdown
Contributor

Just a note for reference, I was initially considering if this new script needs to get added to sanitize.sh as well. But given that this script is similar to check_pytype.sh, which doesn't exist in sanitize.sh currently, this file is probably not needed to be added there too.

@sergiitk
Copy link
Copy Markdown
Member

Good catch, @sreenithi. Since we have this test passing now, I think we should include it into sanitize.sh. Let's discuss it tomorrow.

anniefrchz pushed a commit to anniefrchz/grpc that referenced this pull request Jun 25, 2025
### Description
* Add a new script to run an upgraded version of PyType
* Keeping the old script / version running as is.
* This is Part 1 PR of upgrading PyType in which only one folder is added as input. Final inputs in the second PR will be similar to - https://github.com/grpc/grpc/blob/8342a109aece77f04147b0a82a4e09ba25466e03/setup.cfg#L21-L28
* Follow up PR will have other folders added.

### Testing
* Script runs in CI. Hence testing included in CI.

Closes grpc#39816

COPYBARA_INTEGRATE_REVIEW=grpc#39816 from asheshvidyut:upgrade-pytype-part-01 68955ae
PiperOrigin-RevId: 773260007
@asheshvidyut asheshvidyut added this to the Python Type Hints Support milestone Aug 7, 2025
paulosjca pushed a commit to paulosjca/grpc that referenced this pull request Aug 23, 2025
### Description
* Add a new script to run an upgraded version of PyType
* Keeping the old script / version running as is.
* This is Part 1 PR of upgrading PyType in which only one folder is added as input. Final inputs in the second PR will be similar to - https://github.com/grpc/grpc/blob/8342a109aece77f04147b0a82a4e09ba25466e03/setup.cfg#L21-L28
* Follow up PR will have other folders added.

### Testing
* Script runs in CI. Hence testing included in CI.

Closes grpc#39816

COPYBARA_INTEGRATE_REVIEW=grpc#39816 from asheshvidyut:upgrade-pytype-part-01 68955ae
PiperOrigin-RevId: 773260007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants