-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Infer type from "assert isinstance()" even if types aren't overlapping #1542
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 would argue that this should be an error; if a function expects its |
Yeah, the simple example I gave doesn't make sense. Here is another example:
|
You might want to do some |
@JukkaL, according to my experiments, in the case above, mypy currently does not check the body of the |
Yes, that is used for type checking functions that use However, if there are no type variables with values in the current scope, it's not the right thing to do. |
Ah, I see. I wonder if type checking the branch with the string having an intersection type would work, but I bet there would be some cases where type inference wouldn't do the right thing... |
This is essentially a duplicate of #3603 that has some ideas how to fix this. |
I think that we should infer
x
to bestr
in this example (this is based on a user issue):Here the rationale is that if the
isinstance
checks passes,x
will be a string, not an integer.Currently this doesn't work since mypy considers
int
andstr
to be non-overlapping. This is perhaps okay forif isinstance()
(though perhaps only if variable has a type variable type with values such asAnyStr
), but forassert isinstance()
it doesn't make sense.The text was updated successfully, but these errors were encountered: