Skip to content
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

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

Closed
ariciputi opened this issue Jul 26, 2019 · 3 comments
Closed

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

ariciputi opened this issue Jul 26, 2019 · 3 comments

Comments

@ariciputi
Copy link

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.

@ariciputi ariciputi changed the title Mypy can't deal with exclusice options implied by a Union Mypy can't deal with exclusive options implied by a Union Jul 26, 2019
@ariciputi ariciputi reopened this Jul 26, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Jul 26, 2019

Thanks for the report! It might be a reasonable for mypy to infer a narrower type after testing a boolean. Say, after if bool_value: return (or if bool_value is True: return) we might infer Literal[False] as the type of bool_value. This seems closely related to #6366 but a slightly different use case.

@Michael0x2a What do you think about this?

@ariciputi
Copy link
Author

Interestingly enough the following works with latest version of mypy:

from typing import Collection, Union
from enum import Enum

Sentinel = Enum("Sentinel", ("TRUE", "FALSE"))

def function(a: Union[Sentinel, Collection[str]], b: str) -> int:
    if a is Sentinel.TRUE:
        return 1
    if a is Sentinel.FALSE:
        return 2

    if b in a:
        return 3

    return 4

but is failing with the very same error message as above with version 0.710

HTH

@ilevkivskyi
Copy link
Member

I think this is a duplicate of #6113 (treating bool as equivalent to Union[Literal[True], Literal[False]]). I am going to raise priority there and update the title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants