-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Incorrect type inference in Derived class that was explicitly typed in Base #12268
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
Comments
I'm trying to file a PR for this but am struggling to navigate the codebase. My understanding of the problem is that although Basically, on line 2256 of checker.py, the inferred value is generated. Even after checking the type with the supertype on line 2280, the inferred type is still used. However, I don't have access to the base type in this class or anything. Maybe I should be looking elsewhere? |
Fixes python#12268. If the type of an lvalue is not given at definition time, attempt to infer it from the type of a base member.
Fixes python#12268. If the type of an lvalue is not given at definition time, attempt to infer it from the type of a base member.
Closed PR because this seems like an anti-feature. I think the real issue we want to solve has something more subtle to do with variance. I'll file in a separate PR. |
Would help with python#12268. Modified a test because the two behaviours conflict right now.
For what it's worth, pyright implements the behavior you're describing here. I don't consider it an anti-feature :). It seems logical to me that a type declaration should always take precedence, and type inference should be used only in cases where a declaration is missing. Mypy applies this rule for local variables and for instance/class variables within a class, but it doesn't do it for inherited instance/class variables. Is this behavior intended, or is it a bug / oversight? I'd be curious to hear from core mypy developers. class Base:
# Base class contains type declaration for "var1"
var1: float | None
class Derived(Base):
# Child class contains assignment but no declaration
var1 = 0
reveal_type(Derived().var1) # Pyright: float | None, Mypy: int |
I don't know. Have you looked at the open source snippets I mentioned? I want the code there to still work. |
Sorry, said snippet is in the old closed PR. |
I re-opened because I think that this could be fixed by #12284 |
This appears to have been fixed in mypy 0.9.0. I think it can be closed. |
I think this might be best explained with an example.
Here is the output of
mypy test.py
:The error is on the last line. The complaint is that
variable1
is of typeList[A]
, and so it cannot be assigned tovariable2
. At first, this seems reasonable, but notice that the type ofvariable1
is explicitly defined to beList[Union[A, B]]
in the superclass. More interesting still is that the previous line, line 11, is accepted just fine.I believe Derived types should inherit their superclass types if possible before resorting to a type inference.
Debug information:
And also a real world example of this.
The text was updated successfully, but these errors were encountered: