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

dataclass should use inspect.get_annotations instead of examining cls.__dict__ #97799

Closed
Assignees
larryhastings
Labels
type-featureA feature request or enhancement
@larryhastings

Description

@larryhastings

dataclass needs to examine the annotations of class objects it decorates. Due to__annotations__ being badly designed, it currently pulls the__annotations__ out of the class dict (cls.__dict__.get('__annotations__')). While this works today, this wouldn't work properly for classes defiend in modules usingfrom __future__ import co_annotations if PEP 649 is accepted. It's also not recommended best practices for working with annotations. (Which, admittedly,I wrote.)

Best practices call for usinginspect.get_annotations to get the annotations on any object. This does exactly whatdataclass wants; it doesn't inherit base class annotations if no child class defines annotations, and it always returns a dict. (Empty, if appropriate.)

dataclass has in the past resisted usinginspect.get_annotations because it didn't want to incur the runtime cost of importinginspect. However, as of this time in CPython trunk,inspect is always imported during startup, anddataclass is already importing it anyway. It's timedataclass changed to best practices here.

Metadata

Metadata

Assignees

Labels

type-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp