[issue45972] typing.NamedTuple with default arguments without type annotations is falsy
Erik Montnemery
report at bugs.python.org
Fri Dec 3 06:44:35 EST 2021
Erik Montnemery <erik at montnemery.com> added the comment:
Maybe something like this:
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 735d477db4..8de913d8db 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -1291,7 +1291,8 @@ These are not used in annotations. They are building blocks for declaring types.
.. class:: NamedTuple
- Typed version of :func:`collections.namedtuple`.
+ Typed version of :func:`collections.namedtuple`, annotated fields are passed
+ to an underlying `collections.namedtuple`.
Usage::
@@ -1311,9 +1312,20 @@ These are not used in annotations. They are building blocks for declaring types.
employee = Employee('Guido')
assert employee.id == 3
+ assert employee == ('Guido', 3)
Fields with a default value must come after any fields without a default.
+ Non-annotated fields will not be part of the `collections.namedtuple`::
+
+ class Employee(NamedTuple):
+ name = 'Guido'
+ id = 3
+
+ employee = Employee()
+ assert employee.id == 3
+ assert not employee # Passes because the collections.namedtuple is empty
+
The resulting class has an extra attribute ``__annotations__`` giving a
dict that maps the field names to the field types. (The field names are in
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45972>
_______________________________________
More information about the Python-bugs-list
mailing list