Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PEP 695TypeAliasType.__value__ not supporting recursion intentional?#1816

Unanswered
randolf-scholz asked this question inQ&A
Discussion options

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 thatScalar.__value__ is the Union of twoTypeAliasType members. PEP 695 only states that"__value__ is the evaluated value of the type alias", which under certain interpretations could be considered to mean that the result shouldn't contain anyTypeAliasType. The PEP also mentions__evaluate_value__ in the context of PEP649, i.e. the resolution of Forward References.

What's the recommended way of getting the value of theTypeAliasType recursively? I guess for the moment, I could expand the unions manually. Will this be covered by__evaluate_value__ in the future?

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

I would recommend against using thetype statement when you need to use the "type alias" in a runtime context.

I am not planning to change any of this behavior in the future (but that decision is not solely up to me).

You must be logged in to vote
4 replies
@ntjess
Comment options

@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). AGeneric example:

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 theevaluatedvalue of the type alias statement in the PEP... Given TypeAlias is officiallydeprecated in favor oftype statements, what is the recommended alternative if__value__ support is not planned?

@JelleZijlstra
Comment options

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).

@ntjess
Comment options

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 totyping, my hope was that__value__ would supply runtime-specific generic values, as opposed to user-created custom traversals. The question was mostly about how to correctly interpret the PEP 695 spec if runtime type alias valuesaren't resolved within__value__

@JelleZijlstra
Comment options

Tools interpreting such metadata should recognize GenericAlias instances wrapping TypeAliasType instances and resolve the TypeVar accordingly.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@randolf-scholz@JelleZijlstra@ntjess

[8]ページ先頭

©2009-2025 Movatter.jp