-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Use empty context as fallback for return statements #19767
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice trick! One q
mypy/checker.py
Outdated
# If there are errors with the original type context, try re-inferring in empty context. | ||
original_messages = msg.filtered_errors() | ||
original_type_map = type_map | ||
with self.msg.filter_errors(filter_errors=True, save_filtered_errors=True) as msg: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should [deprecated]
errors be handled here? They can be context-dependent in case of some scary overload
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably just filter_deprecated=True
in both branches to avoid false positives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think we need to filter deprecated as well.
Diff from mypy_primer, showing the effect of this PR on open source code: spark (https://github.com/apache/spark)
+ python/pyspark/core/rdd.py:2210: error: Unused "type: ignore" comment [unused-ignore]
prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/flows.py:1980: error: Argument "on_completion" to "Flow" has incompatible type "Optional[list[FlowStateHook[[VarArg(Any), KwArg(Any)], Any]]]"; expected "Optional[list[FlowStateHook[P, R]]]" [arg-type]
- src/prefect/flows.py:1981: error: Argument "on_failure" to "Flow" has incompatible type "Optional[list[FlowStateHook[[VarArg(Any), KwArg(Any)], Any]]]"; expected "Optional[list[FlowStateHook[P, R]]]" [arg-type]
- src/prefect/flows.py:1982: error: Argument "on_cancellation" to "Flow" has incompatible type "Optional[list[FlowStateHook[[VarArg(Any), KwArg(Any)], Any]]]"; expected "Optional[list[FlowStateHook[P, R]]]" [arg-type]
- src/prefect/flows.py:1983: error: Argument "on_crashed" to "Flow" has incompatible type "Optional[list[FlowStateHook[[VarArg(Any), KwArg(Any)], Any]]]"; expected "Optional[list[FlowStateHook[P, R]]]" [arg-type]
- src/prefect/flows.py:1984: error: Argument "on_running" to "Flow" has incompatible type "Optional[list[FlowStateHook[[VarArg(Any), KwArg(Any)], Any]]]"; expected "Optional[list[FlowStateHook[P, R]]]" [arg-type]
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/context/menu.py:200: error: Argument "default" to "resolve_to_member" of "MenuContext" has incompatible type "None"; expected "User" [arg-type]
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/wrapper/_implementations.py:1756: error: Unsupported operand types for + ("list[None]" and "list[str]") [operator]
jax (https://github.com/google/jax)
+ jax/_src/sharding_impls.py:189: error: Unused "type: ignore" comment [unused-ignore]
ibis (https://github.com/ibis-project/ibis)
- ibis/backends/sql/__init__.py:515: error: Item "Iterable[str]" of "str | Iterable[str]" has no attribute "format" [union-attr]
|
Fixes #19716. It is a follow-up to #19767 and was missed there due to malformed test stubs (the same testcase fails when run by full mypy against typeshed, I did not notice missing AwaitExpr because of the passing test...). I synced typevar variance with typeshed definitions and added AwaitExpr to the list of context-dependent exprs. Cc @ilevkivskyi
Fixes #16924
Fixes #15886
Mypy uses external type context first, this can cause bad type inference in return statements (see example in test case added), usually we recommend a workaround to users like replacing:
with
But this is a bit ugly, and more importantly we can essentially automatically try this workaround. This is what this PR adds. I checked performance impact, and don't see any (but for some reason noise level on my desktop is much higher now).