Skip to content

Clarify namedtuple member rules #1979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add conformance tests for namedtuple spec changes
  • Loading branch information
yangdanny97 committed Jul 11, 2025
commit c7906f8e7fc60212acad600626f064dc654eb2d1
27 changes: 27 additions & 0 deletions conformance/tests/namedtuples_define_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ class Point(NamedTuple):
p10 = Point(1, 2, "", other="") # E


# > Fields must be annotated attributes - methods and un-annotated attributes are not
# > considered fields.


class Point2(NamedTuple):
x: int
y: int
units = "meters" # Not a field

def is_origin(self) -> int: # Not a field
return self.x == 0 and self.y == 0


p11 = Point2(1, 2)
assert_type(p11, Point2)
x, y = p11

p12 = Point2(1, 2, "") # E


# > Field names may not start with an underscore.

class Point3(NamedTuple):
x: int
_y: int # E: illegal field name


# > The runtime implementation of ``NamedTuple`` enforces that fields with default
# > values must come after fields without default values. Type checkers should
# > likewise enforce this restriction::
Expand Down
17 changes: 10 additions & 7 deletions conformance/tests/namedtuples_define_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@
p6_4 = Point6(x=1.1, y=2) # E


# > At runtime, the ``namedtuple`` function disallows field names that are
# > illegal Python identifiers and either raises an exception or replaces these
# > fields with a parameter name of the form ``_N``. The behavior depends on
# > the value of the ``rename`` argument. Type checkers may replicate this
# > behavior statically.
# > At runtime, the ``namedtuple`` function disallows field names that begin with
# > an underscore or are illegal Python identifiers, and either raises an exception
# > or replaces these fields with a parameter name of the form ``_N``. The behavior
# > depends on the value of the ``rename`` argument. Type checkers may replicate
# > this behavior statically.

NT1 = namedtuple("NT1", ["a", "a"]) # E?: duplicate field name
NT2 = namedtuple("NT2", ["abc", "def"]) # E?: illegal field name
NT3 = namedtuple("NT3", ["abc", "def"], rename=False) # E?: illegal field name
NT4 = namedtuple("NT4", ["abc", "_d"], rename=False) # E?: illegal field name

NT4 = namedtuple("NT4", ["abc", "def"], rename=True) # OK
NT4(abc="", _1="") # OK
NT5 = namedtuple("NT5", ["abc", "def"], rename=True) # OK
NT5(abc="", _1="") # OK
NT6 = namedtuple("NT6", ["abc", "_d"], rename=True) # OK
NT6(abc="", _1="") # OK


# > The ``namedtuple`` function also supports a ``defaults`` keyword argument that
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy