-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
NamedTuple single line declaration gives error "Too many arguments for NamedTuple() [misc] #11047
Labels
bug
mypy got something wrong
semantic-analyzer
Problems that happen during semantic analysis
topic-named-tuple
Comments
Working on it. It might take some time though. |
JukkaL
pushed a commit
that referenced
this issue
Sep 22, 2021
While working on #11047 I've noticed that mypy reports the same error for both collection.namedtuple() and typing.NamedTuple(). I've tried to fix to report the correct name.
There are multiple other bugs / missing features.
>>> import collections
>>> collections.namedtuple(b'custom', ['x'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/collections/__init__.py", line 390, in namedtuple
raise ValueError('Type names and field names must be valid '
ValueError: Type names and field names must be valid identifiers: "b'custom'"
>>> N = namedtuple(typename='N', field_names=['raise'], rename=True)
>>> N = namedtuple(typename='N', field_names=['raise'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/collections/__init__.py", line 393, in namedtuple
raise ValueError('Type names and field names cannot be a '
ValueError: Type names and field names cannot be a keyword: 'raise'
|
Status update: I am rewritting argument parsing in Right now I can parse all possible valid combinations of def _parse_typing_namedtuple(self, typename: str) -> NamedTupleStructure:
"""Parsing ``typing.NamedTuple()`` call.
In all of the examples below arguments can be named or positional.
Possible valid cases:
1. ``N = NamedTuple('N')`` - empty namedtuple,
2. ``N = NamedTuple('N', [('a', int)])``
3. ``N = NamedTuple(typename='N', fields=(('a', int),))``
4. ``N = NamedTuple('N', a=int)`` with kwargs
Corner cases, but still valid:
7. ``N = NamedTuple('N', ())``, - empty namedtuple,
we also count ``[]`` here, ``fields`` can be named or positional
6. ``N = NamedTuple('N', fields=None, a=int)``,
in this case ``fields`` will not be present as a namedtuple field
7. ``N = NamedTuple('N', None, a=int)``
8. ``N = NamedTuple(a=int, typename='N')``
kw-arguments can be in a different order, that's fine
Everything else is considered **invalid**.
We only accept statically known types.
""" |
This was referenced Sep 26, 2021
AlexWaygood
added
topic-named-tuple
semantic-analyzer
Problems that happen during semantic analysis
labels
Mar 26, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
mypy got something wrong
semantic-analyzer
Problems that happen during semantic analysis
topic-named-tuple
Bug Report
typing.NamedTuple one line declaration gives incorrect error
To Reproduce
Expected Behavior
mypy doesn't give this error
given the above syntax is valid python and works fine, I would expect it to not throw an error, like the following old structure declaration does not, which is labeled as "back-ward compatible usage" in the official doc
Actual Behavior
Your Environment
The text was updated successfully, but these errors were encountered: