- Notifications
You must be signed in to change notification settings - Fork263
PEP 695TypeAliasType.__value__
not supporting recursion intentional?#1816
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I was moving some code to PEP695 and just realized an issue. Consider the following example: fromtypingimportTypeAliasStringScalar:TypeAlias=str|bytesNumericalScalar:TypeAlias=bool|int|float|complexScalar:TypeAlias=StringScalar|NumericalScalarassertisinstance("foo",StringScalar)# ✅assertisinstance(3.14,NumericalScalar)# ✅assertisinstance(42,Scalar)# ✅ and the supposed PEP 695 analogue fails: typeStringScalar=str|bytestypeNumericalScalar=bool|int|float|complextypeScalar=StringScalar|NumericalScalarassertisinstance("foo",StringScalar.__value__)# ✅assertisinstance(3.14,NumericalScalar.__value__)# ✅assertisinstance(42,Scalar.__value__)# ❌ arg 2 must be a type, a tuple of types, or a union The issue is that What's the recommended way of getting the value of the |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 4 replies
-
I would recommend against using the I am not planning to change any of this behavior in the future (but that decision is not solely up to me). |
BetaWas this translation helpful?Give feedback.
All reactions
😕 1
-
@JelleZijlstra I found this issue based on the same assumption, that PEP 695 indicates a runtime-valid RHS of type parameters (even if not recursively expanded). A typeGenericMetadata[T]=Annotated[T,list[T]]typeHardCodedMetadata=Annotated[int,list[int]]print(HardCodedMetadata.__value__)# Annotated[int, list[int]]print(GenericMetadata[int].__value__)# Annotated[T, list[T]] (not int??) It seems this behavior violates the |
BetaWas this translation helpful?Give feedback.
All reactions
-
I don't know what to recommend to you because I don't know what you are trying to achieve. In general, I aim for the standard library to provide simple building blocks, and user code that knows what it wants can implement more complex operations (such as walking over a type and substituting type variables). |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thanks for the reply. My use case is to annotate IDs with information about what object they reference: typeIdOf[T]=Annotated[UUID,T] This can be hooked by e.g. JSON schema generators in pydantic models to provide more schema information/typing to endpoints accepting UUID strings. As for how it relates to |
BetaWas this translation helpful?Give feedback.
All reactions
-
Tools interpreting such metadata should recognize GenericAlias instances wrapping TypeAliasType instances and resolve the TypeVar accordingly. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1