-
Notifications
You must be signed in to change notification settings - Fork 258
Guarded return value #56
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
@JukkaL can you help with this? I thought there was something in mypy that would let you do this, but I can't seem to get it to work. Isn't there a feature where code guarded by isinstance() is type-checked with the more constrained type? E.g.
|
BTW the PEP doesn't specify (or recommend to the implementers of type checkers) that And it doesn't say anything about the subtyping relationship between |
The closing of python/mypy#245 made me think that this was supported in mypy. Maybe it's not understanding the Union correctly? I wrote up the subtyping relationship between Unions and their constituents in PEP 483, which is supposed to be included by reference in PEP 484. |
@gvanrossum According to PEP 483: """Union[t1, t2, ...] . Classes that are subclass of at least one of t1 etc. are subclasses of this.""" So |
That's correct. I'm guessing mypy's Union implementation has some bugs. |
@vlasovskikh this is correct. But we have reverse situation here: |
@seriyps Yes, but this is a run-time guard, which mypy is supposed to handle. The |
@seriyps I misread the example, sorry. |
@gvanrossum ah, nice! So, the correct example should look like if result is None:
handle_none()
elif isinstance(result, str):
handle_result(result) This looks reasonably. But if analyzer can calculate last May also suggest def main() -> str:
result = None # type: Union[str, None]
while result is None:
result = pool_worker(key) ## -> Union[str, None]
assert isinstance(result, str)
return result |
Type checking Since
Type checking the
There is also an issue for python/mypy#473, python/mypy#476 and python/mypy#477 are other related issues. Type checking |
And the original code is an example of a specific mypy bug, so I'm keeping this open -- |
Ah, I forgot that this is not the mypy issue tracker :-) I added a new mypy issue: python/mypy#572 |
I think that this issue can be closed -- this was about a mypy bug and current limitations of mypy. |
I just presented on this topic for PyCon Belarus, and again at WarGaming, located here in Minsk. One question raised (and emailed to me) at the second presentation was this example. What would be the best approach to coding this to pass the type checker (and do we need the PEP to address a similar example)? This pattern of guarding a return value seems commonplace, and avoiding superfluous warnings is desirable.
% cat sergey_example.py
% mypy sergey_example.py
sergey_example.py, line 17: Argument 1 to "handle_result" has incompatible type "Union[str, None]"; expected "str"
The text was updated successfully, but these errors were encountered: