From c4a96970305b9625adea6637a3d2d8a44291c45c Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 15 Apr 2022 16:45:02 -0700 Subject: [PATCH] Sync typeshed (#12596) Source commit: https://github.com/python/typeshed/commit/d09689f8115173e3b0b04365f5233164014a7a69 Co-authored-by: hauntsaninja <> --- mypy/typeshed/stdlib/_pydecimal.pyi | 82 +++++++++++++++++++ mypy/typeshed/stdlib/asyncio/streams.pyi | 4 +- mypy/typeshed/stdlib/asyncio/transports.pyi | 3 + mypy/typeshed/stdlib/base64.pyi | 41 +++++----- mypy/typeshed/stdlib/builtins.pyi | 47 ++++++++--- mypy/typeshed/stdlib/calendar.pyi | 87 +++++++++++++++------ mypy/typeshed/stdlib/datetime.pyi | 82 ++++++------------- mypy/typeshed/stdlib/linecache.pyi | 2 +- mypy/typeshed/stdlib/poplib.pyi | 6 +- mypy/typeshed/stdlib/tty.pyi | 2 + mypy/typeshed/stdlib/typing_extensions.pyi | 77 +++++++++++++++--- 11 files changed, 300 insertions(+), 133 deletions(-) diff --git a/mypy/typeshed/stdlib/_pydecimal.pyi b/mypy/typeshed/stdlib/_pydecimal.pyi index 56fbddfffa5c..c15a4a41747e 100644 --- a/mypy/typeshed/stdlib/_pydecimal.pyi +++ b/mypy/typeshed/stdlib/_pydecimal.pyi @@ -1,3 +1,85 @@ +import sys + # This is a slight lie, the implementations aren't exactly identical # However, in all likelihood, the differences are inconsequential from decimal import * + +if sys.version_info >= (3, 7): + __all__ = [ + "Decimal", + "Context", + "DecimalTuple", + "DefaultContext", + "BasicContext", + "ExtendedContext", + "DecimalException", + "Clamped", + "InvalidOperation", + "DivisionByZero", + "Inexact", + "Rounded", + "Subnormal", + "Overflow", + "Underflow", + "FloatOperation", + "DivisionImpossible", + "InvalidContext", + "ConversionSyntax", + "DivisionUndefined", + "ROUND_DOWN", + "ROUND_HALF_UP", + "ROUND_HALF_EVEN", + "ROUND_CEILING", + "ROUND_FLOOR", + "ROUND_UP", + "ROUND_HALF_DOWN", + "ROUND_05UP", + "setcontext", + "getcontext", + "localcontext", + "MAX_PREC", + "MAX_EMAX", + "MIN_EMIN", + "MIN_ETINY", + "HAVE_THREADS", + "HAVE_CONTEXTVAR", + ] +else: + __all__ = [ + "Decimal", + "Context", + "DecimalTuple", + "DefaultContext", + "BasicContext", + "ExtendedContext", + "DecimalException", + "Clamped", + "InvalidOperation", + "DivisionByZero", + "Inexact", + "Rounded", + "Subnormal", + "Overflow", + "Underflow", + "FloatOperation", + "DivisionImpossible", + "InvalidContext", + "ConversionSyntax", + "DivisionUndefined", + "ROUND_DOWN", + "ROUND_HALF_UP", + "ROUND_HALF_EVEN", + "ROUND_CEILING", + "ROUND_FLOOR", + "ROUND_UP", + "ROUND_HALF_DOWN", + "ROUND_05UP", + "setcontext", + "getcontext", + "localcontext", + "MAX_PREC", + "MAX_EMAX", + "MIN_EMIN", + "MIN_ETINY", + "HAVE_THREADS", + ] diff --git a/mypy/typeshed/stdlib/asyncio/streams.pyi b/mypy/typeshed/stdlib/asyncio/streams.pyi index 98349222d5a2..666862cc7b7d 100644 --- a/mypy/typeshed/stdlib/asyncio/streams.pyi +++ b/mypy/typeshed/stdlib/asyncio/streams.pyi @@ -159,13 +159,13 @@ class StreamReaderProtocol(FlowControlMixin, protocols.Protocol): class StreamWriter: def __init__( self, - transport: transports.BaseTransport, + transport: transports.WriteTransport, protocol: protocols.BaseProtocol, reader: StreamReader | None, loop: events.AbstractEventLoop, ) -> None: ... @property - def transport(self) -> transports.BaseTransport: ... + def transport(self) -> transports.WriteTransport: ... def write(self, data: bytes) -> None: ... def writelines(self, data: Iterable[bytes]) -> None: ... def write_eof(self) -> None: ... diff --git a/mypy/typeshed/stdlib/asyncio/transports.pyi b/mypy/typeshed/stdlib/asyncio/transports.pyi index c24ded49cfb8..90e54b547a87 100644 --- a/mypy/typeshed/stdlib/asyncio/transports.pyi +++ b/mypy/typeshed/stdlib/asyncio/transports.pyi @@ -27,6 +27,9 @@ class ReadTransport(BaseTransport): class WriteTransport(BaseTransport): def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ... def get_write_buffer_size(self) -> int: ... + if sys.version_info >= (3, 9): + def get_write_buffer_limits(self) -> tuple[int, int]: ... + def write(self, data: Any) -> None: ... def writelines(self, list_of_data: list[Any]) -> None: ... def write_eof(self) -> None: ... diff --git a/mypy/typeshed/stdlib/base64.pyi b/mypy/typeshed/stdlib/base64.pyi index 70fe64292328..ceed7d018d82 100644 --- a/mypy/typeshed/stdlib/base64.pyi +++ b/mypy/typeshed/stdlib/base64.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import ReadableBuffer from typing import IO if sys.version_info >= (3, 10): @@ -46,30 +47,30 @@ else: "urlsafe_b64decode", ] -def b64encode(s: bytes, altchars: bytes | None = ...) -> bytes: ... -def b64decode(s: str | bytes, altchars: bytes | None = ..., validate: bool = ...) -> bytes: ... -def standard_b64encode(s: bytes) -> bytes: ... -def standard_b64decode(s: str | bytes) -> bytes: ... -def urlsafe_b64encode(s: bytes) -> bytes: ... -def urlsafe_b64decode(s: str | bytes) -> bytes: ... -def b32encode(s: bytes) -> bytes: ... -def b32decode(s: str | bytes, casefold: bool = ..., map01: bytes | None = ...) -> bytes: ... -def b16encode(s: bytes) -> bytes: ... -def b16decode(s: str | bytes, casefold: bool = ...) -> bytes: ... +def b64encode(s: ReadableBuffer, altchars: ReadableBuffer | None = ...) -> bytes: ... +def b64decode(s: str | ReadableBuffer, altchars: ReadableBuffer | None = ..., validate: bool = ...) -> bytes: ... +def standard_b64encode(s: ReadableBuffer) -> bytes: ... +def standard_b64decode(s: str | ReadableBuffer) -> bytes: ... +def urlsafe_b64encode(s: ReadableBuffer) -> bytes: ... +def urlsafe_b64decode(s: str | ReadableBuffer) -> bytes: ... +def b32encode(s: ReadableBuffer) -> bytes: ... +def b32decode(s: str | ReadableBuffer, casefold: bool = ..., map01: bytes | None = ...) -> bytes: ... +def b16encode(s: ReadableBuffer) -> bytes: ... +def b16decode(s: str | ReadableBuffer, casefold: bool = ...) -> bytes: ... if sys.version_info >= (3, 10): - def b32hexencode(s: bytes) -> bytes: ... - def b32hexdecode(s: str | bytes, casefold: bool = ...) -> bytes: ... + def b32hexencode(s: ReadableBuffer) -> bytes: ... + def b32hexdecode(s: str | ReadableBuffer, casefold: bool = ...) -> bytes: ... -def a85encode(b: bytes, *, foldspaces: bool = ..., wrapcol: int = ..., pad: bool = ..., adobe: bool = ...) -> bytes: ... -def a85decode(b: str | bytes, *, foldspaces: bool = ..., adobe: bool = ..., ignorechars: str | bytes = ...) -> bytes: ... -def b85encode(b: bytes, pad: bool = ...) -> bytes: ... -def b85decode(b: str | bytes) -> bytes: ... +def a85encode(b: ReadableBuffer, *, foldspaces: bool = ..., wrapcol: int = ..., pad: bool = ..., adobe: bool = ...) -> bytes: ... +def a85decode(b: str | ReadableBuffer, *, foldspaces: bool = ..., adobe: bool = ..., ignorechars: str | bytes = ...) -> bytes: ... +def b85encode(b: ReadableBuffer, pad: bool = ...) -> bytes: ... +def b85decode(b: str | ReadableBuffer) -> bytes: ... def decode(input: IO[bytes], output: IO[bytes]) -> None: ... def encode(input: IO[bytes], output: IO[bytes]) -> None: ... -def encodebytes(s: bytes) -> bytes: ... -def decodebytes(s: bytes) -> bytes: ... +def encodebytes(s: ReadableBuffer) -> bytes: ... +def decodebytes(s: ReadableBuffer) -> bytes: ... if sys.version_info < (3, 9): - def encodestring(s: bytes) -> bytes: ... - def decodestring(s: bytes) -> bytes: ... + def encodestring(s: ReadableBuffer) -> bytes: ... + def decodestring(s: ReadableBuffer) -> bytes: ... diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi index ce2042bcac63..9c5dfcfef22f 100644 --- a/mypy/typeshed/stdlib/builtins.pyi +++ b/mypy/typeshed/stdlib/builtins.pyi @@ -52,7 +52,6 @@ from typing import ( SupportsInt, SupportsRound, TypeVar, - Union, overload, ) from typing_extensions import Literal, SupportsIndex, TypeGuard, final @@ -1747,19 +1746,45 @@ if sys.version_info >= (3, 10): class EncodingWarning(Warning): ... if sys.version_info >= (3, 11): - _SplitCondition = Union[type[BaseException], tuple[type[BaseException], ...], Callable[[BaseException], bool]] + _BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True) + _BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException) + _ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True) + _ExceptionT = TypeVar("_ExceptionT", bound=Exception) - class BaseExceptionGroup(BaseException): - def __new__(cls: type[Self], __message: str, __exceptions: Sequence[BaseException]) -> Self: ... + class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]): + def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_BaseExceptionT_co]) -> Self: ... @property def message(self) -> str: ... @property - def exceptions(self) -> tuple[BaseException, ...]: ... - def subgroup(self: Self, __condition: _SplitCondition) -> Self | None: ... - def split(self: Self, __condition: _SplitCondition) -> tuple[Self | None, Self | None]: ... - def derive(self: Self, __excs: Sequence[BaseException]) -> Self: ... + def exceptions(self) -> tuple[_BaseExceptionT_co | BaseExceptionGroup[_BaseExceptionT_co], ...]: ... + @overload + def subgroup( + self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...] + ) -> BaseExceptionGroup[_BaseExceptionT] | None: ... + @overload + def subgroup(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> Self | None: ... + @overload + def split( + self: Self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...] + ) -> tuple[BaseExceptionGroup[_BaseExceptionT] | None, Self | None]: ... + @overload + def split(self: Self, __condition: Callable[[_BaseExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ... + def derive(self: Self, __excs: Sequence[_BaseExceptionT_co]) -> Self: ... - class ExceptionGroup(BaseExceptionGroup, Exception): - def __new__(cls: type[Self], __message: str, __exceptions: Sequence[Exception]) -> Self: ... + class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception): + def __new__(cls: type[Self], __message: str, __exceptions: Sequence[_ExceptionT_co]) -> Self: ... @property - def exceptions(self) -> tuple[Exception, ...]: ... + def exceptions(self) -> tuple[_ExceptionT_co | ExceptionGroup[_ExceptionT_co], ...]: ... + # We accept a narrower type, but that's OK. + @overload # type: ignore[override] + def subgroup( + self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] + ) -> ExceptionGroup[_ExceptionT] | None: ... + @overload + def subgroup(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> Self | None: ... + @overload # type: ignore[override] + def split( + self: Self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...] + ) -> tuple[ExceptionGroup[_ExceptionT] | None, Self | None]: ... + @overload + def split(self: Self, __condition: Callable[[_ExceptionT_co], bool]) -> tuple[Self | None, Self | None]: ... diff --git a/mypy/typeshed/stdlib/calendar.pyi b/mypy/typeshed/stdlib/calendar.pyi index 05ceca4a3ae0..f106eb1213f1 100644 --- a/mypy/typeshed/stdlib/calendar.pyi +++ b/mypy/typeshed/stdlib/calendar.pyi @@ -4,32 +4,67 @@ from collections.abc import Iterable, Sequence from time import struct_time from typing_extensions import Literal -__all__ = [ - "IllegalMonthError", - "IllegalWeekdayError", - "setfirstweekday", - "firstweekday", - "isleap", - "leapdays", - "weekday", - "monthrange", - "monthcalendar", - "prmonth", - "month", - "prcal", - "calendar", - "timegm", - "month_name", - "month_abbr", - "day_name", - "day_abbr", - "Calendar", - "TextCalendar", - "HTMLCalendar", - "LocaleTextCalendar", - "LocaleHTMLCalendar", - "weekheader", -] +if sys.version_info >= (3, 10): + __all__ = [ + "IllegalMonthError", + "IllegalWeekdayError", + "setfirstweekday", + "firstweekday", + "isleap", + "leapdays", + "weekday", + "monthrange", + "monthcalendar", + "prmonth", + "month", + "prcal", + "calendar", + "timegm", + "month_name", + "month_abbr", + "day_name", + "day_abbr", + "Calendar", + "TextCalendar", + "HTMLCalendar", + "LocaleTextCalendar", + "LocaleHTMLCalendar", + "weekheader", + "FRIDAY", + "MONDAY", + "SATURDAY", + "SUNDAY", + "THURSDAY", + "TUESDAY", + "WEDNESDAY", + ] +else: + __all__ = [ + "IllegalMonthError", + "IllegalWeekdayError", + "setfirstweekday", + "firstweekday", + "isleap", + "leapdays", + "weekday", + "monthrange", + "monthcalendar", + "prmonth", + "month", + "prcal", + "calendar", + "timegm", + "month_name", + "month_abbr", + "day_name", + "day_abbr", + "Calendar", + "TextCalendar", + "HTMLCalendar", + "LocaleTextCalendar", + "LocaleHTMLCalendar", + "weekheader", + ] _LocaleType = tuple[str | None, str | None] diff --git a/mypy/typeshed/stdlib/datetime.pyi b/mypy/typeshed/stdlib/datetime.pyi index 18a4d2dd2856..220e07e25fe0 100644 --- a/mypy/typeshed/stdlib/datetime.pyi +++ b/mypy/typeshed/stdlib/datetime.pyi @@ -65,12 +65,7 @@ class date: def isoformat(self) -> str: ... def timetuple(self) -> struct_time: ... def toordinal(self) -> int: ... - if sys.version_info >= (3, 6): - def replace(self: Self, year: int = ..., month: int = ..., day: int = ...) -> Self: ... - else: - # Prior to Python 3.6, the `replace` method always returned `date`, even in subclasses - def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ... - + def replace(self: Self, year: int = ..., month: int = ..., day: int = ...) -> Self: ... def __le__(self, __other: date) -> bool: ... def __lt__(self, __other: date) -> bool: ... def __ge__(self, __other: date) -> bool: ... @@ -144,29 +139,16 @@ class time: def utcoffset(self) -> timedelta | None: ... def tzname(self) -> str | None: ... def dst(self) -> timedelta | None: ... - if sys.version_info >= (3, 6): - def replace( - self: Self, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> Self: ... - else: - # Prior to Python 3.6, the `replace` method always returned `time`, even in subclasses - def replace( - self, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> time: ... + def replace( + self: Self, + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> Self: ... _date = date _time = time @@ -278,35 +260,19 @@ class datetime(date): def date(self) -> _date: ... def time(self) -> _time: ... def timetz(self) -> _time: ... - if sys.version_info >= (3, 6): - def replace( - self: Self, - year: int = ..., - month: int = ..., - day: int = ..., - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> Self: ... - else: - # Prior to Python 3.6, the `replace` method always returned `datetime`, even in subclasses - def replace( - self, - year: int = ..., - month: int = ..., - day: int = ..., - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> datetime: ... + def replace( + self: Self, + year: int = ..., + month: int = ..., + day: int = ..., + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> Self: ... if sys.version_info >= (3, 8): def astimezone(self: Self, tz: _tzinfo | None = ...) -> Self: ... else: diff --git a/mypy/typeshed/stdlib/linecache.pyi b/mypy/typeshed/stdlib/linecache.pyi index 6b3761f4a0c9..d72d678b5e18 100644 --- a/mypy/typeshed/stdlib/linecache.pyi +++ b/mypy/typeshed/stdlib/linecache.pyi @@ -7,7 +7,7 @@ else: __all__ = ["getline", "clearcache", "checkcache"] _ModuleGlobals = dict[str, Any] -_ModuleMetadata = tuple[int, float, list[str], str] +_ModuleMetadata = tuple[int, float | None, list[str], str] class _SourceLoader(Protocol): def __call__(self) -> str | None: ... diff --git a/mypy/typeshed/stdlib/poplib.pyi b/mypy/typeshed/stdlib/poplib.pyi index 111e5c4ca76a..6b651e98e41f 100644 --- a/mypy/typeshed/stdlib/poplib.pyi +++ b/mypy/typeshed/stdlib/poplib.pyi @@ -1,13 +1,9 @@ import socket import ssl -import sys from typing import Any, BinaryIO, NoReturn, Pattern, overload from typing_extensions import Literal -if sys.version_info >= (3, 10): - __all__ = ["POP3", "error_proto", "POP3_SSL"] -else: - __all__ = ["POP3", "error_proto"] +__all__ = ["POP3", "error_proto", "POP3_SSL"] _LongResp = tuple[bytes, list[bytes], int] diff --git a/mypy/typeshed/stdlib/tty.pyi b/mypy/typeshed/stdlib/tty.pyi index 63f594a8fbb1..08c93f6f2e84 100644 --- a/mypy/typeshed/stdlib/tty.pyi +++ b/mypy/typeshed/stdlib/tty.pyi @@ -2,6 +2,8 @@ import sys from typing import IO if sys.platform != "win32": + __all__ = ["setraw", "setcbreak"] + _FD = int | IO[str] # XXX: Undocumented integer constants diff --git a/mypy/typeshed/stdlib/typing_extensions.pyi b/mypy/typeshed/stdlib/typing_extensions.pyi index 892218f68283..70f395446b0b 100644 --- a/mypy/typeshed/stdlib/typing_extensions.pyi +++ b/mypy/typeshed/stdlib/typing_extensions.pyi @@ -30,6 +30,57 @@ from typing import ( # noqa: Y022,Y027 overload as overload, ) +__all__ = [ + "ClassVar", + "Concatenate", + "Final", + "LiteralString", + "ParamSpec", + "Self", + "Type", + "TypeVarTuple", + "Unpack", + "Awaitable", + "AsyncIterator", + "AsyncIterable", + "Coroutine", + "AsyncGenerator", + "AsyncContextManager", + "ChainMap", + "ContextManager", + "Counter", + "Deque", + "DefaultDict", + "OrderedDict", + "TypedDict", + "SupportsIndex", + "Annotated", + "assert_never", + "assert_type", + "dataclass_transform", + "final", + "IntVar", + "is_typeddict", + "Literal", + "NewType", + "overload", + "Protocol", + "reveal_type", + "runtime", + "runtime_checkable", + "Text", + "TypeAlias", + "TypeGuard", + "TYPE_CHECKING", + "Never", + "NoReturn", + "Required", + "NotRequired", + "get_args", + "get_origin", + "get_type_hints", +] + _T = TypeVar("_T") _F = TypeVar("_F", bound=Callable[..., Any]) _TC = TypeVar("_TC", bound=Type[object]) @@ -82,15 +133,14 @@ TypedDict: object OrderedDict = _Alias() -if sys.version_info >= (3, 7): - def get_type_hints( - obj: Callable[..., Any], - globalns: dict[str, Any] | None = ..., - localns: dict[str, Any] | None = ..., - include_extras: bool = ..., - ) -> dict[str, Any]: ... - def get_args(tp: Any) -> tuple[Any, ...]: ... - def get_origin(tp: Any) -> Any | None: ... +def get_type_hints( + obj: Callable[..., Any], + globalns: dict[str, Any] | None = ..., + localns: dict[str, Any] | None = ..., + include_extras: bool = ..., +) -> dict[str, Any]: ... +def get_args(tp: Any) -> tuple[Any, ...]: ... +def get_origin(tp: Any) -> Any | None: ... Annotated: _SpecialForm _AnnotatedAlias: Any # undocumented @@ -137,12 +187,19 @@ else: # New things in 3.11 if sys.version_info >= (3, 11): - from typing import Never as Never, Self as Self, assert_never as assert_never, reveal_type as reveal_type + from typing import ( + Never as Never, + Self as Self, + assert_never as assert_never, + assert_type as assert_type, + reveal_type as reveal_type, + ) else: Self: _SpecialForm Never: _SpecialForm def reveal_type(__obj: _T) -> _T: ... def assert_never(__arg: NoReturn) -> NoReturn: ... + def assert_type(__val: _T, __typ: Any) -> _T: ... # Experimental (hopefully these will be in 3.11) Required: _SpecialForm