-
-
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
Adds attribute type inference from super-types for partial types #10871
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ok, here's the problematic part: from typing import List
class P:
x: List[int]
class M:
x: List[str]
class C(P, M):
x = []
reveal_type(C.x) # N: Revealed type is "builtins.list[builtins.int]" This code does not raise I have tried to defer this node to the second pass, but we don't have the Now, I will try to do something similar in The last option is: if we have more than one different type definition in mro, just fallback to old behavior. |
Solved it using new |
This comment has been minimized.
This comment has been minimized.
Oh, this one is hard 😕 Solution with It is also related to #3208 One more failing case: from typing import List
class P:
@property
def x(self) -> List[int]:
...
class C(P):
x = []
C.x.append(1) # error: "Callable[[P], List[int]]" has no attribute "append" It works with explicit |
To be fair, current from typing import List
class Parent:
a: List[str]
class A(Parent):
a = []
a.append(1)
reveal_type(A.a) # note: Revealed type is "builtins.list[builtins.int]" It should give the same result as: from typing import List
class Parent:
a: List[str]
class A(Parent):
a: List[int] # error: Incompatible types in assignment (expression has type "List[int]", base class "Parent" defined the type as "List[str]")
reveal_type(A.a) # note: Revealed type is "builtins.list[builtins.int]" But, I think it is a different task. |
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy.git)
- sympy/core/numbers.py:2514: error: Need type annotation for "free_symbols" (hint: "free_symbols: Set[<type>] = ...")
|
This PR implements the suggested general case from #10870 (comment)
What we do? When we are stuck with the partial type inside a class definition, we try to fetch types from parent definitions.
If this definition exist, we reuse the type.
This way, we won't have a warning here:
Closes #10870
Test Plan
I am going to add more tests after someone from the core team approves this approach 🙂