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

Is there a way to parametrizetyping.Annotateds metadata?#2023

Unanswered
MilanStaffehl asked this question inQ&A
Discussion options

I am trying to make a type that behaves asint on type checking, but which has additional metadata on runtime (specifically for the purpose of specifying a subtype ofint).typing.Annotated seems the obvious choice to make this work. However, I would like to create a type (alias) which allows setting the metadata of an annotatedint as a parameter.

A naive first approach might look something like this:

fromtypingimportTypeVar,Annotated,TypeAlias,Literal_RuntimeType_co=TypeVar("_RuntimeType_co",covariant=True,bound=int,default=int)IntMeta:TypeAlias=Annotated[int,_RuntimeType_co]custom_int:IntMeta[Literal[2]]=2

Of course this does not work, since type checkers will seeIntMeta as onlyint, andint does not take any type parameters.

So, my question: is this possible in any way - whether usingAnnotated, or a custom type, or any other way?

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Linkingpython/mypy#18925 for related discussion

You must be logged in to vote
1 reply
@MilanStaffehl
Comment options

Ah, thanks! Looks like the type alias approach falls flat then. I kind of expected this, but a little disappointing nonetheless.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@MilanStaffehl@brianschubert

[8]ページ先頭

©2009-2025 Movatter.jp