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

Use PEP 585 syntax in typing and typing_extensions, and remove module-level defaults where possible #7036

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions stdlib/@python2/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Any = object()

class TypeVar:
__name__: str
__bound__: Type[Any] | None
__constraints__: Tuple[Type[Any], ...]
__bound__: type[Any] | None
__constraints__: tuple[type[Any], ...]
__covariant__: bool
__contravariant__: bool
def __init__(
self, name: str, *constraints: Type[Any], bound: Type[Any] | None = ..., covariant: bool = ..., contravariant: bool = ...
self, name: str, *constraints: type[Any], bound: type[Any] | None = ..., covariant: bool = ..., contravariant: bool = ...
) -> None: ...

_promote = object()
Expand All @@ -24,22 +24,22 @@ class _SpecialForm(object):
def __getitem__(self, typeargs: Any) -> object: ...

Union: _SpecialForm = ...
Optional: _SpecialForm = ...
Tuple: _SpecialForm = ...
Optional: _SpecialForm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this change. It's confusing to readers that half of these seemingly identical names have = ... and the other half don't.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the best solution is to copy the flake8 rule for undefined vars to flake8-pyi and adapt it for stub files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can just switch off my proposed Y032 for typing.pyi and typing_extensions.pyi.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Third option: move all the _SpecialForm objects in typing/typing_extensions that need a default value together in the stub, and add a comment explaining why they need a default value when the other _SpecialForm objects don't.

Tuple: _SpecialForm
Generic: _SpecialForm = ...
Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Type: _SpecialForm = ...
ClassVar: _SpecialForm = ...
Final: _SpecialForm = ...
Type: _SpecialForm
ClassVar: _SpecialForm
Final: _SpecialForm
_F = TypeVar("_F", bound=Callable[..., Any])

def final(f: _F) -> _F: ...
def overload(f: _F) -> _F: ...

Literal: _SpecialForm = ...
Literal: _SpecialForm
# TypedDict is a (non-subscriptable) special form.
TypedDict: object = ...
TypedDict: object

class GenericMeta(type): ...

Expand All @@ -58,7 +58,7 @@ _V_co = TypeVar("_V_co", covariant=True) # Any type covariant containers.
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
_T_contra = TypeVar("_T_contra", contravariant=True) # Ditto contravariant.
_TC = TypeVar("_TC", bound=Type[object])
_TC = TypeVar("_TC", bound=type[object])

def no_type_check(f: _F) -> _F: ...
def no_type_check_decorator(decorator: _F) -> _F: ...
Expand Down Expand Up @@ -140,7 +140,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
@overload
@abstractmethod
def throw(
self, __typ: Type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ...
self, __typ: type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ...
) -> _T_co: ...
@overload
@abstractmethod
Expand Down Expand Up @@ -240,10 +240,10 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
class MappingView(object):
def __len__(self) -> int: ...

class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ...
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
def __iter__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...

class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ...
Expand All @@ -259,7 +259,7 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
class ContextManager(Protocol[_T_co]):
def __enter__(self) -> _T_co: ...
def __exit__(
self, __exc_type: Type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None
self, __exc_type: type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None
) -> bool | None: ...

