-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Nested tagged unions stopped working #10764
Comments
Thanks for following up! |
Not obvious and not fully equivalent code, but if you change your check to: (which is similar behaviour to #6366 (comment) , see other comments on that issue as well) |
@hauntsaninja Thanks a lot for pointing me to this workaround. But I've noticed another issue demonstrated here: 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 is ModelType.Simple1:
reveal_type(model) Mypy play link: https://mypy-play.net/?mypy=0.910&python=3.9&gist=3fea5f8e3bfa94c7b9835fb53133af4d Expected output:
Actual output:
It seems to nail down the type just one level to the right nested union but not further down. I'm not fully sure if this is connected to the particular error I was reporting here as this also happens in mypy 0.812. |
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): NoneThe text was updated successfully, but these errors were encountered: