Skip to content

Mypy can't deal with exclusive options implied by a Union #7265

Closed
@ariciputi

Description

@ariciputi

Hi,
I have a use case in which (AFAICT) mypy is not able to deal with the exclusive options given by a Union.

  • Are you reporting a bug, or opening a feature request?

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

Here it is a minimal code snippets:

from typing import Collection, Union

def function(a: Union[bool, Collection[str]], b: str) -> int:
    if a is True:
        return 1
    if a is False:
        return 2

    if b in a:
        return 3

    return 4
  • What is the actual behavior/output?

example.py:10: error: Unsupported right operand type for in ("Union[bool, Collection[str]]")

  • What is the behavior/output you expect?

This is a perfectly valid Python code, so mypy shouldn't complain.

  • What are the versions of mypy and Python you are using?
$ mypy --version
mypy 0.720

Do you see the same issue after installing mypy from Git master?

Haven't tried

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

None

Hope you can help.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions