Skip to content

Literal broadened to superclass erroneously #12215

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
adamjstewart opened this issue Feb 19, 2022 · 3 comments
Closed

Literal broadened to superclass erroneously #12215

adamjstewart opened this issue Feb 19, 2022 · 3 comments
Labels
bug mypy got something wrong topic-literal-types

Comments

@adamjstewart
Copy link

Bug Report

In some situations, the type checker will broaden a Literal to its superclass, causing type errors.

To Reproduce

Create the following test.py file:

from typing import *

def foo() -> Iterable[Union[Tuple[Literal["foo"], int], Tuple[Literal["bar"], str]]]:
    for i in range(10):
        if i % 2 == 0:
            yield "foo", i
        else:
            yield "bar", str(i)

Expected Behavior

I would expect these type hints to be correct.

Actual Behavior

$ mypy test.py 
test.py:7: error: Incompatible types in "yield" (actual type "Tuple[str, int]", expected type "Union[Tuple[Literal['foo'], int], Tuple[Literal['bar'], str]]")
test.py:9: error: Incompatible types in "yield" (actual type "Tuple[str, str]", expected type "Union[Tuple[Literal['foo'], int], Tuple[Literal['bar'], str]]")
Found 2 errors in 1 file (checked 1 source file)

Here, "actual type" should not be Tuple[str, int] or Tuple[str, str], it should be Tuple[Literal['foo'], int] or Tuple[Literal['bar'], str].

Your Environment

  • Mypy version used: 0.920
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9.9
  • Operating system and version: macOS 10.15.7
@adamjstewart adamjstewart added the bug mypy got something wrong label Feb 19, 2022
@anentropic
Copy link

Another example of similar behaviour:

from typing import Any, Literal, TypeVar


T = TypeVar('T', bound=Any)


def original() -> Literal["a", "b", "c"] | None:
    return "a"


def get_some(val: T | None) -> T:
    assert val is not None
    return val


val = get_some(original())
reveal_type(val)

Gives:

demo2.py:17: note: Revealed type is "builtins.str*"

...the "literal-ness" of T got lost in the process of narrowing the type from Optional[T] to T.

@erictraut
Copy link

This appears to have been fixed in mypy 0.93. I think it can be closed.

@hauntsaninja
Copy link
Collaborator

Fixed in #11236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-literal-types
Projects
None yet
Development

No branches or pull requests

5 participants