You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NamedTuple single line declaration gives error "Too many arguments for NamedTuple() [misc] #11047
mypy --show-error-codes test.py
test.py:[line-no]: error: Too many arguments forNamedTuple() [misc]
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
fullname must be str only, right now bytes is allowed by mypy, but not cpython:
>>>importcollections>>>collections.namedtuple(b'custom', ['x'])
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/collections/__init__.py", line390, innamedtupleraiseValueError('Type names and field names must be valid 'ValueError: Typenamesandfieldnamesmustbevalididentifiers: "b'custom'"
rename=True is not supported:
>>>N=namedtuple(typename='N', field_names=['raise'], rename=True)
>>>N=namedtuple(typename='N', field_names=['raise'])
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/collections/__init__.py", line393, innamedtupleraiseValueError('Type names and field names cannot be a 'ValueError: Typenamesandfieldnamescannotbeakeyword: 'raise'
Status update: I am rewritting argument parsing in semanal_namedtuple completely. It was way to complicated.
Right now I can parse all possible valid combinations of NamedTuple and its arguments.
Something like:
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. """
Activity
sobolevn commentedon Sep 17, 2021
Working on it. It might take some time though.
Adds better NamedTuple error messages (#11127)
sobolevn commentedon Sep 22, 2021
There are multiple other bugs / missing features.
fullnamemust bestronly, right nowbytesis allowed bymypy, but notcpython:rename=Trueis not supported:module=is not supportedsobolevn commentedon Sep 26, 2021
Status update: I am rewritting argument parsing in
semanal_namedtuplecompletely. It was way to complicated.Right now I can parse all possible valid combinations of
NamedTupleand its arguments.Something like: