-
-
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
join constraints with Any when constraints are in same shape #6487
Conversation
this do not fix this, so I think a good way is to treat |
I think this is not a good solution TBH. It just masks the problem instead of solving it. For example, IIUC in the original example in the issue you propose to solve The option of making I am leaning towards the tricky option @JukkaL mentions. Essentially we would need to avoid pushing unions on the type context stack (when type-checking r.h.s. of an assignment, and when type-checking arguments in a call). Instead we would need to push every item of a union, type-check the expression in this context, and then union the results. We already do something similar for type-checking overload calls with union arguments. This latter approach looks potentially promising from the point of view of quality of inference results, however it might be tricky. Also performance implications are not clear. |
Yes you'r right, this does not actually solve the problem. And I have also test if just treat every I will see what can be done to infer |
Note that making |
@JukkaL Argument of Pyre people (who actually consider it a subtype) is that if we prohibit this type to appear as a type of variable (btw mypy partially prohibits this), then the unsafety is practically negligible. On one hand this sounds reasonable, but on other hand, I don't like this kind of special-casing. Plus it will break protocol checks. |
Personally, I think |
@oraluben Do you understand how mypy generally handles empty lists? There are two special cases. Case 1: Type inferred from context:
Mypy does some constraint solving here, where the expected return type of the call to A simpler case is:
Again, the type of You can even check this by writing Case 2: Type inferred from peeking at later assignment:
Here the type of Otherwise: If neither of these cases holds, the type of
Here You get the same error (though for a different reason) if you try to split the example from case 1 in two:
Here the type of |
Fix #6463, #2164 by join Union with same shape with
Any
, for example,Union[List[int], List[str]]
->List[Any]
, I useAny
becauseList[Union[str, int]]
is obviously wrong.according to this,
another way to fix that issue is to adjust
any_constraints
andsolve_constraints
to solve Union on any position of a type, currently it seems to work only on the 'leaf node'.I'm not sure if this is a proper way to fix that, please have a look at your convenience.