We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
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:
test.py
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].
Tuple[str, int]
Tuple[str, str]
Tuple[Literal['foo'], int]
Tuple[Literal['bar'], str]
Your Environment
mypy.ini
The text was updated successfully, but these errors were encountered:
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.
T
Optional[T]
Sorry, something went wrong.
This appears to have been fixed in mypy 0.93. I think it can be closed.
Fixed in #11236
No branches or pull requests
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:Expected Behavior
I would expect these type hints to be correct.
Actual Behavior
Here, "actual type" should not be
Tuple[str, int]
orTuple[str, str]
, it should beTuple[Literal['foo'], int]
orTuple[Literal['bar'], str]
.Your Environment
mypy.ini
(and other config files): N/AThe text was updated successfully, but these errors were encountered: