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
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:
>>>fromtypingimportNamedTuple>>>NamedTuple('a', (), f=int)
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/typing.py", line1864, inNamedTupleraiseTypeError("Either list of fields or keywords"TypeError: EitherlistoffieldsorkeywordscanbeprovidedtoNamedTuple, notboth
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.
The text was updated successfully, but these errors were encountered:
While working on python/mypy#11047 I've noticed that there's an issue with
typing.NamedTuple
Right now
__init__
oftyping.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
andkwargs
:Source: https://github.com/python/cpython/blob/f56268a2cd38b3fe2be1e4361d3d8b581e73559b/Lib/typing.py#L2290-L2294
So, it should be an overload instead:
Notice, that
fields=None
is still allowed: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]
orTuple[str, type]
? It is supposed to be types. But, it is not enforced anywhere (exceptmypy
) as far as I know.I will send a PR with the fix.
The text was updated successfully, but these errors were encountered: