-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
False positive for Union[Literal[False], ClassName] #8264
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
Yeah, this looks like a bug. cc @Michael0x2a |
From: https://docs.python.org/3/library/typing.html#typing.cast You may cast from pathlib import Path
from typing_extensions import Literal
from typing import Union, cast
a: Union[Literal[False], Path] = Path()
if a and cast(Path, a).exists():
print('the file exists') mypy output:
|
It looks like this was fixed by #8368, which landed a few days ago. |
I have mypy installed from the master branch, yet these errors seem to still be present:
|
@dosisod -- I don't seem to be able to repro. Running the following example against the same mypy checkout you're using results in no errors and a revealed type of from pathlib import Path
from typing_extensions import Literal
from typing import Union
a: Union[Literal[False], Path] = Path()
if a and a.exists():
reveal_type(a)
print('the file exists') Maybe try clearing your mypy cache? |
This fixed my problem, thanks. |
Are you reporting a bug, or opening a feature request?
Bug
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Item "bool" of "Union[Literal[False], Path]" has no attribute "exists"
No reported error
Do you see the same issue after installing mypy from Git master?
mypy 0.760 - did not try installing from git master
--ignore-missing-imports
Hey there!
As you can see I found some unexpected behavior when trying to use the union of the literal "False" together with a class. It's a bit of a tricky one, as some variable can only be False or an instance of the class. I get that it's not a very common occurrence, since most people would just make it an optional, but nonetheless it's not the expected output from my point of view.
Thanks!
The text was updated successfully, but these errors were encountered: