I’m adding type statement support to our tool. We use type information for automatic type conversion, and at the moment the type statement is not supported. My plan is to just unwrap the created type aliases so that the type conversion logic sees the underlying types, and possibly later preserve the original name for documentation purposes.
I got unwrapping working like this:
while instance(hint, TypeAliasType):
try:
hint = hint.__value__
except Exception as err:
raise DataError(f"Resolving type alias '{hint}' failed: {err}")
I also tested that it can handle cases like these as expected:
type ForwardRef = Simple
type Simple = int
type Params = list[int]
type Union = int | float
type Invalid = bad
All seems to work fine, but I want to make sure I’ve covered everything. I’ve read the docs related to the type statement to see have I missed something, and got confused by quoting used in this example from PEP 695:
# A type alias that includes a forward reference
type AnimalOrVegetable = Animal | "Vegetable"
I know quoting can be used to create forward references like def f(a: "SomeType"), but based on these examples quoting wouldn’t be needed in this context:
>>> type A = B
>>> type B = int
>>> A.__value__
B
>>> A.__value__.__value__
<class 'int'>
If quoting is used, values don’t seem to be resolved:
>>> type A = "B"
>>> type B = int
>>> A.__value__
'B'
My question is, is the example in the PEP invalid or is quoting like that actually allowed? If it is allowed, how can I resolve the value?