-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Special-case type inference of empty collections #16122
Merged
Merged
Conversation
This file contains 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
Diff from mypy_primer, showing the effect of this PR on open source code: mypy (https://github.com/python/mypy)
+ mypy/test/testpep561.py:134: error: Unused "type: ignore" comment [unused-ignore]
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/dependencies/reloaders.py:513: error: Incompatible return value type (got "tuple[Path, Union[tuple[None, set[Never]], tuple[str, set[Never]]]]", expected "tuple[Path, Union[tuple[str, set[str]], tuple[None, set[Path]]]]") [return-value]
graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/type/definition.py:1010: error: Unused "type: ignore" comment [unused-ignore]
+ tests/type/test_definition.py:735: error: Unused "type: ignore" comment [unused-ignore]
+ tests/type/test_definition.py:886: error: Unused "type: ignore" comment [unused-ignore]
koda-validate (https://github.com/keithasaurus/koda-validate)
+ koda_validate/union.py:171: error: Unused "type: ignore" comment [unused-ignore]
discord.py (https://github.com/Rapptz/discord.py)
- discord/state.py:258: error: Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "dict[str, Callable[[Any], None]]") [assignment]
- discord/message.py:1841: error: Incompatible types in assignment (expression has type "list[Never]", variable has type "list[User | Member]") [assignment]
- discord/message.py:1841: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
- discord/message.py:1841: note: Consider using "Sequence" instead, which is covariant
- discord/app_commands/tree.py:648: error: Incompatible return value type (got "list[Never]", expected "list[ContextMenu] | list[Command[Any, [VarArg(Any), KwArg(Any)], Any] | Group] | list[Command[Any, [VarArg(Any), KwArg(Any)], Any] | Group | ContextMenu]") [return-value]
|
mypy_primer looks good (as expected). |
hauntsaninja
approved these changes
Sep 16, 2023
Noticed a small regression in Home Assistant. It seems to be related to the custom dict type in combination with from typing import TypeVar, Any
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
class CustomDictType(dict[_KT, _VT]):
...
class A:
options: CustomDictType[str, CustomDictType[str, int]]
def ok(a: A) -> None:
entry: dict[str, Any]
entry = a.options.get("entry", {})
if entry:
reveal_type(entry)
val = entry.get("name")
def bad(a: A) -> None:
entry: dict[str, Any]
if (
entry := a.options.get("entry", {})
):
reveal_type(entry)
val = entry.get("name")
|
Yeah, I guess walrus doesn't correctly "erases" inferred types, there is much simpler repro: y: List[int]
if (y := []):
reveal_type(y) # builtins.list[Never]??? |
hauntsaninja
pushed a commit
that referenced
this pull request
Sep 28, 2023
This fixes a regression caused by #16122
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 #230
Fixes #6463
I bet it fixes some other duplicates, I closed couple yesterday, but likely there are more.
This may look a bit ad-hoc, but after some thinking this now starts to make sense to me for two reasons:
Similar issues keep coming, so I think it is a good idea to add this special-casing (especially taking into account how simple it is, and that it closer some "popular" issues).