-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
After Optional[str] == 'literal', infer that it's not optional #1825
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
We should probably fix this more generally with Unions: def f(a: Union[int, str]) -> str
if a == '<string>': return a # mypy should understand that a must be a string here
else: return str(a) Are we okay with saying that equality must imply some sort of type equivalence? (In particular, that |
I was more thinking that the join of the two types should not be object. We should carefully introduce and test such a change because I expect it'll cause some noise in various large projects (most of which I expect to be questionable code in those projects, but some might be due to our rules needing some exception, and either wait it'll take a while to sort it all out). FWIW the same rule should apply to |
Asuming that on The only operator where I think this could safely be done is "is" and "is not", but that doesn't cover the example. |
In general that assumption is unsafe, but here |
Example:
With
--strict-optional
, this produces an error:It would be nice if mypy understood that a is a string if this == returns True.
The text was updated successfully, but these errors were encountered: