Skip to content

Commit 99cec2d

Browse files
authored
Reduce duplication of code between typing and typing_extensions (#7075)
1 parent 54eef59 commit 99cec2d

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

stdlib/@python2/typing.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TypeVar:
2020

2121
_promote = object()
2222

23+
# N.B. Keep this definition in sync with typing_extensions._SpecialForm
2324
class _SpecialForm(object):
2425
def __getitem__(self, typeargs: Any) -> object: ...
2526

@@ -44,7 +45,7 @@ _F = TypeVar("_F", bound=Callable[..., Any])
4445
def final(f: _F) -> _F: ...
4546
def overload(f: _F) -> _F: ...
4647

47-
Literal: _SpecialForm = ...
48+
Literal: _SpecialForm
4849
# TypedDict is a (non-subscriptable) special form.
4950
TypedDict: object
5051

stdlib/@python2/typing_extensions.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ from typing import ( # noqa Y022
2727
_T = TypeVar("_T")
2828
_F = TypeVar("_F", bound=Callable[..., Any])
2929

30+
# unfortunately we have to duplicate this class definition from typing.pyi or we break pytype
3031
class _SpecialForm:
31-
def __getitem__(self, typeargs: Any) -> Any: ...
32+
def __getitem__(self, typeargs: Any) -> object: ...
3233

3334
# This alias for above is kept here for backwards compatibility.
3435
runtime = runtime_checkable

stdlib/typing.pyi

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TypeVar:
2929
# Used for an undocumented mypy feature. Does not exist at runtime.
3030
_promote = object()
3131

32+
# N.B. Keep this definition in sync with typing_extensions._SpecialForm
3233
class _SpecialForm:
3334
def __getitem__(self, typeargs: Any) -> object: ...
3435
if sys.version_info >= (3, 10):
@@ -138,13 +139,9 @@ if sys.version_info >= (3, 9):
138139
# Predefined type variables.
139140
AnyStr = TypeVar("AnyStr", str, bytes) # noqa: Y001
140141

141-
if sys.version_info >= (3, 8):
142-
# This class did actually exist in 3.7, but had a different base.
143-
# We'll just pretend it didn't exist though: the main external use case for _ProtocolMeta is
144-
# to inherit from for your own custom protocol metaclasses. If you're using 3.7, at runtime
145-
# you'd use typing_extensions.Protocol, which would be unrelated to typing._ProtocolMeta and
146-
# so you'd run into metaclass conflicts at runtime if you used typing._ProtocolMeta.
147-
class _ProtocolMeta(ABCMeta): ...
142+
# Technically in 3.7 this inherited from GenericMeta. But let's not reflect that, since
143+
# type checkers tend to assume that Protocols all have the ABCMeta metaclass.
144+
class _ProtocolMeta(ABCMeta): ...
148145

149146
# Abstract base classes.
150147

stdlib/typing_extensions.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ from typing import ( # noqa Y022
3535
_T = TypeVar("_T")
3636
_F = TypeVar("_F", bound=Callable[..., Any])
3737

38+
# unfortunately we have to duplicate this class definition from typing.pyi or we break pytype
3839
class _SpecialForm:
39-
def __getitem__(self, typeargs: Any) -> Any: ...
40+
def __getitem__(self, typeargs: Any) -> object: ...
41+
if sys.version_info >= (3, 10):
42+
def __or__(self, other: Any) -> _SpecialForm: ...
43+
def __ror__(self, other: Any) -> _SpecialForm: ...
4044

4145
# This alias for above is kept here for backwards compatibility.
4246
runtime = runtime_checkable
@@ -51,11 +55,6 @@ Literal: _SpecialForm
5155

5256
def IntVar(name: str) -> Any: ... # returns a new TypeVar
5357

54-
if sys.version_info < (3, 8):
55-
# Technically in 3.6 this inherited from GenericMeta. But let's not reflect that, since
56-
# type checkers tend to assume that Protocols all have the ABCMeta metaclass.
57-
class _ProtocolMeta(abc.ABCMeta): ...
58-
5958
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
6059
class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
6160
__required_keys__: frozenset[str]

0 commit comments

Comments
 (0)