Skip to content

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

Closed
beeb opened this issue Jan 9, 2020 · 6 comments
Closed

False positive for Union[Literal[False], ClassName] #8264

beeb opened this issue Jan 9, 2020 · 6 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-literal-types

Comments

@beeb
Copy link

beeb commented Jan 9, 2020

  • 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.

from pathlib import Path
from typing_extensions import Literal  # support Python 3.7
a: Union[Literal[False], Path] = Path()
# mypy wrongly reports 'Item "bool" of "Union[Literal[False], Path]" has no attribute "exists"' but here we check that a is not False
if a and a.exists(): 
    print('the file exists')
  • What is the actual behavior/output?

Item "bool" of "Union[Literal[False], Path]" has no attribute "exists"

  • What is the behavior/output you expect?

No reported error

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.760 - did not try installing from git master

  • What are the mypy flags you are using? (For example --strict-optional)

--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!

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-literal-types labels Jan 13, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 13, 2020

Yeah, this looks like a bug.

cc @Michael0x2a

@dosisod
Copy link
Contributor

dosisod commented Jan 14, 2020

From: https://docs.python.org/3/library/typing.html#typing.cast

You may cast a to the desired type:

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:

Success: no issues found in 1 source file

@Michael0x2a
Copy link
Collaborator

It looks like this was fixed by #8368, which landed a few days ago.

@dosisod
Copy link
Contributor

dosisod commented Feb 10, 2020

I have mypy installed from the master branch, yet these errors seem to still be present:

$ mypy --pretty test.py
test.py:7: error: Item "bool" of "Union[Literal[False], Path]" has no attribute "exists"
    if a and a.exists():
             ^
Found 1 error in 1 file (checked 1 source file)

$ mypy --version
mypy 0.770+dev.d6c2c01fd04043bff55222f1f43a9fe2e15c0ccf

@Michael0x2a
Copy link
Collaborator

@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 pathlib.Path for me:

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?

@dosisod
Copy link
Contributor

dosisod commented Feb 11, 2020

This fixed my problem, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-literal-types
Projects
None yet
Development

No branches or pull requests

4 participants