Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9d996f

Browse files
authored
Various stubtest exceptions (#5227)
1 parent 4d734e3 commit c9d996f

10 files changed

Lines changed: 108 additions & 70 deletions

File tree

stdlib/_dummy_threading.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Thread:
5454
target: Optional[Callable[..., Any]] = ...,
5555
name: Optional[str] = ...,
5656
args: Iterable[Any] = ...,
57-
kwargs: Mapping[str, Any] = ...,
57+
kwargs: Optional[Mapping[str, Any]] = ...,
5858
*,
5959
daemon: Optional[bool] = ...,
6060
) -> None: ...
@@ -65,7 +65,7 @@ class Thread:
6565
target: Optional[Callable[..., Any]] = ...,
6666
name: Optional[Text] = ...,
6767
args: Iterable[Any] = ...,
68-
kwargs: Mapping[Text, Any] = ...,
68+
kwargs: Optional[Mapping[Text, Any]] = ...,
6969
) -> None: ...
7070
def start(self) -> None: ...
7171
def run(self) -> None: ...
@@ -130,14 +130,15 @@ class Condition:
130130

131131
class Semaphore:
132132
def __init__(self, value: int = ...) -> None: ...
133-
def __enter__(self) -> bool: ...
134133
def __exit__(
135134
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
136135
) -> Optional[bool]: ...
137136
if sys.version_info >= (3,):
138-
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
137+
def acquire(self, blocking: bool = ..., timeout: Optional[float] = ...) -> bool: ...
138+
def __enter__(self, blocking: bool = ..., timeout: Optional[float] = ...) -> bool: ...
139139
else:
140140
def acquire(self, blocking: bool = ...) -> bool: ...
141+
def __enter__(self, blocking: bool = ...) -> bool: ...
141142
if sys.version_info >= (3, 9):
142143
def release(self, n: int = ...) -> None: ...
143144
else:

stdlib/threading.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Thread:
5454
target: Optional[Callable[..., Any]] = ...,
5555
name: Optional[str] = ...,
5656
args: Iterable[Any] = ...,
57-
kwargs: Mapping[str, Any] = ...,
57+
kwargs: Optional[Mapping[str, Any]] = ...,
5858
*,
5959
daemon: Optional[bool] = ...,
6060
) -> None: ...
@@ -65,7 +65,7 @@ class Thread:
6565
target: Optional[Callable[..., Any]] = ...,
6666
name: Optional[Text] = ...,
6767
args: Iterable[Any] = ...,
68-
kwargs: Mapping[Text, Any] = ...,
68+
kwargs: Optional[Mapping[Text, Any]] = ...,
6969
) -> None: ...
7070
def start(self) -> None: ...
7171
def run(self) -> None: ...
@@ -130,14 +130,15 @@ class Condition:
130130

131131
class Semaphore:
132132
def __init__(self, value: int = ...) -> None: ...
133-
def __enter__(self) -> bool: ...
134133
def __exit__(
135134
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
136135
) -> Optional[bool]: ...
137136
if sys.version_info >= (3,):
138-
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
137+
def acquire(self, blocking: bool = ..., timeout: Optional[float] = ...) -> bool: ...
138+
def __enter__(self, blocking: bool = ..., timeout: Optional[float] = ...) -> bool: ...
139139
else:
140140
def acquire(self, blocking: bool = ...) -> bool: ...
141+
def __enter__(self, blocking: bool = ...) -> bool: ...
141142
if sys.version_info >= (3, 9):
142143
def release(self, n: int = ...) -> None: ...
143144
else:

stdlib/timeit.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ else:
3939
stmt: _stmt = ..., setup: _stmt = ..., timer: _Timer = ..., repeat: int = ..., number: int = ...
4040
) -> List[float]: ...
4141

42-
def main(args: Optional[Sequence[str]]) -> None: ...
42+
_timerFunc = Callable[[], float]
43+
44+
def main(args: Optional[Sequence[str]] = ..., *, _wrap_timer: Optional[Callable[[_timerFunc], _timerFunc]] = ...) -> None: ...

stdlib/trace.pyi

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import types
22
from _typeshed import StrPath
3-
from typing import Any, Callable, Mapping, Optional, Sequence, Tuple, TypeVar, Union
3+
from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Tuple, TypeVar, Union
44

55
_T = TypeVar("_T")
66
_localtrace = Callable[[types.FrameType, str, Any], Callable[..., Any]]
7+
_fileModuleFunction = Tuple[str, Optional[str], str]
78

