Skip to content

Commit

Permalink
[0.970 backport] add typing_extensions.NamedTuple (#13149)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Jul 16, 2022
1 parent 9f99400 commit d06dcf0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions mypy/typeshed/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 @@ -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: ...

0 comments on commit d06dcf0

Please sign in to comment.