class Mapping(Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
Expand All @@ -274,10 +274,10 @@ class Mapping(Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
def get(self, k: _KT, default: _VT_co | _T) -> _VT_co | _T: ...
def keys(self) -> list[_KT]: ...
def values(self) -> list[_VT_co]: ...
def items(self) -> list[Tuple[_KT, _VT_co]]: ...
def items(self) -> list[tuple[_KT, _VT_co]]: ...
def iterkeys(self) -> Iterator[_KT]: ...
def itervalues(self) -> Iterator[_VT_co]: ...
def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
def iteritems(self) -> Iterator[tuple[_KT, _VT_co]]: ...
def __contains__(self, o: object) -> bool: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
Expand All @@ -293,12 +293,12 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
def pop(self, k: _KT) -> _VT: ...
@overload
def pop(self, k: _KT, default: _VT | _T = ...) -> _VT | _T: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def popitem(self) -> tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
def update(self, __m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
@overload
def update(self, __m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def update(self, __m: Iterable[tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
@overload
def update(self, **kwargs: _VT) -> None: ...

Expand Down Expand Up @@ -355,7 +355,7 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
def __enter__(self) -> IO[AnyStr]: ...
@abstractmethod
def __exit__(
self, t: Type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
self, t: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...

class BinaryIO(IO[str]):
Expand Down Expand Up @@ -404,16 +404,16 @@ class Match(Generic[AnyStr]):
@overload
def group(self, group1: str) -> AnyStr: ...
@overload
def group(self, group1: int, group2: int, *groups: int) -> Tuple[AnyStr, ...]: ...
def group(self, group1: int, group2: int, *groups: int) -> tuple[AnyStr, ...]: ...
@overload
def group(self, group1: str, group2: str, *groups: str) -> Tuple[AnyStr, ...]: ...
def groups(self, default: AnyStr = ...) -> Tuple[AnyStr, ...]: ...
def group(self, group1: str, group2: str, *groups: str) -> tuple[AnyStr, ...]: ...
def groups(self, default: AnyStr = ...) -> tuple[AnyStr, ...]: ...
def groupdict(self, default: AnyStr = ...) -> Dict[str, AnyStr]: ...
def start(self, __group: int | str = ...) -> int: ...
def end(self, __group: int | str = ...) -> int: ...
def span(self, __group: int | str = ...) -> Tuple[int, int]: ...
def span(self, __group: int | str = ...) -> tuple[int, int]: ...
@property
def regs(self) -> Tuple[Tuple[int, int], ...]: ... # undocumented
def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented

# We need a second TypeVar with the same definition as AnyStr, because
# Pattern is generic over AnyStr (determining the type of its .pattern
Expand All @@ -438,17 +438,17 @@ class Pattern(Generic[AnyStr]):
@overload
def sub(self, repl: Callable[[Match[_AnyStr2]], _AnyStr2], string: _AnyStr2, count: int = ...) -> _AnyStr2: ...
@overload
def subn(self, repl: _AnyStr2, string: _AnyStr2, count: int = ...) -> Tuple[_AnyStr2, int]: ...
def subn(self, repl: _AnyStr2, string: _AnyStr2, count: int = ...) -> tuple[_AnyStr2, int]: ...
@overload
def subn(self, repl: Callable[[Match[_AnyStr2]], _AnyStr2], string: _AnyStr2, count: int = ...) -> Tuple[_AnyStr2, int]: ...
def subn(self, repl: Callable[[Match[_AnyStr2]], _AnyStr2], string: _AnyStr2, count: int = ...) -> tuple[_AnyStr2, int]: ...

# Functions

def get_type_hints(
obj: Callable[..., Any], globalns: Dict[Text, Any] | None = ..., localns: Dict[Text, Any] | None = ...
) -> None: ...
@overload
def cast(tp: Type[_T], obj: Any) -> _T: ...
def cast(tp: type[_T], obj: Any) -> _T: ...
@overload
def cast(tp: str, obj: Any) -> Any: ...
@overload
Expand All @@ -457,11 +457,11 @@ def cast(tp: object, obj: Any) -> Any: ...
# Type constructors

# NamedTuple is special-cased in the type checker
class NamedTuple(Tuple[Any, ...]):
_fields: Tuple[str, ...]
def __init__(self, typename: Text, fields: Iterable[Tuple[Text, Any]] = ..., **kwargs: Any) -> None: ...
class NamedTuple(tuple[Any, ...]):
_fields: tuple[str, ...]
def __init__(self, typename: Text, fields: Iterable[tuple[Text, Any]] = ..., **kwargs: Any) -> None: ...
@classmethod
def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ...
def _make(cls: type[Self], iterable: Iterable[Any]) -> Self: ...
def _asdict(self) -> Dict[str, Any]: ...
def _replace(self: Self, **kwargs: Any) -> Self: ...

Expand All @@ -480,7 +480,7 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
def viewvalues(self) -> ValuesView[object]: ...
def __delitem__(self, k: NoReturn) -> None: ...

def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
def NewType(name: str, tp: type[_T]) -> type[_T]: ...

# This itself is only available during type checking
def type_check_only(func_or_cls: _F) -> _F: ...
16 changes: 8 additions & 8 deletions stdlib/@python2/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def runtime_checkable(cls: _TC) -> _TC: ...
# This alias for above is kept here for backwards compatibility.
runtime = runtime_checkable
Protocol: _SpecialForm = ...
Final: _SpecialForm = ...
Final: _SpecialForm

def final(f: _F) -> _F: ...

Literal: _SpecialForm = ...
Literal: _SpecialForm

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

Expand All @@ -58,7 +58,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
def __delitem__(self, k: NoReturn) -> None: ...

# TypedDict is a (non-subscriptable) special form.
TypedDict: object = ...
TypedDict: object

OrderedDict = _Alias()

Expand All @@ -69,8 +69,8 @@ def get_type_hints(
include_extras: bool = ...,
) -> dict[str, Any]: ...

Annotated: _SpecialForm = ...
_AnnotatedAlias: Any = ... # undocumented
Annotated: _SpecialForm
_AnnotatedAlias: Any # undocumented

@runtime_checkable
class SupportsIndex(Protocol, metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -99,6 +99,6 @@ class ParamSpec:
@property
def kwargs(self) -> ParamSpecKwargs: ...

Concatenate: _SpecialForm = ...
TypeAlias: _SpecialForm = ...
TypeGuard: _SpecialForm = ...
Concatenate: _SpecialForm
TypeAlias: _SpecialForm
TypeGuard: _SpecialForm
40 changes: 20 additions & 20 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Any = object()
class TypeVar:
__name__: str
__bound__: Any | None
__constraints__: Tuple[Any, ...]
__constraints__: tuple[Any, ...]
__covariant__: bool
__contravariant__: bool
def __init__(
Expand All @@ -42,19 +42,19 @@ _T = TypeVar("_T")
def overload(func: _F) -> _F: ...

Union: _SpecialForm = ...
Optional: _SpecialForm = ...
Tuple: _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 = ...
ClassVar: _SpecialForm
NoReturn: _SpecialForm = ...
if sys.version_info >= (3, 8):
Final: _SpecialForm = ...
Final: _SpecialForm
def final(f: _T) -> _T: ...
Literal: _SpecialForm = ...
Literal: _SpecialForm
# TypedDict is a (non-subscriptable) special form.
TypedDict: object

Expand All @@ -80,9 +80,9 @@ if sys.version_info >= (3, 10):
def kwargs(self) -> ParamSpecKwargs: ...
def __or__(self, other: Any) -> _SpecialForm: ...
def __ror__(self, other: Any) -> _SpecialForm: ...
Concatenate: _SpecialForm = ...
TypeAlias: _SpecialForm = ...
TypeGuard: _SpecialForm = ...
Concatenate: _SpecialForm
TypeAlias: _SpecialForm
TypeGuard: _SpecialForm
class NewType:
def __init__(self, name: str, tp: type) -> None: ...
def __call__(self, x: _T) -> _T: ...
Expand Down Expand Up @@ -126,7 +126,7 @@ if sys.version_info >= (3, 7):
OrderedDict = _Alias()

if sys.version_info >= (3, 9):
Annotated: _SpecialForm = ...
Annotated: _SpecialForm

# Predefined type variables.
AnyStr = TypeVar("AnyStr", str, bytes) # noqa: Y001
Expand Down Expand Up @@ -393,7 +393,7 @@ class MappingView(Sized):
def __init__(self, mapping: Mapping[Any, Any]) -> None: ... # undocumented
def __len__(self) -> int: ...

class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
def __and__(self, o: Iterable[Any]) -> set[tuple[_KT_co, _VT_co]]: ...
def __rand__(self, o: Iterable[_T]) -> set[_T]: ...
Expand Down Expand Up @@ -477,9 +477,9 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
def setdefault(self, __key: _KT, __default: _VT) -> _VT: ...
# 'update' used to take a Union, but using overloading is better.
# The second overloaded type here is a bit too general, because
# Mapping[Tuple[_KT, _VT], W] is a subclass of Iterable[Tuple[_KT, _VT]],
# Mapping[tuple[_KT, _VT], W] is a subclass of Iterable[tuple[_KT, _VT]],
# but will always have the behavior of the first overloaded type
# at runtime, leading to keys of a mix of types _KT and Tuple[_KT, _VT].
# at runtime, leading to keys of a mix of types _KT and tuple[_KT, _VT].
# We don't currently have any way of forcing all Mappings to use
# the first overload, but by using overloading rather than a Union,
# mypy will commit to using the first overload when the argument is
Expand Down Expand Up @@ -595,13 +595,13 @@ class Match(Generic[AnyStr]):
@overload
def group(self, __group: str | int) -> AnyStr | Any: ...
@overload
def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> Tuple[AnyStr | Any, ...]: ...
def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[AnyStr | Any, ...]: ...
# Each item of groups()'s return tuple is either "AnyStr" or
# "AnyStr | None", depending on the pattern.
@overload
def groups(self) -> Tuple[AnyStr | Any, ...]: ...
def groups(self) -> tuple[AnyStr | Any, ...]: ...
@overload
def groups(self, default: _T) -> Tuple[AnyStr | _T, ...]: ...
def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ...
# Each value in groupdict()'s return dict is either "AnyStr" or
# "AnyStr | None", depending on the pattern.
@overload
Expand All @@ -612,7 +612,7 @@ class Match(Generic[AnyStr]):
def end(self, __group: int | str = ...) -> int: ...
def span(self, __group: int | str = ...) -> tuple[int, int]: ...
@property
def regs(self) -> Tuple[tuple[int, int], ...]: ... # undocumented
def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented
# __getitem__() returns "AnyStr" or "AnyStr | None", depending on the pattern.
@overload
def __getitem__(self, __key: _Literal[0]) -> AnyStr: ...
Expand Down Expand Up @@ -678,7 +678,7 @@ else:

if sys.version_info >= (3, 8):
def get_origin(tp: Any) -> Any | None: ...
def get_args(tp: Any) -> Tuple[Any, ...]: ...
def get_args(tp: Any) -> tuple[Any, ...]: ...

@overload
def cast(typ: Type[_T], val: Any) -> _T: ...
Expand All @@ -689,13 +689,13 @@ def cast(typ: object, val: Any) -> Any: ...

# Type constructors

class NamedTuple(Tuple[Any, ...]):
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, ...]
_fields: tuple[str, ...]
_source: str
@overload
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ...
Expand Down
Loading