Skip to content

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

Closed
@beeb

Description

@beeb
  • 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!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions