Do not consider bare TypeVar not overlapping with None for reachability analysis#18138
Merged
JukkaL merged 6 commits intopython:masterfrom Nov 21, 2024
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Collaborator
Author
|
CI failed with the Windows problem I faced multiple times before, the rest is green. Primer diff is amazing this time! Every diff line seems to be an improvement.
|
This comment has been minimized.
This comment has been minimized.
JukkaL
approved these changes
Nov 15, 2024
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Nice, based on mypy_primer output this fixes multiple bugs, and makes the inferred types more consistent. Just left some minor comments about style (optional), looks good otherwise.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: jinja (https://github.com/pallets/jinja)
+ src/jinja2/lexer.py:761: error: "object" not callable [operator]
+ src/jinja2/lexer.py:781: error: Incompatible types in "yield" (actual type "Tuple[int, object, str]", expected type "Tuple[int, str, str]") [misc]
prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/states.py:229: error: Incompatible types in assignment (expression has type "BaseException", variable has type "ResultRecord[Any]") [assignment]
sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/search/__init__.py:628:18: error: "Node" has no attribute "children" [attr-defined]
+ sphinx/ext/graphviz.py: note: In function "render_dot":
+ sphinx/ext/graphviz.py:307:9: error: Item "StandaloneHTMLBuilder" of "StandaloneHTMLBuilder | LaTeXBuilder | TexinfoBuilder" has no attribute "_graphviz_warned_dot" [union-attr]
+ sphinx/ext/graphviz.py:307:9: error: Item "LaTeXBuilder" of "StandaloneHTMLBuilder | LaTeXBuilder | TexinfoBuilder" has no attribute "_graphviz_warned_dot" [union-attr]
+ sphinx/ext/graphviz.py:307:9: error: Item "TexinfoBuilder" of "StandaloneHTMLBuilder | LaTeXBuilder | TexinfoBuilder" has no attribute "_graphviz_warned_dot" [union-attr]
schema_salad (https://github.com/common-workflow-language/schema_salad)
+ schema_salad/metaschema.py: note: In member "save" of class "EnumSchema":
+ schema_salad/metaschema.py:1882:49: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:1886:49: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py: note: In member "save" of class "SaladEnumSchema":
+ schema_salad/metaschema.py:5378:51: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5381:49: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5385:49: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5389:47: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5392:51: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5395:50: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5398:50: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5404:26: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5411:26: error: Argument "base_url" to "save" has incompatible type "Any | None"; expected "str" [arg-type]
+ schema_salad/metaschema.py:5415:49: error: Argument 2 to "save_relative_uri" has incompatible type "Any | None"; expected "str" [arg-type]
kopf (https://github.com/nolar/kopf)
+ kopf/_kits/webhooks.py:539: error: Item "None" of "Any | None" has no attribute "public_url" [union-attr]
pytest (https://github.com/pytest-dev/pytest)
- src/_pytest/_code/code.py:1071: error: Argument 1 to "from_exception" of "ExceptionInfo" has incompatible type "Optional[BaseException]"; expected "BaseException" [arg-type]
- src/_pytest/_code/code.py:1071: error: Item "None" of "Optional[BaseException]" has no attribute "__traceback__" [union-attr]
- src/_pytest/_code/code.py:1077: error: Argument 1 to "from_exception" of "ExceptionInfo" has incompatible type "Optional[BaseException]"; expected "BaseException" [arg-type]
- src/_pytest/_code/code.py:1077: error: Item "None" of "Optional[BaseException]" has no attribute "__traceback__" [union-attr]
paroxython (https://github.com/laowantong/paroxython)
+ paroxython/flatten_ast.py:401: error: "AST" has no attribute "lineno" [attr-defined]
pandera (https://github.com/pandera-dev/pandera)
- pandera/api/dataframe/model.py:72: error: Unused "type: ignore" comment [unused-ignore]
- pandera/api/dataframe/model.py:77: error: Unused "type: ignore" comment [unused-ignore]
cki-lib (https://gitlab.com/cki-project/cki-lib)
- cki_lib/s3bucket.py:57: error: Item "None" of "Any | None" has no attribute "get_credentials" [union-attr]
PyGithub (https://github.com/PyGithub/PyGithub)
- github/GithubObject.py:315: error: Unused "type: ignore" comment [unused-ignore]
discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/core.py:590: error: Unused "type: ignore" comment [unused-ignore]
- discord/ext/commands/core.py:645: error: Item "object" of the upper bound "Cog | None" of type variable "CogT" has no attribute "cog_command_error" [union-attr]
- discord/ext/commands/core.py:884: error: Item "object" of the upper bound "Cog | None" of type variable "CogT" has no attribute "cog_before_invoke" [union-attr]
- discord/ext/commands/core.py:904: error: Item "object" of the upper bound "Cog | None" of type variable "CogT" has no attribute "cog_after_invoke" [union-attr]
- discord/ext/commands/core.py:1148: error: "type[CogT]" has no attribute "__cog_name__" [attr-defined]
- discord/ext/commands/core.py:1277: error: Item "object" of the upper bound "Cog | None" of type variable "CogT" has no attribute "cog_check" [union-attr]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/app.py:130: error: Argument "game_extra_info" to "CMsgClientGamesPlayedGamePlayed" has incompatible type "NameT"; expected "str" [arg-type]
- steam/ext/commands/commands.py:923: error: Incompatible return value type (got "Command[Any, Any, Any] | MaybeCommandT", expected "MaybeBool | MaybeCommandT") [return-value]
ibis (https://github.com/ibis-project/ibis)
+ ibis/util.py:137: error: Incompatible return value type (got "tuple[()]", expected "tuple[V]") [return-value]
- ibis/selectors.py:708: error: Argument 1 to "ColumnIndex" has incompatible type "int | slice[Any, Any, Any] | Iterable[int | str]"; expected "str | int | Slice | tuple[int | str, ...]" [arg-type]
+ ibis/selectors.py:708: error: Argument 1 to "ColumnIndex" has incompatible type "str | int | slice[Any, Any, Any] | Iterable[int | str]"; expected "str | int | Slice | tuple[int | str, ...]" [arg-type]
- ibis/expr/types/relations.py:2629: error: Argument 3 to "DropNull" has incompatible type "Sequence[str] | None"; expected "tuple[Value[Any, Columnar], ...] | None" [arg-type]
+ ibis/expr/types/relations.py:2629: error: Argument 3 to "DropNull" has incompatible type "Sequence[str] | str | None"; expected "tuple[Value[Any, Columnar], ...] | None" [arg-type]
+ ibis/backends/sqlite/__init__.py:497: error: Item "None" of "Any | Any | Any | Any | Any | None" has no attribute "schema" [union-attr]
- ibis/backends/duckdb/__init__.py:532: error: Argument 1 to "read_parquet" of "Backend" has incompatible type "str | Any | Path | list[Any] | tuple[Any, ...]"; expected "str | Iterable[str]" [arg-type]
+ ibis/backends/duckdb/__init__.py:532: error: Argument 1 to "read_parquet" of "Backend" has incompatible type "str | Path | Any"; expected "str | Iterable[str]" [arg-type]
- ibis/backends/duckdb/__init__.py:536: error: Argument 1 to "read_csv" of "Backend" has incompatible type "str | Any | Path | list[Any] | tuple[Any, ...]"; expected "str | list[str] | tuple[str]" [arg-type]
+ ibis/backends/duckdb/__init__.py:536: error: Argument 1 to "read_csv" of "Backend" has incompatible type "str | Path | Any"; expected "str | list[str] | tuple[str]" [arg-type]
- ibis/backends/duckdb/__init__.py:538: error: Argument 1 to "read_postgres" of "Backend" has incompatible type "str | Any | Path | list[Any] | tuple[Any, ...]"; expected "str" [arg-type]
+ ibis/backends/duckdb/__init__.py:538: error: Argument 1 to "read_postgres" of "Backend" has incompatible type "str | Path | Any"; expected "str" [arg-type]
- ibis/backends/pyspark/__init__.py:962: error: Argument 1 to "read_parquet" of "Backend" has incompatible type "str | Any | Path | list[Any] | tuple[Any, ...]"; expected "str | Path" [arg-type]
- ibis/backends/pyspark/__init__.py:966: error: Argument 1 to "read_csv" of "Backend" has incompatible type "str | Any | Path | list[Any] | tuple[Any, ...]"; expected "str | list[str] | tuple[str]" [arg-type]
+ ibis/backends/pyspark/__init__.py:966: error: Argument 1 to "read_csv" of "Backend" has incompatible type "str | Path | Any"; expected "str | list[str] | tuple[str]" [arg-type]
|
JukkaL
approved these changes
Nov 21, 2024
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Thanks for the updates, LGTM!
This was referenced Dec 8, 2024
JelleZijlstra
pushed a commit
that referenced
this pull request
Dec 9, 2024
|
Very nice! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #18126.
Simply allowing such intersection was insufficient: existing binder logic widened the type to
T | Noneafter theis Nonecheck.This PR extends the binder logic to prevent constructing a union type when all conditional branches are reachable and contain no assignments: checking
if isinstance(something, Something)does not change the type ofsomethingafter the end of theifblock.