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

Sync typeshed #14902

Merged
merged 6 commits into from
Mar 15, 2023
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
1 change: 1 addition & 0 deletions mypy/typeshed/stdlib/VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ importlib: 2.7-
importlib.metadata: 3.8-
importlib.metadata._meta: 3.10-
importlib.resources: 3.7-
importlib.resources.abc: 3.11-
inspect: 2.7-
io: 2.7-
ipaddress: 3.3-
Expand Down
13 changes: 7 additions & 6 deletions mypy/typeshed/stdlib/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import _typeshed
import sys
from _typeshed import SupportsWrite
from collections.abc import Callable
from typing import Any, Generic, TypeVar
from typing_extensions import Literal
from typing import Any, TypeVar
from typing_extensions import Concatenate, Literal, ParamSpec

_T = TypeVar("_T")
_R_co = TypeVar("_R_co", covariant=True)
_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
_P = ParamSpec("_P")

# These definitions have special processing in mypy
class ABCMeta(type):
Expand All @@ -28,13 +29,13 @@ class ABCMeta(type):

def abstractmethod(funcobj: _FuncT) -> _FuncT: ...

class abstractclassmethod(classmethod[_R_co], Generic[_R_co]):
class abstractclassmethod(classmethod[_T, _P, _R_co]):
__isabstractmethod__: Literal[True]
def __init__(self: abstractclassmethod[_R_co], callable: Callable[..., _R_co]) -> None: ...
def __init__(self, callable: Callable[Concatenate[_T, _P], _R_co]) -> None: ...

class abstractstaticmethod(staticmethod[_R_co], Generic[_R_co]):
class abstractstaticmethod(staticmethod[_P, _R_co]):
__isabstractmethod__: Literal[True]
def __init__(self, callable: Callable[..., _R_co]) -> None: ...
def __init__(self, callable: Callable[_P, _R_co]) -> None: ...

class abstractproperty(property):
__isabstractmethod__: Literal[True]
Expand Down
25 changes: 13 additions & 12 deletions mypy/typeshed/stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ from typing import ( # noqa: Y022
overload,
type_check_only,
)
from typing_extensions import Literal, Self, SupportsIndex, TypeAlias, TypeGuard, final
from typing_extensions import Concatenate, Literal, ParamSpec, Self, SupportsIndex, TypeAlias, TypeGuard, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -75,6 +75,7 @@ _SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=Tr
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
_P = ParamSpec("_P")

class object:
__doc__: str | None
Expand Down Expand Up @@ -113,32 +114,32 @@ class object:
@classmethod
def __subclasshook__(cls, __subclass: type) -> bool: ...

