3 ms·
mypy typechecks that just fine[1]. However, I think that's possibly a bug :-) -- I agree that narrowing a literal via subclassing is unsound. That's why the ex
by woodruffw 2y ago
mypy typechecks that just fine[1].
However, I think that's possibly a bug :-) -- I agree that narrowing a literal via subclassing is unsound. That's why the example in the blog used `str` for the superclass, not the closure of all `Literal` variants.
(I use this pattern pretty extensively in Python codebases that are typechecked with mypy, and I haven't run into many issues with mypy failing to understand the variant shapes -- the exception to this so far has been with `RootModel`, where mypy has needed Pydantic's mypy plugin[2] to understand the relationship between the "root" type and its underlying union. But it's possible that this is essentially unsound as well.)
[1]: https://mypy-play.net/?mypy=latest&python=3.12&gist=f35da62ec4ac59f06360a758ca5f58f6 https://mypy-play.net/?mypy=latest&python=3.12&gist=f35da62e...
[2]: https://docs.pydantic.dev/latest/integrations/mypy/ https://docs.pydantic.dev/latest/integrations/mypy/
- reubenmorais 2y agoUsing str in the superclass equally unsound and also doesn't type check. There's no good way to do it, as the discriminator type is by definition disjoint between all kinds.