-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Optional generic not accepted with --strict-optional
#2678
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
Hm, I wonder if the problem is due to a bad interaction between the ThingClass TypeVar and the Optional from the context? I think it ought to infer that if the context is Optional[ChildThing] and the return type (to be matched to that context) is Optional[ThingClass], then it must solve this to ThingClass==ChildThing, but it is then not correctly concluding that the argument type must be Type[ChildThing]. |
This problem isn't Optional-specific -- it'll happen with any Union. For example: from typing import *
class A: pass
class B(A): pass
T = TypeVar("T", bound=A)
def f(x: Type[T]) -> Union[T, str]:
return "foo"
x = None # type: Union[B, str]
reveal_type(x)
reveal_type(f(B))
x = f(B)
We've known for a while that TypeVars and Unions don't play well together. There's been a bit of spot-patching so far, but I think we're at the point where a larger careful rethinking makes sense. |
Fixes #4872 Fixes #3876 Fixes #2678 Fixes #5199 Fixes #5493 (It also fixes a bunch of similar issues previously closed as duplicates, except one, see below). This PR fixes a problems when mypy commits to soon to using outer context for type inference. This is done by: * Postponing inference to inner (argument) context in situations where type inferred from outer (return) context doesn't satisfy bounds or constraints. * Adding a special case for situation where optional return is inferred against optional context. In such situation, unwrapping the optional is a better idea in 99% of cases. (Note: this doesn't affect type safety, only gives empirically more reasonable inferred types.) In general, instead of adding a special case, it would be better to use inner and outer context at the same time, but this a big change (see comment in code), and using the simple special case fixes majority of issues. Among reported issues, only #5311 will stay unfixed.
Fixes python#4872 Fixes python#3876 Fixes python#2678 Fixes python#5199 Fixes python#5493 (It also fixes a bunch of similar issues previously closed as duplicates, except one, see below). This PR fixes a problems when mypy commits to soon to using outer context for type inference. This is done by: * Postponing inference to inner (argument) context in situations where type inferred from outer (return) context doesn't satisfy bounds or constraints. * Adding a special case for situation where optional return is inferred against optional context. In such situation, unwrapping the optional is a better idea in 99% of cases. (Note: this doesn't affect type safety, only gives empirically more reasonable inferred types.) In general, instead of adding a special case, it would be better to use inner and outer context at the same time, but this a big change (see comment in code), and using the simple special case fixes majority of issues. Among reported issues, only python#5311 will stay unfixed.
When running with
--strict-optional
, mypy complains about the last statement here. However, this seems like it should work, both because it does work when--strict-optional
is left out, and because the revealed type looks like it should match.The text was updated successfully, but these errors were encountered: