GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Deprecate unusual ways of creating `typing.NamedTuple` classes · Issue #105566 · python/cpython

Deprecate unusual ways of creating typing.NamedTuple classes #105566

New issue
Copy link
New issue
Copy link
Closed
#105609
Closed
Deprecate unusual ways of creating typing.NamedTuple classes#105566
#105609
Copy link
Assignees
AlexWaygood
Labels
3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@AlexWaygood
AlexWaygood
opened on Jun 9, 2023
Issue body actions

Feature or enhancement

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

from typing import NamedTuple

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

Pitch

typing.NamedTuple has been around for quite a while now, but none of the above methods of constructing NamedTuples 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 constructing NamedTuples 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 constructing NamedTuples are not commonly seen in the wild. They're also pretty redundant -- if you want to construct a NamedTuple 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 empty NamedTuple, meanwhile, you can do it in one of the following two ways, which are both supported by type checkers:

Bar = NamedTuple("Bar", [])
class Baz(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:

  • Deprecate keyword args syntax for TypedDict definition #90224
  • Remove kwargs-based TypedDict creation #104786

Linked PRs

  • gh-105566: Deprecate unusual ways of creating typing.NamedTuple classes #105609
Reacted by Jelle Zijlstra, Shantanu and sobolevn

Activity

  1. AlexWaygood
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-typing
    3.13only security fixesonly security fixes
    on Jun 9, 2023
  2. AlexWaygood
    changed the title [-]Deprecate unusual ways of constructing `NamedTuple`s[/-] [+]Deprecate unusual ways of creating `typing.NamedTuple` classes[/+] on Jun 9, 2023
  3. AlexWaygood
    added a commit that references this issue on Jun 9, 2023

    pythongh-105566: Deprecate unusual ways of constructing `typing.Named…

    be88d69
  4. AlexWaygood
    self-assigned this
    on Jun 10, 2023
  5. AlexWaygood
    closed this as completedin #105609on Jun 14, 2023
  6. AlexWaygood
    added a commit that references this issue on Jun 14, 2023

    gh-105566: Deprecate unusual ways of creating `typing.NamedTuple` cla…

    ad56340
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @AlexWaygood
    AlexWaygood

Labels

3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.