89
class CoverageResults:
10+
def __init__(
11+
self,
12+
counts: Optional[Dict[Tuple[str, int], int]] = ...,
13+
calledfuncs: Optional[Dict[_fileModuleFunction, int]] = ...,
14+
infile: Optional[StrPath] = ...,
15+
callers: Optional[Dict[Tuple[_fileModuleFunction, _fileModuleFunction], int]] = ...,
16+
outfile: Optional[StrPath] = ...,
17+
) -> None: ... # undocumented
918
def update(self, other: CoverageResults) -> None: ...
1019
def write_results(self, show_missing: bool = ..., summary: bool = ..., coverdir: Optional[StrPath] = ...) -> None: ...
1120
def write_results_file(
@@ -33,7 +42,7 @@ class Trace:
3342
locals: Optional[Mapping[str, Any]] = ...,
3443
) -> None: ...
3544
def runfunc(self, func: Callable[..., _T], *args: Any, **kw: Any) -> _T: ...
36-
def file_module_function_of(self, frame: types.FrameType) -> Tuple[str, Optional[str], str]: ...
45+
def file_module_function_of(self, frame: types.FrameType) -> _fileModuleFunction: ...
3746
def globaltrace_trackcallers(self, frame: types.FrameType, why: str, arg: Any) -> None: ...
3847
def globaltrace_countfuncs(self, frame: types.FrameType, why: str, arg: Any) -> None: ...
3948
def globaltrace_lt(self, frame: types.FrameType, why: str, arg: Any) -> None: ...

stdlib/traceback.pyi

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from _typeshed import SupportsWrite
33
from types import FrameType, TracebackType
4-
from typing import IO, Any, Dict, Generator, Iterable, Iterator, List, Mapping, Optional, Tuple, Type
4+
from typing import IO, Any, Dict, Generator, Iterable, Iterator, List, Mapping, Optional, Set, Tuple, Type
55

66
_PT = Tuple[str, int, str, Optional[str]]
77

@@ -90,20 +90,45 @@ if sys.version_info >= (3, 5):
9090
text: str
9191
offset: int
9292
msg: str
93-
def __init__(
94-
self,
95-
exc_type: Type[BaseException],
96-
exc_value: BaseException,
97-
exc_traceback: TracebackType,
98-
*,
99-
limit: Optional[int] = ...,
100-
lookup_lines: bool = ...,
101-
capture_locals: bool = ...,
102-
) -> None: ...
103-
@classmethod
104-
def from_exception(
105-
cls, exc: BaseException, *, limit: Optional[int] = ..., lookup_lines: bool = ..., capture_locals: bool = ...
106-
) -> TracebackException: ...
93+
if sys.version_info >= (3, 10):
94+
def __init__(
95+
self,
96+
exc_type: Type[BaseException],
97+
exc_value: BaseException,
98+
exc_traceback: TracebackType,
99+
*,
100+
limit: Optional[int] = ...,
101+
lookup_lines: bool = ...,
102+
capture_locals: bool = ...,
103+
compact: bool = ...,
104+
_seen: Optional[Set[int]] = ...,
105+
) -> None: ...
106+
@classmethod
107+
def from_exception(
108+
cls,
109+
exc: BaseException,
110+
*,
111+
limit: Optional[int] = ...,
112+
lookup_lines: bool = ...,
113+
capture_locals: bool = ...,
114+
compact: bool = ...,
115+
) -> TracebackException: ...
116+
else:
117+
def __init__(
118+
self,
119+
exc_type: Type[BaseException],
120+
exc_value: BaseException,
121+
exc_traceback: TracebackType,
122+
*,
123+
limit: Optional[int] = ...,
124+
lookup_lines: bool = ...,
125+
capture_locals: bool = ...,
126+
_seen: Optional[Set[int]] = ...,
127+
) -> None: ...
128+
@classmethod
129+
def from_exception(
130+
cls, exc: BaseException, *, limit: Optional[int] = ..., lookup_lines: bool = ..., capture_locals: bool = ...
131+
) -> TracebackException: ...
107132
def format(self, *, chain: bool = ...) -> Generator[str, None, None]: ...
108133
def format_exception_only(self) -> Generator[str, None, None]: ...
109134
class FrameSummary(Iterable[Any]):
@@ -117,6 +142,7 @@ if sys.version_info >= (3, 5):
117142
filename: str,
118143
lineno: int,
119144
name: str,
145+
*,
120146
lookup_line: bool = ...,
121147
locals: Optional[Mapping[str, str]] = ...,
122148
line: Optional[str] = ...,

