-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Infer union types from multiple assigments #18568
Comments
The generics use case shown above seems important for NumPy, in particular. We could first try inferring rvalue with empty type context, and if it doesn't produce a useful type, fall back to using the current variable type as type context. So this would work correctly: x = [1]
reveal_type(x) # list[int]
x = ["a"]
reveal_type(x) # list[str] We'd still use the type context here, unless we add some special support for partial types: x = [1]
reveal_type(x) # list[int]
x = [] # Fall back to type context for item type
reveal_type(x) # list[int] |
I implemented the idea about using empty type context first and it seems good so far. My prototype supports a good subset of expected use cases, and I'm currently getting only 4 new errors in self check -- because of more precise inferred types. I still need to figure out how to support loops properly and deferrals, but overall I'm pretty optimistic about the approach. |
Just curious, did you ask them about this, or you found this in their code? I doubt they use regular lists, or do you mean they have a similar problem with Btw note that we already have some special-casing w.r.t. to empty context for optional types, see here https://github.com/python/mypy/blob/master/mypy/checker.py#L4471-L4494 |
The same issue is with any call that returns a generic type with a type variable, including |
These long-standing issues can be solved at least partially by making type inference more flexible:
This would be an alternative to the current
--allow-redefinition
flag which uses renaming to allow somewhat flexible redefinitions. This would also be an alternative to the more general renaming pass proposed in #18516.The idea is simply to allow each assignment to refine the type of a variable in the scope where it's being inferred. Example:
I've done some prototyping and the implementation seems fairly simple, but there could be some edge cases that are tricky. The prototype also seems to be reasonably compatible with existing behavior, though there will likely be some difference. I'm not sure yet if we'd have to wait for 2.0 to enable this by default.
This has some benefits over a renaming based solution (#18516):
It has also some drawbacks:
This would only be enabled for simple variables (
x = ...
), not for attributes (self.x = ...
), at least initially.It's not clear if we can support general redefinitions which involve generic types. Initially code like this would still generate errors:
The renaming based approach would support the above example without issues. However, the approach proposed here could be used together with
--allow-redefinition
to support the above use case (with limitations, since--allow-redefinition
isn't very general).Thanks to @ilevkivskyi for #18538 which will make this approach feasible, and for suggesting that we don't need to support redefinitions involving partial types initially.
The text was updated successfully, but these errors were encountered: