Skip to content

Commit bf201c8

Browse files
authored
Various stubtest fixes (#5215)
1 parent 3536e2a commit bf201c8

File tree

8 files changed

+78
-42
lines changed

8 files changed

+78
-42
lines changed

stdlib/netrc.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
from _typeshed import AnyPath
12
from typing import Dict, List, Optional, Tuple
23

34
class NetrcParseError(Exception):
45
filename: Optional[str]
56
lineno: Optional[int]
67
msg: str
8+
def __init__(self, msg: str, filename: Optional[AnyPath] = ..., lineno: Optional[int] = ...) -> None: ...
79

810
# (login, account, password) tuple
911
_NetrcTuple = Tuple[str, Optional[str], Optional[str]]
1012

1113
class netrc:
1214
hosts: Dict[str, _NetrcTuple]
1315
macros: Dict[str, List[str]]
14-
def __init__(self, file: str = ...) -> None: ...
16+
def __init__(self, file: Optional[AnyPath] = ...) -> None: ...
1517
def authenticators(self, host: str) -> Optional[_NetrcTuple]: ...

stdlib/ntpath.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ def isdir(s: AnyPath) -> bool: ...
8686
def islink(path: AnyPath) -> bool: ...
8787
def ismount(path: AnyPath) -> bool: ...
8888
@overload
89-
def join(a: StrPath, *paths: StrPath) -> str: ...
89+
def join(path: StrPath, *paths: StrPath) -> str: ...
9090
@overload
91-
def join(a: BytesPath, *paths: BytesPath) -> bytes: ...
91+
def join(path: BytesPath, *paths: BytesPath) -> bytes: ...
9292
@overload
9393
def relpath(path: BytesPath, start: Optional[BytesPath] = ...) -> bytes: ...
9494
@overload

stdlib/operator.pyi

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sys
22
from typing import (
33
Any,
4-
Callable,
54
Container,
5+
Generic,
66
Mapping,
77
MutableMapping,
88
MutableSequence,
@@ -14,6 +14,7 @@ from typing import (
1414
)
1515

1616
_T = TypeVar("_T")
17+
_T_co = TypeVar("_T_co", covariant=True)
1718
_K = TypeVar("_K")
1819
_V = TypeVar("_V")
1920

@@ -148,15 +149,36 @@ if sys.version_info < (3,):
148149
if sys.version_info >= (3, 4):
149150
def length_hint(__obj: Any, __default: int = ...) -> int: ...
150151

151-
@overload
152-
def attrgetter(attr: str) -> Callable[[Any], Any]: ...
153-
@overload
154-
def attrgetter(*attrs: str) -> Callable[[Any], Tuple[Any, ...]]: ...
155-
@overload
156-
def itemgetter(item: Any) -> Callable[[Any], Any]: ...
157-
@overload
158-
def itemgetter(*items: Any) -> Callable[[Any], Tuple[Any, ...]]: ...
159-
def methodcaller(__name: str, *args: Any, **kwargs: Any) -> Callable[..., Any]: ...
152+
class attrgetter(Generic[_T_co]):
153+
@overload
154+
def __new__(cls, attr: str) -> attrgetter[Any]: ...
155+
@overload
156+
def __new__(cls, attr: str, __attr2: str) -> attrgetter[Tuple[Any, Any]]: ...
157+
@overload
158+
def __new__(cls, attr: str, __attr2: str, __attr3: str) -> attrgetter[Tuple[Any, Any, Any]]: ...
159+
@overload
160+
def __new__(cls, attr: str, __attr2: str, __attr3: str, __attr4: str) -> attrgetter[Tuple[Any, Any, Any, Any]]: ...
161+
@overload
162+
def __new__(cls, attr: str, *attrs: str) -> attrgetter[Tuple[Any, ...]]: ...
163+
def __call__(self, obj: Any) -> _T_co: ...
164+
165+
class itemgetter(Generic[_T_co]):
166+
@overload
167+
def __new__(cls, item: Any) -> itemgetter[Any]: ...
168+
@overload
169+
def __new__(cls, item: Any, __item2: Any) -> itemgetter[Tuple[Any, Any]]: ...
170+
@overload
171+
def __new__(cls, item: Any, __item2: Any, __item3: Any) -> itemgetter[Tuple[Any, Any, Any]]: ...
172+
@overload
173+
def __new__(cls, item: Any, __item2: Any, __item3: Any, __item4: Any) -> itemgetter[Tuple[Any, Any, Any, Any]]: ...
174+
@overload
175+
def __new__(cls, item: Any, *items: Any) -> itemgetter[Tuple[Any, ...]]: ...
176+
def __call__(self, obj: Any) -> _T_co: ...
177+
178+
class methodcaller:
179+
def __init__(self, __name: str, *args: Any, **kwargs: Any) -> None: ...
180+
def __call__(self, obj: Any) -> Any: ...
181+
160182
def iadd(__a: Any, __b: Any) -> Any: ...
161183
def __iadd__(a: Any, b: Any) -> Any: ...
162184
def iand(__a: Any, __b: Any) -> Any: ...

stdlib/optparse.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class HelpFormatter:
5353
short_first: Any
5454
width: int
5555
def __init__(self, indent_increment: int, max_help_position: int, width: Optional[int], short_first: int) -> None: ...
56-
def _format__Text(self, _Text: _Text) -> _Text: ...
5756
def dedent(self) -> None: ...
5857
def expand_default(self, option: Option) -> _Text: ...
5958
def format_description(self, description: _Text) -> _Text: ...
@@ -165,8 +164,8 @@ class Values:
165164
def _update_careful(self, dict: Mapping[_Text, Any]) -> None: ...
166165
def _update_loose(self, dict: Mapping[_Text, Any]) -> None: ...
167166
def ensure_value(self, attr: _Text, value: Any) -> Any: ...
168-
def read_file(self, filename: _Text, mode: _Text) -> None: ...
169-
def read_module(self, modname: _Text, mode: _Text) -> None: ...
167+
def read_file(self, filename: _Text, mode: _Text = ...) -> None: ...
168+
def read_module(self, modname: _Text, mode: _Text = ...) -> None: ...
170169
def __getattr__(self, name: str) -> Any: ...
171170
def __setattr__(self, name: str, value: Any) -> None: ...
172171

@@ -187,7 +186,7 @@ class OptionParser(OptionContainer):
187186
def __init__(
188187
self,
189188
usage: Optional[_Text] = ...,
190-
option_list: Iterable[Option] = ...,
189+
option_list: Optional[Iterable[Option]] = ...,
191190
option_class: Type[Option] = ...,
192191
version: Optional[_Text] = ...,
193192
conflict_handler: _Text = ...,

stdlib/os/__init__.pyi

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from _typeshed import (
1111
from builtins import OSError, _PathLike
1212
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper as _TextIOWrapper
1313
from posix import listdir as listdir, times_result
14+
from subprocess import Popen
1415
from typing import (
1516
IO,
1617
Any,
@@ -176,7 +177,36 @@ R_OK: int
176177
W_OK: int
177178
X_OK: int
178179

180+
_EnvironCodeFunc = Callable[[AnyStr], AnyStr]
181+
179182
class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]):
183+
encodekey: _EnvironCodeFunc[AnyStr]
184+
decodekey: _EnvironCodeFunc[AnyStr]
185+
encodevalue: _EnvironCodeFunc[AnyStr]
186+
decodevalue: _EnvironCodeFunc[AnyStr]
187+
if sys.version_info >= (3, 9):
188+
def __init__(
189+
self,
190+
data: MutableMapping[AnyStr, AnyStr],
191+
encodekey: _EnvironCodeFunc[AnyStr],
192+
decodekey: _EnvironCodeFunc[AnyStr],
193+
encodevalue: _EnvironCodeFunc[AnyStr],
194+
decodevalue: _EnvironCodeFunc[AnyStr],
195+
) -> None: ...
196+
else:
197+
putenv: Callable[[AnyStr, AnyStr], None]
198+
unsetenv: Callable[[AnyStr, AnyStr], None]
199+
def __init__(
200+
self,
201+
data: MutableMapping[AnyStr, AnyStr],
202+
encodekey: _EnvironCodeFunc[AnyStr],
203+
decodekey: _EnvironCodeFunc[AnyStr],
204+
encodevalue: _EnvironCodeFunc[AnyStr],
205+
decodevalue: _EnvironCodeFunc[AnyStr],
206+
putenv: Callable[[AnyStr, AnyStr], None],
207+
unsetenv: Callable[[AnyStr, AnyStr], None],
208+
) -> None: ...
209+
def setdefault(self, key: AnyStr, value: AnyStr) -> AnyStr: ... # type: ignore
180210
def copy(self) -> Dict[AnyStr, AnyStr]: ...
181211
def __delitem__(self, key: AnyStr) -> None: ...
182212
def __getitem__(self, key: AnyStr) -> AnyStr: ...
@@ -726,6 +756,7 @@ if sys.platform != "win32":
726756
def plock(op: int) -> None: ... # ???op is int?
727757

728758
class _wrap_close(_TextIOWrapper):
759+
def __init__(self, stream: _TextIOWrapper, proc: Popen[str]) -> None: ...
729760
def close(self) -> Optional[int]: ... # type: ignore
730761

731762
def popen(cmd: str, mode: str = ..., buffering: int = ...) -> _wrap_close: ...

stdlib/pipes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Template:
88
def append(self, cmd: str, kind: str) -> None: ...
99
def prepend(self, cmd: str, kind: str) -> None: ...
1010
def open(self, file: str, rw: str) -> os._wrap_close: ...
11-
def copy(self, file: str, rw: str) -> os._wrap_close: ...
11+
def copy(self, infile: str, outfile: str) -> int: ...
1212

1313
# Not documented, but widely used.
1414
# Documented as shlex.quote since 3.3.

stdlib/pkgutil.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ else:
2121
def extend_path(path: Iterable[str], name: str) -> Iterable[str]: ...
2222

2323
class ImpImporter:
24-
def __init__(self, dirname: Optional[str] = ...) -> None: ...
24+
def __init__(self, path: Optional[str] = ...) -> None: ...
2525

2626
class ImpLoader:
2727
def __init__(self, fullname: str, file: IO[str], filename: str, etc: Tuple[str, str, int]) -> None: ...

tests/stubtest_whitelists/py3_common.txt

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ _collections_abc.Set.isdisjoint
2424
_csv.Dialect.__init__
2525
_dummy_threading
2626
_importlib_modulespec
27-
_operator.attrgetter # not a function at runtime
28-
_operator.itemgetter # not a function at runtime
29-
_operator.methodcaller
3027
_threading_local.local.__new__
3128
_typeshed.* # Utility types for typeshed, doesn't exist at runtime
3229
_weakref.CallableProxyType.__getattr__
@@ -225,26 +222,11 @@ multiprocessing.synchronize.Event.__init__
225222
multiprocessing.synchronize.SemLock.__init__
226223
multiprocessing.synchronize.SemLock.acquire
227224
multiprocessing.synchronize.SemLock.release
228-
netrc.NetrcParseError.__init__
229-
netrc.netrc.__init__
230-
ntpath.join
231-
numbers.Number.__hash__
232-
operator.attrgetter # not a function at runtime
233-
operator.itemgetter # not a function at runtime
234-
operator.methodcaller
235-
optparse.HelpFormatter._format__Text
236-
optparse.OptionParser.__init__
237-
optparse.Values.__getattr__
238-
optparse.Values.read_file
239-
optparse.Values.read_module
240-
os._Environ.__init__
241-
os._Environ.setdefault
242-
os._wrap_close.__init__
243-
pickle.Pickler.persistent_id
244-
pickle.Unpickler.persistent_load
245-
pipes.Template.copy
246-
pkgutil.ImpImporter.__init__
247-
poplib.POP3_SSL.stls
225+
numbers.Number.__hash__ # typeshed marks this as abstract but code just sets this as None
226+
optparse.Values.__getattr__ # Some attributes are set in __init__ using setattr
227+
pickle.Pickler.persistent_id # C pickler persistent_id is an attribute
228+
pickle.Unpickler.persistent_load # C unpickler persistent_load is an attribute
229+
poplib.POP3_SSL.stls # bad declaration of inherited function. See poplib.pyi
248230
pydoc.HTMLDoc.docdata
249231
pydoc.HTMLDoc.docproperty
250232
pydoc.HTMLDoc.docroutine

0 commit comments

Comments
 (0)