Skip to content
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

Add typing_extensions.NamedTuple #8295

Merged
merged 7 commits into from
Jul 14, 2022
Merged
26 changes: 26 additions & 0 deletions stdlib/typing_extensions.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import abc
import collections
import sys
from _typeshed import IdentityFunction, Self as TypeshedSelf # see #6932 for why the Self alias cannot have a leading underscore
from collections.abc import Iterable
from typing import ( # noqa: Y022,Y027,Y039
TYPE_CHECKING as TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -54,6 +56,7 @@ __all__ = [
"Counter",
"Deque",
"DefaultDict",
"NamedTuple",
"OrderedDict",
"TypedDict",
"SupportsIndex",
Expand Down Expand Up @@ -193,9 +196,11 @@ else:
def is_typeddict(tp: object) -> bool: ...

# New things in 3.11
# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11
if sys.version_info >= (3, 11):
from typing import (
LiteralString as LiteralString,
NamedTuple as NamedTuple,
Never as Never,
NotRequired as NotRequired,
Required as Required,
Expand Down Expand Up @@ -237,3 +242,24 @@ else:
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
**kwargs: object,
) -> IdentityFunction: ...

class NamedTuple(tuple[Any, ...]):
if sys.version_info < (3, 8):
_field_types: collections.OrderedDict[str, type]
elif sys.version_info < (3, 9):
_field_types: dict[str, type]
_field_defaults: dict[str, Any]
_fields: tuple[str, ...]
_source: str
@overload
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ...
@overload
def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...
@classmethod
def _make(cls: type[TypeshedSelf], iterable: Iterable[Any]) -> TypeshedSelf: ...
if sys.version_info >= (3, 8):
def _asdict(self) -> dict[str, Any]: ...
else:
def _asdict(self) -> collections.OrderedDict[str, Any]: ...

def _replace(self: TypeshedSelf, **kwargs: Any) -> TypeshedSelf: ...