-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Closed
Description
While working on python/mypy#11047 I've noticed that there's an issue with typing.NamedTuple
Right now __init__
of typing.NamedTuple
has type: def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., **kwargs: Any) -> None: ...
which is not correct.
Why? Let's try to add both fields
and kwargs
:
>>> from typing import NamedTuple
>>> NamedTuple('a', (), f=int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line 1864, in NamedTuple
raise TypeError("Either list of fields or keywords"
TypeError: Either list of fields or keywords can be provided to NamedTuple, not both
So, it should be an overload instead:
@overload
def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ...) -> None: ...
@overload
def __init__(self, typename: str, **kwargs: Any) -> None: ...
Notice, that fields=None
is still allowed:
>>> NamedTuple('a', fields=None, f=int)
But, I don't think that we should have def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...
instead.
One more thing: is it Tuple[str, Any]
or Tuple[str, type]
? It is supposed to be types. But, it is not enforced anywhere (except mypy
) as far as I know.
I will send a PR with the fix.
Metadata
Metadata
Assignees
Labels
No labels