-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
typing.NamedTuple with default arguments without type annotations is falsy #90130
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
Comments
typing.NamedTuple behaves in surprising ways when it has default arguments which lack type annotations: >>> from typing import NamedTuple
>>> class MyTuple(NamedTuple):
... a = 1000
...
>>> tmp = MyTuple()
>>> tmp.a
1000
>>> len(tmp)
0
>>> bool(tmp)
False Tested in Python 3.8 and 3.9 |
What's happening is that typing.NamedTuple ignores non-annotated attributes entirely when computing the names it passes along to namedtuple(), so here "a" is just a class attribute. You're accessing it from there, but the tuple itself is entirely empty. Perhaps it should error out if no names at all are found? |
I don't think we'd want to prohibit zero-length namedtuples (or NamedTuples). I've used them, especially when I'm dynamically creating them. This is just a side effect of how Python works. I don't think there's anything to do here, except maybe mention it in the docs, and I'm not even sure that's a good idea. |
I agree that prohibiting zero-length NamedTuples seems like a bad idea, and also agree that this probably doesn't need to be documented. The behaviour here definitely looks weird at first glance, but it's probably not a good idea to tamper with the __bool__() methods of NamedTuple classes either: an empty NamedTuple probably *should* be falsey by default, even if it has a class attribute. |
I think elaborating in the documentation that only annotated attributes make it to the underlying namedtuple() would be helpful, it's not obvious that they are instead just class attributes. |
To me, the fact that NamedTuple uses class attributes to provide field defaults feels like an implementation detail that is only relevant to an unusual edge case. Where do you think the documentation should be improved, and what is your suggested wording? :) |
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.
\```py
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 |
Agree that it's good to document that typing.NamedTuple only picks up annotated attributes. Would you be willing to submit a PR? |
This implementation detail would have to be part of the specification of NamedTuple though, right? It doesn't feel like something that we should leave unspecified.
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: