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

typing a collection with type hints for sub type#2031

AnsweredbyDaverball
goolmoos asked this question inQ&A
Discussion options

I am writing a certain data structure. if I just added into a collection, I want to be able to type safely use the subtype of the data.
is this possible? what are the correct annotations?

example (obviously not currently working, but demonstrates the idea):
a linked list with every node storingdata: T. after adding a node withdata: S, S: T, I want to access the data asS

from __future__importannotationsfromtypingimportFinal,Generic,TypeVarT=TypeVar("T")U=TypeVar("U",bound=T,covariant=True)S=TypeVar("S",bound=T)# link in a list with data types T, this one is a U: TclassLinkNode(Generic[T,U]):data:Final[U]next:LinkNode[T,T]|None=Nonedef__init__(self,data:U)->None:self.data=data# goal: callers know node.add_next(s).data is Sdefadd_next(self,data:S)->LinkNode[T,S]:n:LinkNode[T,S]=LinkNode(data)self.next=nreturnn
You must be logged in to vote

See:#1226

This has been requested many times, but since this feature is a type of higher kinded types (HKT) this is non-trivial and has many side-effects that need to be considered carefully in the design of such a feature. So far nobody has stepped up to do the hard work of writing up a specification/PEP that resolves those issues.

There are many languages with no support for HKTs at all, due to the inherent risk of introducing really bad soundness holes and the difficulty of reasoning about those HKTs.

Replies: 1 comment

Comment options

See:#1226

This has been requested many times, but since this feature is a type of higher kinded types (HKT) this is non-trivial and has many side-effects that need to be considered carefully in the design of such a feature. So far nobody has stepped up to do the hard work of writing up a specification/PEP that resolves those issues.

There are many languages with no support for HKTs at all, due to the inherent risk of introducing really bad soundness holes and the difficulty of reasoning about those HKTs.

You must be logged in to vote
0 replies
Answer selected bygoolmoos
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
@goolmoos@Daverball

[8]ページ先頭

©2009-2025 Movatter.jp