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

Type inference: Mapping from TypedDict?#2007

Answeredbyerictraut
Pringauss asked this question inQ&A
Discussion options

Trying to learn how to write statically typed Python, but got confused for quite some time. Here is a toy example:

fromtypingimportTypedDictfromcollections.abcimportMapping# represent the loose idea of a point as a mapping (i.e. dict or variants) from coordinates to floatstypePointLike=Mapping[str,float]# a specific type of pointclassPoint2D(TypedDict):x:floaty:float# other types of points may be defined here or elsewhere (thanks to duck typing)# examples: 3D space, RGB...# the point (pun intended) is to implement transformations between different point types# those should be abstracted with a Protocol later on# here is a trivial transformationdefidentity_transform[T:PointLike](p:T)->T:# return the point unchangedreturnp# let's test itmy_point=Point2D(x=0.7,y=-0.3)identity_transform(my_point)# runs fine, but type checker error!

Bothmypy andpyright will error on the last line, as can be seen in the playground links provided.

My main question is: why is this? After thinking about it for way too long, my guess would be that, since Python dictionaries are mutable, there is nothing stopping you from adding another key of a different type tomy_point. Therefore, type checkers cannot infer its type asMapping[str, float], and are inferring theTypedDict instance to be likeMapping[str, Any]. Please correct me if I am wrong.

A second question is: what would, then, be the appropriate way to implement the idea in the problem while keeping static type checkers happy?

You must be logged in to vote

To understand why yourTypedDict isn't assignable toMapping[str, float], check outthis section of the typing spec. (Scroll down to the last bullet point in that section.)

There is a draftPEP 728 that proposes to add the concept of "closed TypedDicts" to the type system. Pyright has experimental support for this PEP if you want to play around with it. Here's what that looks like in thepyright playground.

Replies: 1 comment 1 reply

Comment options

To understand why yourTypedDict isn't assignable toMapping[str, float], check outthis section of the typing spec. (Scroll down to the last bullet point in that section.)

There is a draftPEP 728 that proposes to add the concept of "closed TypedDicts" to the type system. Pyright has experimental support for this PEP if you want to play around with it. Here's what that looks like in thepyright playground.

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

This is it, thanks!

Answer selected byPringauss
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
@Pringauss@erictraut

[8]ページ先頭

©2009-2025 Movatter.jp