-
Is it possible to create a Special: TypeAlias = Foo | Bar Subclasses of I believe I can create a T = TypeVar("T", Foo, Bar) but my concern is that I'd be using a type variable where a "normal" annotation works best. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Probably not, could you talk more about your use case? Note value constrained TypeVars may not work the way you expect them to: https://mypy-play.net/?mypy=latest&python=3.12&gist=6768354f1bab66850422a86eb5723a1f (the TypeVar is prevented from solving to |
Beta Was this translation helpful? Give feedback.
There's no way to specify an "exact type" in any typing construct. From a type theory standpoint, any subclass of
Foo
is also aFoo
.One potential solution is to mark
Foo
as@final
which would prevent any subclasses from being defined.