Clarify why some module-level objects in typing have default values (#7037)

This commit is contained in:
Alex Waygood
2022-01-29 01:38:25 +00:00
committed by GitHub
parent 470ea31ccd
commit 7e79706ddd
4 changed files with 24 additions and 14 deletions

View File

@@ -23,12 +23,19 @@ _promote = object()
class _SpecialForm(object):
def __getitem__(self, typeargs: Any) -> object: ...
Union: _SpecialForm = ...
Optional: _SpecialForm
Tuple: _SpecialForm
# Unlike the vast majority module-level objects in stub files,
# these `_SpecialForm` objects in typing need the default value `= ...`,
# due to the fact that they are used elswhere in the same file.
# Otherwise, flake8 erroneously flags them as undefined.
# `_SpecialForm` objects in typing.py that are not used elswhere in the same file
# do not need the default value assignment.
Generic: _SpecialForm = ...
Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Union: _SpecialForm = ...
Optional: _SpecialForm
Tuple: _SpecialForm
Type: _SpecialForm
ClassVar: _SpecialForm
Final: _SpecialForm

View File

@@ -14,26 +14,24 @@ from typing import ( # noqa Y022
Mapping,
NewType as NewType,
NoReturn as NoReturn,
Protocol as Protocol,
Text as Text,
Type as Type,
TypeVar,
ValuesView,
_Alias,
overload as overload,
runtime_checkable as runtime_checkable,
)
_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., Any])
_TC = TypeVar("_TC", bound=type[object])
class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
def runtime_checkable(cls: _TC) -> _TC: ...
# This alias for above is kept here for backwards compatibility.
runtime = runtime_checkable
Protocol: _SpecialForm = ...
Final: _SpecialForm
def final(f: _F) -> _F: ...

View File

@@ -41,16 +41,23 @@ _T = TypeVar("_T")
def overload(func: _F) -> _F: ...
# Unlike the vast majority module-level objects in stub files,
# these `_SpecialForm` objects in typing need the default value `= ...`,
# due to the fact that they are used elswhere in the same file.
# Otherwise, flake8 erroneously flags them as undefined.
# `_SpecialForm` objects in typing.py that are not used elswhere in the same file
# do not need the default value assignment.
Union: _SpecialForm = ...
Optional: _SpecialForm
Tuple: _SpecialForm
Generic: _SpecialForm = ...
# Protocol is only present in 3.8 and later, but mypy needs it unconditionally
Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Type: _SpecialForm = ...
ClassVar: _SpecialForm
NoReturn: _SpecialForm = ...
Optional: _SpecialForm
Tuple: _SpecialForm
ClassVar: _SpecialForm
if sys.version_info >= (3, 8):
Final: _SpecialForm
def final(f: _T) -> _T: ...

View File

@@ -22,26 +22,24 @@ from typing import ( # noqa Y022
Mapping,
NewType as NewType,
NoReturn as NoReturn,
Protocol as Protocol,
Text as Text,
Type as Type,
TypeVar,
ValuesView,
_Alias,
overload as overload,
runtime_checkable as runtime_checkable,
)
_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., Any])
_TC = TypeVar("_TC", bound=type[object])
class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
def runtime_checkable(cls: _TC) -> _TC: ...
# This alias for above is kept here for backwards compatibility.
runtime = runtime_checkable
Protocol: _SpecialForm = ...
Final: _SpecialForm
Self: _SpecialForm
Required: _SpecialForm