From 409e97c5ad22defa069656759babe00ae605a85a Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 4 Dec 2021 14:11:21 +0300 Subject: [PATCH 1/2] Do not use `Any` in `NamedTuple()` constructor While working on https://github.com/python/mypy/pull/11206 I've found that this test now fails: https://github.com/python/mypy/blob/cee5d3e9a29f43a10a8eeca760976657bf1689c9/test-data/unit/check-flags.test#L1177-L1185 Looks like `object` is better suited here. `Literal[1]` is also an object, `Union[int, str]` is an object as well. So, it should always work (I've tested that the current `mypy` agrees with me). And no `Any` related problems. --- stdlib/typing.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index a56ea8c8c4d1..5fe16824d7ea 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -681,9 +681,9 @@ class NamedTuple(Tuple[Any, ...]): _fields: Tuple[str, ...] _source: str @overload - def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ... + def __init__(self, typename: str, fields: Iterable[tuple[str, object]] = ...) -> None: ... @overload - def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ... + def __init__(self, typename: str, fields: None = ..., **kwargs: object) -> None: ... @classmethod def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ... if sys.version_info >= (3, 8): From 9eb47db8e7b2affaf5655b3690cacb10c3396b98 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 4 Dec 2021 17:42:12 +0300 Subject: [PATCH 2/2] Update typing.pyi --- stdlib/typing.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 5fe16824d7ea..5bb92e4f92e6 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -680,6 +680,9 @@ class NamedTuple(Tuple[Any, ...]): _field_defaults: dict[str, Any] _fields: Tuple[str, ...] _source: str + # NOTE: The overloads for __init__ are not strictly accurate + # (type for `fields` and `**kwargs` should be more precise than `object`) + # but typing `NamedTuple` like this makes it easier for mypy to special-case. See #6489 @overload def __init__(self, typename: str, fields: Iterable[tuple[str, object]] = ...) -> None: ... @overload