1

Having generic pydantic model Localized[T], I want to make shortcut type for Union[T, Localized[T]].

I tried to make shortcut like this:

MaybeLocalized = TypeVar("MaybeLocalized", bound=Union[T, Localized[T]])

but got an error:

>> MaybeLocalized[str]
TypeError: 'TypeVar' object is not subscriptable

nvm, apparently MaybeLocalized = Union[T, Localized[T]] works just fine.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
aiven
  • 3,775
  • 3
  • 27
  • 52
  • Note that a `TypeVar` is not just a shortcut - it is a generic type factory in its own right, and probably not what you want if you already working with generics. If you just want a shortcut, an alias is the way to go (what you already found to work: `MaybeLocalized = Union[T, Localized[T]]`). – MrBean Bremen Jul 04 '21 at 17:13

0 Answers0