-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Fix type inference for index expression with bounded TypeVar #11434
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -631,3 +631,74 @@ def g(s: S) -> Callable[[S], None]: ... | |
| def f(x: S) -> None: | ||
| h = g(x) | ||
| h(x) | ||
|
|
||
| [case testTypeVarWithTypedDictBoundInIndexExpression] | ||
| from typing import TypeVar | ||
| from typing_extensions import TypedDict | ||
|
|
||
| class Data(TypedDict): | ||
| x: int | ||
|
|
||
|
|
||
| T = TypeVar("T", bound=Data) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the same work for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it works because |
||
|
|
||
|
|
||
| def f(data: T) -> None: | ||
| reveal_type(data["x"]) # N: Revealed type is "builtins.int" | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
||
| [case testTypeVarWithUnionTypedDictBoundInIndexExpression] | ||
| from typing import TypeVar, Union, Dict | ||
| from typing_extensions import TypedDict | ||
|
|
||
| class Data(TypedDict): | ||
| x: int | ||
|
|
||
|
|
||
| T = TypeVar("T", bound=Union[Data, Dict[str, str]]) | ||
|
|
||
|
|
||
| def f(data: T) -> None: | ||
| reveal_type(data["x"]) # N: Revealed type is "Union[builtins.int, builtins.str*]" | ||
|
|
||
| [builtins fixtures/tuple.pyi] | ||
| [builtins fixtures/dict.pyi] | ||
|
|
||
| [case testTypeVarWithTypedDictValueInIndexExpression] | ||
| from typing import TypeVar, Union, Dict | ||
| from typing_extensions import TypedDict | ||
|
|
||
| class Data(TypedDict): | ||
| x: int | ||
|
|
||
|
|
||
| T = TypeVar("T", Data, Dict[str, str]) | ||
|
|
||
|
|
||
| def f(data: T) -> None: | ||
| _: Union[str, int] = data["x"] | ||
| [builtins fixtures/tuple.pyi] | ||
| [builtins fixtures/dict.pyi] | ||
|
|
||
| [case testSelfTypeVarIndexExpr] | ||
| from typing import TypeVar, Union, Type | ||
| from typing_extensions import TypedDict | ||
|
|
||
| T = TypeVar("T", bound="Indexable") | ||
|
|
||
| class Indexable: | ||
| def __init__(self, index: str) -> None: | ||
| self.index = index | ||
|
|
||
| def __getitem__(self: T, index: str) -> T: | ||
| return self._new_instance(index) | ||
|
|
||
| @classmethod | ||
| def _new_instance(cls: Type[T], index: str) -> T: | ||
| return cls("foo") | ||
|
|
||
| def m(self: T) -> T: | ||
| return self["foo"] | ||
|
|
||
| [builtins fixtures/tuple.pyi] | ||
| [builtins fixtures/classmethod.pyi] | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
What if this type var has
.valuesinstead of.upper_bound? Will it still work?Uh oh!
There was an error while loading. Please reload this page.
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.
surprisingly it works. I'm not sure why, but I suspect it's related to
TypeVars expansion inchecker.py::check_func_def. Added test for this case as well