Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently store settable property type #18774

Merged

Conversation

ilevkivskyi
Copy link
Member

Fixes #18764

There are two things important to understand this PR:

  • First, mypy has a performance optimization - we store decorator/overload type during semantic analysis in certain "trivial" situations (to avoid deferrals). Otherwise, we infer the type of decorated function or an overload variant during type checking.
  • Second, for settable properties we store getter type in two places, as a Var.type of getter (as a decorator), and also in overall OverloadedFuncDef.type. The latter is ugly, but unfortunately it is hard to get rid of, since some code in multiple plugins rely on this.

It turns out there are three inconsistencies in how these two things interact (first one causes the actual crash):

  • For trivial settable properties (i.e. without extra decorators) when we store the type in semanal.py we only store it the second way (i.e. as OverloadedFuncDef.type).
  • For non-trivial settable properties (where getter and/or setter are themselves decorated), we only set the inferred type the first way (as Var.type).
  • When inferring setter type (unlike getter, that is handled correctly) we actually ignore any extra decorators (this is probably quire niche, but still inconsistent).

Essentially I simply remove these inconsistencies.

This comment has been minimized.

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thank you for fixing! Confirmed that this fixes the original crash 🙏

@hauntsaninja
Copy link
Collaborator

Nice, looks like a lot of the aiohttp primer is from the check_untyped_after_decorator change

@ilevkivskyi
Copy link
Member Author

Change in prefect looks like a new false negative (I know how to fix this, but it also makes me think that we currently implicitly rely on setter being second, but it also may be third, after the deleter).

The couple new errors in xarray look like a new false positive. But TBH I don't feel like chasing this one, as the same error would appear if they had --local-partial-types enabled.

@ilevkivskyi
Copy link
Member Author

OK, I actually checked the xarray thing, it turns out to be some weird bug in --show-error-context causing errors to be shown twice. A similar problem exists on master. So to avoid scope creep, I am not trying to fix it (and also the setter vs deleter ordering, I just add a TODO).

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/abc.py:84: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_routedef.py:57: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_routedef.py:82: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/helpers.py:148: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/helpers.py:177: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/helpers.py:248: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/helpers.py:873: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/helpers.py:890: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/client_reqrep.py:325: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/client.py:1250: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/client.py:1287: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_server.py:43: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_server.py:52: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_app.py:167: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_app.py:182: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_app.py:216: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/web_runner.py:257: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/worker.py:203: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/test_utils.py:160: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/test_utils.py:270: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/test_utils.py:278: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/pytest_plugin.py:45: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/pytest_plugin.py:53: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/pytest_plugin.py:402: error: Unused "type: ignore" comment  [unused-ignore]
+ aiohttp/pytest_plugin.py:409: error: Unused "type: ignore" comment  [unused-ignore]

xarray (https://github.com/pydata/xarray)
+ xarray/core/variable.py: note: In function "encoding":
+ xarray/core/variable.py:857: error: Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "None")  [assignment]
+ xarray/core/variable.py:858: error: Incompatible return value type (got "None", expected "dict[Any, Any]")  [return-value]

trio (https://github.com/python-trio/trio)
+ src/trio/_file_io.py:431: error: Unused "type: ignore[misc]" comment  [unused-ignore]
+ src/trio/_path.py:186: error: Unused "type: ignore[misc]" comment  [unused-ignore]

@ilevkivskyi ilevkivskyi merged commit af5186e into python:master Mar 10, 2025
18 checks passed
@ilevkivskyi ilevkivskyi deleted the consistent-settable-property-type branch March 10, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash on numtype numpy stubs package related to property
2 participants