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

[0.970 backport] add typing_extensions.NamedTuple #13149

Merged
merged 1 commit into from Jul 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions mypy/typeshed/stdlib/typing_extensions.pyi
@@ -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 @@ -52,6 +54,7 @@ __all__ = [
"Counter",
"Deque",
"DefaultDict",
"NamedTuple",
"OrderedDict",
"TypedDict",
"SupportsIndex",
Expand Down Expand Up @@ -189,9 +192,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 @@ -233,3 +238,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: ...