-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Now TypeVar
with values correctly handles is_subtype
check
#11378
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.
Diff from mypy_primer, showing the effect of this PR on open source code: pip (https://github.com/pypa/pip.git)
+ src/pip/_internal/req/req_uninstall.py:186: error: unused "type: ignore" comment
|
It was successfully tested by the OP: python/typing#909 (reply in thread)
|
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.
Unfortunately, I don't think that this approach works.
else: | ||
return False | ||
if isinstance(right, TypeVarType) and right.values: | ||
return any(self._is_subtype(left, r_val) for r_val in right.values) |
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.
This is not the correct fix. Now this invalid code generates no error:
from typing import AnyStr, Generic
class Foo(Generic[AnyStr]):
def method1(self, s: AnyStr, t: AnyStr) -> None:
print(s)
class Bar(Foo[AnyStr]):
def method1(self, s: AnyStr, t: AnyStr) -> None:
print('Child before')
super().method1('x', b'y') # Should be an error
print('Child after')
We type check the method by substituting all instances of AnyStr
in the method first with str
, and later with bytes
. Currently we don't perform the substitution in SuperExpr
, which seems to be the root cause of the problem.
SuperExpr
should perhaps be modified to support type variable substitution somehow. For example, we could add a list of (type variable, substitution) tuples as an attribute of SuperExpr
that are initially empty, but as substitutions are performed, we'd add an item to the list. When inferring the type of SuperExpr
, we'd perform these substitutions. This way the type of super()
could effectively be treated as Foo[str]
or Foo[bytes]
instead of Foo[AnyStr]
, which should make the original error go away.
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.
Ok, I see 😞
I will refactor this one to expand SuperExpr()
👍
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.
One more idea: we can represent method1
as an @overload
of two functions:
def method1(self, s: str, t: str) -> None:
def method1(self, s: bytes, t: bytes) -> None:
So, this will help us to represent correct argument combinations.
What do you think?
Context: python/typing#909