-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Use native ExpandTypeVisitor for expanding type aliases as well #15209
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.
|
Looking at mypy-primer:
In conclusion, the PR does a better job of inferring types around |
|
For context, the more precise types we currently have for Could we add a test case for functions where a parameter is annotated with an alias to a generic TypeGuard? |
|
Yeah, it looks like a bug (or maybe not) in For now I will just add a test. |
|
Diff from mypy_primer, showing the effect of this PR on open source code: vision (https://github.com/pytorch/vision) got 1.25x slower (42.3s -> 52.9s)
+ torchvision/models/_api.py:125: error: Item "None" of "str | None" has no attribute "endswith" [union-attr]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/state.py:2696: error: Need type annotation for "func" [var-annotated]
- steam/state.py:2696: error: Argument 2 to "getmembers" has incompatible type "Callable[[Any], Any | bool]"; expected "Callable[[Any], TypeGuard[_T]]" [arg-type]
+ steam/state.py:2696: error: Argument 2 to "getmembers" has incompatible type "Callable[[Any], Any | bool]"; expected "Callable[[Any], TypeGuard[Any | bool]]" [arg-type]
+ steam/state.py:2697: error: Argument 1 to "get_annotations" has incompatible type "Any | bool"; expected "Callable[..., object] | type[Any] | Module" [arg-type]
+ steam/state.py:2699: error: Incompatible types in assignment (expression has type "Any | bool", target has type "ParserCallback[ConnectionState, ProtobufMessage | Message | GCProtobufMessage | GCMessage]") [assignment]
+ steam/state.py:2701: error: Incompatible types in assignment (expression has type "Any | bool", target has type "ParserCallback[ConnectionState, ProtobufMessage | Message | GCProtobufMessage | GCMessage]") [assignment]
- steam/_gc/state.py:82: error: Need type annotation for "func" [var-annotated]
- steam/_gc/state.py:82: error: Argument 2 to "getmembers" has incompatible type "Callable[[Any], bool]"; expected "Callable[[Any], TypeGuard[_T]]" [arg-type]
+ steam/_gc/state.py:82: error: Argument 2 to "getmembers" has incompatible type "Callable[[Any], bool]"; expected "Callable[[Any], TypeGuard[bool]]" [arg-type]
+ steam/_gc/state.py:83: error: Argument 1 to "get_annotations" has incompatible type "bool"; expected "Callable[..., object] | type[Any] | Module" [arg-type]
+ steam/_gc/state.py:87: error: Incompatible types in assignment (expression has type "bool", target has type "ParserCallback[GCState[Any], GCProtobufMessage | GCMessage]") [assignment]
spark (https://github.com/apache/spark)
+ python/pyspark/pandas/supported_api_gen.py:160: error: Argument 2 to "_organize_by_implementation_status" has incompatible type "dict[str, FunctionType]"; expected "dict[str, Callable[..., Any]]" [arg-type]
+ python/pyspark/pandas/supported_api_gen.py:160: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ python/pyspark/pandas/supported_api_gen.py:160: note: Consider using "Mapping" instead, which is covariant in the value type
+ python/pyspark/pandas/supported_api_gen.py:160: error: Argument 3 to "_organize_by_implementation_status" has incompatible type "dict[str, FunctionType]"; expected "dict[str, Callable[..., Any]]" [arg-type]
materialize (https://github.com/MaterializeInc/materialize) got 1.11x faster (80.3s -> 72.5s)
|
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.
Thank you! And thanks Jelle and Alex for looking through the primer diff
This used to be a source of code duplication, and also using the "native" visitor will allow us to implement new features for type aliases easily (e.g. variadic type aliases, that I am going to do in next PR).