From b6122f0164c527d26396bbb3f3768682ec58f6da Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 5 Oct 2022 12:20:32 +0300 Subject: [PATCH 1/3] Add metaclasses for `string.Template` and `dataclasses.InitVar` --- stdlib/dataclasses.pyi | 9 +++++++-- stdlib/string.pyi | 11 +++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 04ae771fc064..29154176c3ef 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -4,7 +4,7 @@ import types from builtins import type as Type # alias to avoid name clashes with fields named "type" from collections.abc import Callable, Iterable, Mapping from typing import Any, Generic, Protocol, TypeVar, overload -from typing_extensions import Literal +from typing_extensions import Literal, TypeAlias if sys.version_info >= (3, 9): from types import GenericAlias @@ -217,7 +217,12 @@ def is_dataclass(obj: Any) -> bool: ... class FrozenInstanceError(AttributeError): ... -class InitVar(Generic[_T]): +if sys.version_info >= (3, 9): + _InitVarMeta: TypeAlias = type +else: + class _InitVarMeta(type): ... + +class InitVar(Generic[_T], metaclass=_InitVarMeta): type: Type[_T] def __init__(self, type: Type[_T]) -> None: ... if sys.version_info >= (3, 9): diff --git a/stdlib/string.pyi b/stdlib/string.pyi index 5a79e9e76752..6fb803fe53be 100644 --- a/stdlib/string.pyi +++ b/stdlib/string.pyi @@ -3,7 +3,7 @@ from _typeshed import StrOrLiteralStr from collections.abc import Iterable, Mapping, Sequence from re import Pattern, RegexFlag from typing import Any, ClassVar, overload -from typing_extensions import LiteralString +from typing_extensions import LiteralString, TypeAlias __all__ = [ "ascii_letters", @@ -32,7 +32,14 @@ whitespace: LiteralString def capwords(s: StrOrLiteralStr, sep: StrOrLiteralStr | None = ...) -> StrOrLiteralStr: ... -class Template: +if sys.version_info >= (3, 9): + _TemplateMetaclass: TypeAlias = type +else: + class _TemplateMetaclass(type): + pattern: ClassVar[str] + def __init__(cls, name: str, bases: tuple[type, ...], dct: dict[str, Any]) -> None: ... + +class Template(metaclass=_TemplateMetaclass): template: str delimiter: ClassVar[str] idpattern: ClassVar[str] From a31fc4820685618aa71025a079385fc386425905 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 5 Oct 2022 12:24:50 +0300 Subject: [PATCH 2/3] Update dataclasses.pyi --- stdlib/dataclasses.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 29154176c3ef..0d9f6411cce8 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -220,7 +220,9 @@ class FrozenInstanceError(AttributeError): ... if sys.version_info >= (3, 9): _InitVarMeta: TypeAlias = type else: - class _InitVarMeta(type): ... + class _InitVarMeta(type): + # Not used, instead `InitVar.__class_getitem__` is called. + def __getitem__(self, params: _T) -> InitVar[_T]: ... class InitVar(Generic[_T], metaclass=_InitVarMeta): type: Type[_T] From f3f3f3b592b26a9d727f975c3b2699d70a934915 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 5 Oct 2022 12:36:20 +0300 Subject: [PATCH 3/3] Update dataclasses.pyi --- stdlib/dataclasses.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 0d9f6411cce8..560147f9e96b 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -222,7 +222,7 @@ if sys.version_info >= (3, 9): else: class _InitVarMeta(type): # Not used, instead `InitVar.__class_getitem__` is called. - def __getitem__(self, params: _T) -> InitVar[_T]: ... + def __getitem__(self, params: Any) -> InitVar[Any]: ... class InitVar(Generic[_T], metaclass=_InitVarMeta): type: Type[_T]