stdlib/types.pyi

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,36 +273,42 @@ class FrameType:
273273
class GetSetDescriptorType:
274274
__name__: str
275275
__objclass__: type
276-
def __get__(self, obj: Any, type: type = ...) -> Any: ...
277-
def __set__(self, obj: Any) -> None: ...
276+
def __get__(self, __obj: Any, __type: type = ...) -> Any: ...
277+
def __set__(self, __instance: Any, __value: Any) -> None: ...
278278
def __delete__(self, obj: Any) -> None: ...
279279

280280
class MemberDescriptorType:
281281
__name__: str
282282
__objclass__: type
283-
def __get__(self, obj: Any, type: type = ...) -> Any: ...
284-
def __set__(self, obj: Any) -> None: ...
283+
def __get__(self, __obj: Any, __type: type = ...) -> Any: ...
284+
def __set__(self, __instance: Any, __value: Any) -> None: ...
285285
def __delete__(self, obj: Any) -> None: ...
286286

287287
if sys.version_info >= (3, 7):
288288
def new_class(
289-
name: str, bases: Iterable[object] = ..., kwds: Dict[str, Any] = ..., exec_body: Callable[[Dict[str, Any]], None] = ...
289+
name: str,
290+
bases: Iterable[object] = ...,
291+
kwds: Optional[Dict[str, Any]] = ...,
292+
exec_body: Optional[Callable[[Dict[str, Any]], None]] = ...,
290293
) -> type: ...
291294
def resolve_bases(bases: Iterable[object]) -> Tuple[Any, ...]: ...
292295

293296
else:
294297
def new_class(
295-
name: str, bases: Tuple[type, ...] = ..., kwds: Dict[str, Any] = ..., exec_body: Callable[[Dict[str, Any]], None] = ...
298+
name: str,
299+
bases: Tuple[type, ...] = ...,
300+
kwds: Optional[Dict[str, Any]] = ...,
301+
exec_body: Optional[Callable[[Dict[str, Any]], None]] = ...,
296302
) -> type: ...
297303

298304
def prepare_class(
299-
name: str, bases: Tuple[type, ...] = ..., kwds: Dict[str, Any] = ...
305+
name: str, bases: Tuple[type, ...] = ..., kwds: Optional[Dict[str, Any]] = ...
300306
) -> Tuple[type, Dict[str, Any], Dict[str, Any]]: ...
301307

302308
# Actually a different type, but `property` is special and we want that too.
303309
DynamicClassAttribute = property
304310

305-
def coroutine(f: Callable[..., Any]) -> CoroutineType: ...
311+
def coroutine(func: Callable[..., Any]) -> CoroutineType: ...
306312

307313
if sys.version_info >= (3, 9):
308314
class GenericAlias:

stdlib/urllib/error.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from typing import IO, Mapping, Optional, Union
1+
from email.message import Message
2+
from typing import IO, Mapping, Optional, Tuple, Union
23
from urllib.response import addinfourl
34

45
# Stubs for urllib.error
56

67
class URLError(IOError):
78
reason: Union[str, BaseException]
9+
def __init__(self, reason: Union[str, BaseException], filename: Optional[str] = ...) -> None: ...
810

911
class HTTPError(URLError, addinfourl):
1012
code: int
1113
def __init__(self, url: str, code: int, msg: str, hdrs: Mapping[str, str], fp: Optional[IO[bytes]]) -> None: ...
1214

13-
class ContentTooShortError(URLError): ...
15+
class ContentTooShortError(URLError):
16+
content: Tuple[str, Message]
17+
def __init__(self, message: str, content: Tuple[str, Message]) -> None: ...

stdlib/zipfile.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class ZipExtFile(io.BufferedIOBase):
5959
decrypter: Optional[Callable[[Sequence[int]], bytes]] = ...,
6060
close_fileobj: bool = ...,
6161
) -> None: ...
62+
def read(self, n: Optional[int] = ...) -> bytes: ...
63+
def readline(self, limit: int = ...) -> bytes: ... # type: ignore
6264
def __repr__(self) -> str: ...
6365
def peek(self, n: int = ...) -> bytes: ...
6466
def read1(self, n: Optional[int]) -> bytes: ... # type: ignore

stdlib/zlib.pyi

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from array import array
3-
from typing import Any, Union
3+
from typing import Any, Optional, Union
44

55
DEFLATED: int
66
DEF_MEM_LEVEL: int
@@ -43,7 +43,12 @@ def compress(__data: bytes, level: int = ...) -> bytes: ...
4343

