[Python] Upgrade Pytype (Part - 1)#39816
Conversation
cf70568 to
5e28b3e
Compare
setup_updated.cfg
Outdated
| src/python/grpcio/grpc/beta | ||
| src/python/grpcio/grpc/__init__.py | ||
| src/python/grpcio/grpc/_simple_stubs.py | ||
| output = ~/.cache/pytype |
There was a problem hiding this comment.
Sanity tests run in parallel. Hence this is required.
grpc/tools/run_tests/sanity/sanity_tests.yaml
Lines 30 to 31 in 62facbe
| 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" |
There was a problem hiding this comment.
same here
| python3 -m pytype --keep-going -j "$JOBS" --strict-import --config "setup_updated.cfg" | |
| pytype --keep-going -j auto --strict-import --config "setup_updated.cfg" |
There was a problem hiding this comment.
upd: -j auto eliminates the need for nproc
There was a problem hiding this comment.
also - we should just set it in the config
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
setup_updated.cfg
Outdated
| # 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] |
There was a problem hiding this comment.
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 foundThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This (Tuple[ChannelArgumentType, ...]) is invalid for python3.7 and currently our CI has check_pytype.sh which runs on 3.7.
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>
| JOBS=$(nproc) || JOBS=4 | ||
|
|
There was a problem hiding this comment.
JOBS=$(nproc) || JOBS=4
can be removed now
| source $VIRTUALENV/bin/activate | ||
|
|
||
| pip install pytype==2024.10.11 | ||
| pytype -j auto --config=pytype.toml |
There was a problem hiding this comment.
Let's move auto to the config, but provide --output=~/.cache/pytype as an argument.
jobs = 'auto'Reasons:
- We do want to autodetect cpu number even when run pytype locally
- No reason to write grpc-only output to ~/.cache/pytype when running pytype locally
|
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 |
|
Good catch, @sreenithi. Since we have this test passing now, I think we should include it into |
### 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
### 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
Description
grpc/setup.cfg
Lines 21 to 28 in 8342a10
Testing