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

Deprecate unusual ways of creatingtyping.NamedTuple classes #105566

Closed
Assignees
AlexWaygood
Labels
3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-typingtype-featureA feature request or enhancement
@AlexWaygood

Description

@AlexWaygood

Feature or enhancement

I propose that we deprecate in Python 3.13 the following unusual ways of constructing atyping.NamedTuple:

fromtypingimportNamedTupleFoo=NamedTuple("Foo",x=int,y=int)# NamedTuple with "x" and "y" fieldsBar=NamedTuple("Bar")# empty NamedTupleBaz=NamedTuple("Baz",None)# empty NamedTupleEggs=NamedTuple("Eggs",fields=None)# empty NamedTuple

Pitch

typing.NamedTuple has been around for quite a while now, but none of the above methods of constructingNamedTuples are supported by type checkers. If they're still unsupported by type checkers after all this time, they're unlikely to ever be supported by type checkers.

Deprecating, and eventually removing, these ways of constructingNamedTuples will allow us to simplify the code at runtime. It will also be less confusing for users. Every way in which the runtime and type checkers differ in behaviour is a potential point of confusion for users; in general, we should work to keep these points of difference to a minimum.

These methods of constructingNamedTuples are not commonly seen in the wild. They're also pretty redundant -- if you want to construct aNamedTuple in one line, in a single function call, you can do it like this, which is supported by type checkers:

Foo=NamedTuple("Foo", [("x",int), ("y",int)])

If you want to construct an emptyNamedTuple, meanwhile, you can do it in one of the following two ways, which are both supported by type checkers:

Bar=NamedTuple("Bar", [])classBaz(NamedTuple): ...

Previous discussion

For very similar reasons, we previously deprecated and removed the keyword-argument syntax for creating TypedDicts. This was deprecated in 3.11, and removed in 3.13:

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-typingtype-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