4444
if sys.version_info >= (3,):
4545
def compressobj(
46-
level: int = ..., method: int = ..., wbits: int = ..., memLevel: int = ..., strategy: int = ..., zdict: bytes = ...
46+
level: int = ...,
47+
method: int = ...,
48+
wbits: int = ...,
49+
memLevel: int = ...,
50+
strategy: int = ...,
51+
zdict: Optional[bytes] = ...,
4752
) -> _Compress: ...
4853

4954
else:

tests/stubtest_whitelists/py3_common.txt

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -251,39 +251,24 @@ tempfile.SpooledTemporaryFile.__next__
251251
tempfile.SpooledTemporaryFile.readable
252252
tempfile.SpooledTemporaryFile.seekable
253253
tempfile.SpooledTemporaryFile.writable
254-
threading.Condition.acquire
255-
threading.Condition.release
256-
threading.Lock
257-
threading.Semaphore.__enter__
258-
threading.Semaphore.acquire
259-
threading.Thread.__init__
260-
timeit.main
261-
tkinter.Misc.grid_propagate
262-
tkinter.Misc.pack_propagate
254+
threading.Condition.acquire # Condition functions are exported in __init__
255+
threading.Condition.release # Condition functions are exported in __init__
256+
threading.Lock # A factory function that returns 'most efficient lock'. Marking it as a function will make it harder for users to mark the Lock type.
257+
tkinter.Misc.grid_propagate # The noarg placeholder is a set value list
258+
tkinter.Misc.pack_propagate # The noarg placeholder is a set value list
263259
tkinter.Tk.eval # from __getattr__
264260
tkinter.Tk.report_callback_exception # A bit of a lie, since it's actually a method, but typing it as an attribute allows it to be assigned to
265261
tkinter.Wm.wm_iconphoto # Default value of argument can't be used without runtime error
266262
tkinter.font.Font.__getitem__ # Argument name differs (doesn't matter for __dunder__ methods)
267-
trace.CoverageResults.__init__
268-
traceback.FrameSummary.__init__
269-
traceback.TracebackException.__init__
270-
traceback.TracebackException.from_exception
271-
types.GetSetDescriptorType.__get__
272-
types.GetSetDescriptorType.__set__
273-
types.MemberDescriptorType.__get__
274-
types.MemberDescriptorType.__set__
275-
types.SimpleNamespace.__init__
276-
types.coroutine
277-
types.new_class
278-
types.prepare_class
279-
typing.AwaitableGenerator
280-
typing.IO.__iter__
281-
typing.IO.__next__
282-
typing.type_check_only
263+
traceback.TracebackException.from_exception # explicitly expanding arguemnts going into TracebackException __init__
264+
types.GetSetDescriptorType.__get__ # this function can accept no value for the type parameter.
265+
types.MemberDescriptorType.__get__ # this function can accept no value for the type parameter.
266+
types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature
267+
typing.IO.__iter__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
268+
typing.IO.__next__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
269+
typing.type_check_only # typing decorator that is not available at runtime
283270
unittest.mock.patch # It's a complicated overload and I haven't been able to figure out why stubtest doesn't like it
284-
urllib.error.ContentTooShortError.__init__
285-
urllib.error.URLError.__init__
286-
urllib.parse._DefragResultBase.__new__
271+
urllib.parse._DefragResultBase.__new__ # Generic NamedTuple is problematic in mypy, so regular tuple was used. See https://github.com/python/mypy/issues/685
287272
# The following methods were changed in point releases from Python 3.6 to 3.9
288273
# as part of a security fix. These excludes can be removed when the GitHub
289274
# action workflow uses Python versions that include the fix (adding a
@@ -293,7 +278,7 @@ urllib.parse.parse_qsl
293278
urllib.request.BaseHandler.http_error_nnn
294279
urllib.request.HTTPPasswordMgrWithPriorAuth.__init__
295280
urllib.robotparser.RobotFileParser.can_fetch
296-
warnings.catch_warnings.__init__
281+
warnings.catch_warnings.__init__ # Defining this ruins the __new__ overrides
297282
weakref.CallableProxyType.__getattr__
298283
weakref.ProxyType.__getattr__
299284
weakref.ReferenceType.__call__
@@ -306,9 +291,6 @@ webbrowser.UnixBrowser.remote_action
306291
webbrowser.UnixBrowser.remote_action_newtab
307292
webbrowser.UnixBrowser.remote_action_newwin
308293
wsgiref.types # Doesn't exist, see comments in file
309-
zipfile.ZipExtFile.read
310-
zipfile.ZipExtFile.readline
311-
zlib.compressobj
312294

313295
# These enums derive from (int, IntEnum) or (str, Enum). Strangely,
314296
# at runtime, they inherit Enum.__new__, not int.__new__ or

0 commit comments

Comments
 (0)