class staticmethod(Generic[_R_co]):
class staticmethod(Generic[_P, _R_co]):
@property
def __func__(self) -> Callable[..., _R_co]: ...
def __func__(self) -> Callable[_P, _R_co]: ...
@property
def __isabstractmethod__(self) -> bool: ...
def __init__(self: staticmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ...
def __init__(self, __f: Callable[_P, _R_co]) -> None: ...
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ...
if sys.version_info >= (3, 10):
__name__: str
__qualname__: str
@property
def __wrapped__(self) -> Callable[..., _R_co]: ...
def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ...
def __wrapped__(self) -> Callable[_P, _R_co]: ...
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ...

class classmethod(Generic[_R_co]):
class classmethod(Generic[_T, _P, _R_co]):
@property
def __func__(self) -> Callable[..., _R_co]: ...
def __func__(self) -> Callable[Concatenate[_T, _P], _R_co]: ...
@property
def __isabstractmethod__(self) -> bool: ...
def __init__(self: classmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ...
def __init__(self, __f: Callable[Concatenate[_T, _P], _R_co]) -> None: ...
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ...
if sys.version_info >= (3, 10):
__name__: str
__qualname__: str
@property
def __wrapped__(self) -> Callable[..., _R_co]: ...
def __wrapped__(self) -> Callable[Concatenate[_T, _P], _R_co]: ...

class type:
@property
Expand Down
3 changes: 2 additions & 1 deletion mypy/typeshed/stdlib/codecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ class StreamRecoder(BinaryIO):
def readlines(self, sizehint: int | None = None) -> list[bytes]: ...
def __next__(self) -> bytes: ...
def __iter__(self) -> Self: ...
# Base class accepts more types than just bytes
def write(self, data: bytes) -> None: ... # type: ignore[override]
def writelines(self, list: Iterable[bytes]) -> None: ...
def writelines(self, list: Iterable[bytes]) -> None: ... # type: ignore[override]
def reset(self) -> None: ...
def __getattr__(self, name: str) -> Any: ...
def __enter__(self) -> Self: ...
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/http/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class HTTPMessage(email.message.Message):

def parse_headers(fp: io.BufferedIOBase, _class: Callable[[], email.message.Message] = ...) -> HTTPMessage: ...

class HTTPResponse(io.BufferedIOBase, BinaryIO):
class HTTPResponse(io.BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible method definitions in the base classes
msg: HTTPMessage
headers: HTTPMessage
version: int
Expand Down
6 changes: 5 additions & 1 deletion mypy/typeshed/stdlib/importlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ast
import sys
import types
from _typeshed import (
Expand All @@ -7,6 +8,7 @@ from _typeshed import (
OpenBinaryModeWriting,
OpenTextMode,
ReadableBuffer,
StrPath,
)
from abc import ABCMeta, abstractmethod
from collections.abc import Iterator, Mapping, Sequence
Expand Down Expand Up @@ -52,7 +54,9 @@ class InspectLoader(Loader):
def get_source(self, fullname: str) -> str | None: ...
def exec_module(self, module: types.ModuleType) -> None: ...
@staticmethod
def source_to_code(data: ReadableBuffer | str, path: str = "<string>") -> types.CodeType: ...
def source_to_code(
data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: ReadableBuffer | StrPath = "<string>"
) -> types.CodeType: ...

class ExecutionLoader(InspectLoader):
@abstractmethod
Expand Down
12 changes: 12 additions & 0 deletions mypy/typeshed/stdlib/importlib/resources/abc.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys

if sys.version_info >= (3, 11):
# These are all actually defined in this file on 3.11+,
# and re-exported from importlib.abc,
# but it's much less code duplication for typeshed if we pretend that they're still defined
# in importlib.abc on 3.11+, and re-exported from this file
from importlib.abc import (
ResourceReader as ResourceReader,
Traversable as Traversable,
TraversableResources as TraversableResources,
)
12 changes: 6 additions & 6 deletions mypy/typeshed/stdlib/io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BufferedIOBase(IOBase):
def read(self, __size: int | None = ...) -> bytes: ...
def read1(self, __size: int = ...) -> bytes: ...

class FileIO(RawIOBase, BinaryIO):
class FileIO(RawIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
mode: str
name: FileDescriptorOrPath # type: ignore[assignment]
def __init__(
Expand All @@ -102,7 +102,7 @@ class FileIO(RawIOBase, BinaryIO):
def read(self, __size: int = -1) -> bytes: ...
def __enter__(self) -> Self: ...

class BytesIO(BufferedIOBase, BinaryIO):
class BytesIO(BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
def __init__(self, initial_bytes: ReadableBuffer = ...) -> None: ...
# BytesIO does not contain a "name" field. This workaround is necessary
# to allow BytesIO sub-classes to add this field, as it is defined
Expand All @@ -113,17 +113,17 @@ class BytesIO(BufferedIOBase, BinaryIO):
def getbuffer(self) -> memoryview: ...
def read1(self, __size: int | None = -1) -> bytes: ...

class BufferedReader(BufferedIOBase, BinaryIO):
class BufferedReader(BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
def __enter__(self) -> Self: ...
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
def peek(self, __size: int = 0) -> bytes: ...

class BufferedWriter(BufferedIOBase, BinaryIO):
class BufferedWriter(BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
def __enter__(self) -> Self: ...
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
def write(self, __buffer: ReadableBuffer) -> int: ...

class BufferedRandom(BufferedReader, BufferedWriter):
class BufferedRandom(BufferedReader, BufferedWriter): # type: ignore[misc] # incompatible definitions of methods in the base classes
def __enter__(self) -> Self: ...
def seek(self, __target: int, __whence: int = 0) -> int: ... # stubtest needs this

Expand All @@ -144,7 +144,7 @@ class TextIOBase(IOBase):
def readlines(self, __hint: int = -1) -> list[str]: ... # type: ignore[override]
def read(self, __size: int | None = ...) -> str: ...

class TextIOWrapper(TextIOBase, TextIO):
class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes
def __init__(
self,
buffer: IO[bytes],
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/lzma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class LZMACompressor:

class LZMAError(Exception): ...

class LZMAFile(io.BufferedIOBase, IO[bytes]):
class LZMAFile(io.BufferedIOBase, IO[bytes]): # type: ignore[misc] # incompatible definitions of writelines in the base classes
def __init__(
self,
filename: _PathOrFile | None = None,
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/pydoc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ErrorDuringImport(Exception):
def __init__(self, filename: str, exc_info: OptExcInfo) -> None: ...

def importfile(path: str) -> ModuleType: ...
def safeimport(path: str, forceload: bool = ..., cache: MutableMapping[str, ModuleType] = ...) -> ModuleType: ...
def safeimport(path: str, forceload: bool = ..., cache: MutableMapping[str, ModuleType] = ...) -> ModuleType | None: ...

class Doc:
PYTHONDOCS: str
Expand Down
4 changes: 4 additions & 0 deletions mypy/typeshed/stdlib/signal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Signals(IntEnum):
SIGPWR: int
SIGRTMAX: int
SIGRTMIN: int
if sys.version_info >= (3, 11):
SIGSTKFLT: int

class Handlers(IntEnum):
SIG_DFL: int
Expand Down Expand Up @@ -147,6 +149,8 @@ else:
SIGPWR: Signals
SIGRTMAX: Signals
SIGRTMIN: Signals
if sys.version_info >= (3, 11):
SIGSTKFLT: Signals
@final
class struct_siginfo(structseq[int], tuple[int, int, int, int, int, int, int]):
if sys.version_info >= (3, 10):
Expand Down
24 changes: 22 additions & 2 deletions mypy/typeshed/stdlib/tempfile.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
import sys
from _typeshed import BytesPath, GenericPath, StrPath, WriteableBuffer
from _typeshed import BytesPath, GenericPath, ReadableBuffer, StrPath, WriteableBuffer
from collections.abc import Iterable, Iterator
from types import TracebackType
from typing import IO, Any, AnyStr, Generic, overload
Expand Down Expand Up @@ -215,7 +215,17 @@ class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
def tell(self) -> int: ...
def truncate(self, size: int | None = ...) -> int: ...
def writable(self) -> bool: ...
@overload
def write(self: _TemporaryFileWrapper[str], s: str) -> int: ...
@overload
def write(self: _TemporaryFileWrapper[bytes], s: ReadableBuffer) -> int: ...
@overload
def write(self, s: AnyStr) -> int: ...
@overload
def writelines(self: _TemporaryFileWrapper[str], lines: Iterable[str]) -> None: ...
@overload
def writelines(self: _TemporaryFileWrapper[bytes], lines: Iterable[ReadableBuffer]) -> None: ...
@overload
def writelines(self, lines: Iterable[AnyStr]) -> None: ...

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -392,8 +402,18 @@ class SpooledTemporaryFile(IO[AnyStr], _SpooledTemporaryFileBase):
def seek(self, offset: int, whence: int = ...) -> int: ...
def tell(self) -> int: ...
def truncate(self, size: int | None = None) -> None: ... # type: ignore[override]
@overload
def write(self: SpooledTemporaryFile[str], s: str) -> int: ...
@overload
def write(self: SpooledTemporaryFile[bytes], s: ReadableBuffer) -> int: ...
@overload
def write(self, s: AnyStr) -> int: ...
def writelines(self, iterable: Iterable[AnyStr]) -> None: ... # type: ignore[override]
@overload
def writelines(self: SpooledTemporaryFile[str], iterable: Iterable[str]) -> None: ...
@overload
def writelines(self: SpooledTemporaryFile[bytes], iterable: Iterable[ReadableBuffer]) -> None: ...
@overload
def writelines(self, iterable: Iterable[AnyStr]) -> None: ...
def __iter__(self) -> Iterator[AnyStr]: ... # type: ignore[override]
# These exist at runtime only on 3.11+.
def readable(self) -> bool: ...
Expand Down
16 changes: 15 additions & 1 deletion mypy/typeshed/stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986
import sys
import typing_extensions
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import IdentityFunction, Incomplete, SupportsKeysAndGetItem
from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem
from abc import ABCMeta, abstractmethod
from contextlib import AbstractAsyncContextManager, AbstractContextManager
from re import Match as Match, Pattern as Pattern
Expand Down Expand Up @@ -687,8 +687,22 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
@abstractmethod
def writable(self) -> bool: ...
@abstractmethod
@overload
def write(self: IO[str], __s: str) -> int: ...
@abstractmethod
@overload
def write(self: IO[bytes], __s: ReadableBuffer) -> int: ...
@abstractmethod
@overload
def write(self, __s: AnyStr) -> int: ...
@abstractmethod
@overload
def writelines(self: IO[str], __lines: Iterable[str]) -> None: ...
@abstractmethod
@overload
def writelines(self: IO[bytes], __lines: Iterable[ReadableBuffer]) -> None: ...
@abstractmethod
@overload
def writelines(self, __lines: Iterable[AnyStr]) -> None: ...
@abstractmethod
def __next__(self) -> AnyStr: ...
Expand Down
2 changes: 2 additions & 0 deletions mypy/typeshed/stdlib/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ __all__ = [
"assert_never",
"assert_type",
"dataclass_transform",
"deprecated",
"final",
"IntVar",
"is_typeddict",
Expand Down Expand Up @@ -326,3 +327,4 @@ class TypeVarTuple:
def __iter__(self) -> Any: ... # Unpack[Self]

def override(__arg: _F) -> _F: ...
def deprecated(__msg: str, *, category: type[Warning] | None = ..., stacklevel: int = 1) -> Callable[[_T], _T]: ...
4 changes: 3 additions & 1 deletion test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ def bin(f: IO[bytes]) -> None:
txt(sys.stdout)
bin(sys.stdout)
[out]
_program.py:5: error: Argument 1 to "write" of "IO" has incompatible type "bytes"; expected "str"
_program.py:5: error: No overload variant of "write" of "IO" matches argument type "bytes"
_program.py:5: note: Possible overload variants:
_program.py:5: note: def write(self, str, /) -> int
Comment on lines -271 to +273
Copy link
Member Author

@AlexWaygood AlexWaygood Mar 15, 2023

Choose a reason for hiding this comment

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

The change in error message here is slightly unfortunate; the previous error message was better. It's due to a combination of python/typeshed#9861, which made IO.write an overloaded method, and #14882, which means that mypy now correctly deduces that only one overload applies in this specific situation.

_program.py:10: error: Argument 1 to "bin" has incompatible type "TextIO"; expected "IO[bytes]"

[case testBuiltinOpen]
Expand Down