Bug Report
Starting with mypy 0.900 tagged unions don't work anymore if they are nested.
To Reproduce
from typing import Literal, Union
from enum import Enum
class ModelType(str, Enum):
Simple1 = "simple1"
Simple2 = "simple2"
Complex1 = "complex1"
Complex2 = "complex2"
class BaseModel:
type: ModelType
class BaseSimpleModel(BaseModel):
type: Literal[ModelType.Simple1, ModelType.Simple2]
simple: str
class Simple1Model(BaseSimpleModel):
type: Literal[ModelType.Simple1]
class Simple2Model(BaseSimpleModel):
type: Literal[ModelType.Simple2]
SimpleModel = Union[Simple1Model, Simple2Model]
class BaseComplexModel(BaseModel):
type: Literal[ModelType.Complex1, ModelType.Complex2]
complex: str
class Complex1Model(BaseComplexModel):
type: Literal[ModelType.Complex1]
class Complex2Model(BaseComplexModel):
type: Literal[ModelType.Complex2]
ComplexModel = Union[Complex1Model, Complex2Model]
Model = Union[SimpleModel, ComplexModel]
model: Model = Simple1Model()
if model.type == ModelType.Simple1 or model.type == ModelType.Simple2:
reveal_type(model)
reveal_type(model.simple)
else:
reveal_type(model)
reveal_type(model.complex)
Type-check with mypy >= 0.900 and it won't pass, try with mypy 0.812 for example, and it will pass.
I've also prepared it on mypy-play.
Working (mypy 0.812):
https://mypy-play.net/?mypy=0.812&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead
Broken (mypy 0.910):
https://mypy-play.net/?mypy=0.910&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead
Expected Behavior
main.py:43: note: Revealed type is 'Union[main.Simple1Model, main.Simple2Model]'
main.py:44: note: Revealed type is 'builtins.str'
main.py:46: note: Revealed type is 'Union[main.Complex1Model, main.Complex2Model]'
main.py:47: note: Revealed type is 'builtins.str'
Actual Behavior
main.py:43: note: Revealed type is "Union[main.Simple1Model, main.Simple2Model, main.Complex1Model, main.Complex2Model]"
main.py:44: error: Item "Complex1Model" of "Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]" has no attribute "simple"
main.py:44: error: Item "Complex2Model" of "Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]" has no attribute "simple"
main.py:44: note: Revealed type is "Union[builtins.str, Any]"
main.py:46: note: Revealed type is "Union[main.Simple1Model, main.Simple2Model, main.Complex1Model, main.Complex2Model]"
main.py:47: error: Item "Simple1Model" of "Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]" has no attribute "complex"
main.py:47: error: Item "Simple2Model" of "Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]" has no attribute "complex"
main.py:47: note: Revealed type is "Union[Any, builtins.str]"
As you can see mypy isn't able to correctly nail down the union members anymore when unions are nested.
Your Environment
- Mypy version used: 0.912
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini (and other config files): None
- Python version used: 3.9
- Operating system and version: macOS
Bug Report
Starting with mypy 0.900 tagged unions don't work anymore if they are nested.
To Reproduce
Type-check with mypy >= 0.900 and it won't pass, try with mypy 0.812 for example, and it will pass.
I've also prepared it on mypy-play.
Working (mypy 0.812):
https://mypy-play.net/?mypy=0.812&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead
Broken (mypy 0.910):
https://mypy-play.net/?mypy=0.910&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead
Expected Behavior
Actual Behavior
As you can see mypy isn't able to correctly nail down the union members anymore when unions are nested.
Your Environment
mypy.ini(and other config files): None