Skip to content

Commit 844e4f7

Browse files
authored
Updates to boltons types (#13517)
1 parent ac8f263 commit 844e4f7

14 files changed

Lines changed: 263 additions & 161 deletions

stubs/boltons/boltons/cacheutils.pyi

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import weakref
2-
from _typeshed import Incomplete, Self, SupportsItems, SupportsKeysAndGetItem
2+
from _typeshed import Incomplete, SupportsItems, SupportsKeysAndGetItem
33
from collections.abc import Callable, Generator, Hashable, Iterable, Iterator, Mapping
44
from typing import Any, Generic, TypeVar, overload
5+
from typing_extensions import Self
56

67
_KT = TypeVar("_KT")
78
_VT = TypeVar("_VT")
@@ -18,8 +19,10 @@ class LRI(dict[_KT, _VT]):
1819
miss_count: int
1920
soft_miss_count: int
2021
max_size: int
21-
on_miss: Callable[[_KT], _VT]
22-
def __init__(self, max_size: int = 128, values: Incomplete | None = None, on_miss: Incomplete | None = None) -> None: ...
22+
on_miss: Callable[[_KT], _VT] | None
23+
def __init__(
24+
self, max_size: int = 128, values: Incomplete | None = None, on_miss: Callable[[_KT], _VT] | None = None
25+
) -> None: ...
2326
def __setitem__(self, key: _KT, value: _VT) -> None: ...
2427
def __getitem__(self, key: _KT) -> _VT: ...
2528
@overload
@@ -33,9 +36,9 @@ class LRI(dict[_KT, _VT]):
3336
def pop(self, key: _KT, default: _T) -> _T | _VT: ...
3437
def popitem(self) -> tuple[_KT, _VT]: ...
3538
def clear(self) -> None: ...
36-
def copy(self: Self) -> Self: ...
39+
def copy(self) -> Self: ...
3740
@overload
38-
def setdefault(self, key: _KT, default: None = None) -> _VT: ...
41+
def setdefault(self, key: _KT, default: None = None) -> _VT | None: ...
3942
@overload
4043
def setdefault(self, key: _KT, default: _VT) -> _VT: ...
4144
def update(self, E: SupportsKeysAndGetItem[_KT, _VT] | Iterable[tuple[_KT, _VT]], **F: _VT) -> None: ... # type: ignore[override]
@@ -57,7 +60,14 @@ class CachedFunction:
5760
scoped: Incomplete
5861
typed: Incomplete
5962
key_func: Incomplete
60-
def __init__(self, func, cache, scoped: bool = True, typed: bool = False, key: Incomplete | None = None): ...
63+
def __init__(
64+
self,
65+
func,
66+
cache: Mapping[Any, Any] | Callable[..., Incomplete],
67+
scoped: bool = True,
68+
typed: bool = False,
69+
key: Callable[..., Incomplete] | None = None,
70+
): ...
6171
def __call__(self, *args, **kwargs): ...
6272

6373
class CachedMethod:
@@ -67,17 +77,37 @@ class CachedMethod:
6777
typed: Incomplete
6878
key_func: Incomplete
6979
bound_to: Incomplete
70-
def __init__(self, func, cache, scoped: bool = True, typed: bool = False, key: Incomplete | None = None): ...
80+
def __init__(
81+
self,
82+
func,
83+
cache: Mapping[Any, Any] | Callable[..., Incomplete],
84+
scoped: bool = True,
85+
typed: bool = False,
86+
key: Callable[..., Incomplete] | None = None,
87+
): ...
7188
def __get__(self, obj, objtype: Incomplete | None = None): ...
7289
def __call__(self, *args, **kwargs): ...
7390

74-
def cached(cache: Mapping[Any, Any], scoped: bool = True, typed: bool = False, key: Incomplete | None = None): ...
75-
def cachedmethod(cache, scoped: bool = True, typed: bool = False, key: Incomplete | None = None): ...
91+
def cached(
92+
cache: Mapping[Any, Any] | Callable[..., Incomplete],
93+
scoped: bool = True,
94+
typed: bool = False,
95+
key: Callable[..., Incomplete] | None = None,
96+
): ...
97+
def cachedmethod(
98+
cache: Mapping[Any, Any] | Callable[..., Incomplete],
99+
scoped: bool = True,
100+
typed: bool = False,
101+
key: Callable[..., Incomplete] | None = None,
102+
): ...
76103

77-
class cachedproperty(Generic[_T]):
78-
func: Callable[[Incomplete], _T]
79-
def __init__(self, func: Callable[[Incomplete], _T]) -> None: ...
80-
def __get__(self, obj: _T, objtype: type | None = None): ...
104+
class cachedproperty(Generic[_KT, _VT]):
105+
func: Callable[[_KT], _VT]
106+
def __init__(self, func: Callable[[_KT], _VT]) -> None: ...
107+
@overload
108+
def __get__(self, obj: None, objtype: type | None = None) -> Self: ...
109+
@overload
110+
def __get__(self, obj: _KT, objtype: type | None = None) -> _VT: ...
81111

82112
class ThresholdCounter(Generic[_T]):
83113
total: int
@@ -103,8 +133,8 @@ class ThresholdCounter(Generic[_T]):
103133
def update(self, iterable: Iterable[_T] | Mapping[_T, int], **kwargs: Iterable[_T] | Mapping[_T, int]) -> None: ...
104134

105135
class MinIDMap(Generic[_T]):
106-
mapping: weakref.WeakKeyDictionary[_T, int]
107-
ref_map: dict[_T, int]
136+
mapping: weakref.WeakKeyDictionary[_T, tuple[int, weakref.ReferenceType[_T]]]
137+
ref_map: dict[weakref.ReferenceType[_T], int]
108138
free: list[int]
109139
def __init__(self) -> None: ...
110140
def get(self, a: _T) -> int: ...

stubs/boltons/boltons/dictutils.pyi

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from _typeshed import SupportsKeysAndGetItem
2-
from binascii import Incomplete
32
from collections.abc import Generator, ItemsView, Iterable, KeysView, ValuesView
4-
from typing import NoReturn, TypeVar
3+
from typing import NoReturn, TypeVar, overload
54
from typing_extensions import Self, TypeAlias
65

76
_KT = TypeVar("_KT")
@@ -13,26 +12,32 @@ class OrderedMultiDict(dict[_KT, _VT]):
1312
def addlist(self, k: _KT, v: Iterable[_VT]) -> None: ...
1413
def clear(self) -> None: ...
1514
def copy(self) -> Self: ...
16-
def counts(self) -> OrderedMultiDict[_KT, _VT]: ...
15+
def counts(self) -> Self: ...
1716
@classmethod
18-
def fromkeys(cls, keys: _KT, default: _VT | None = None) -> OrderedMultiDict[_KT, _VT]: ... # type: ignore[override]
19-
def get(self, k: _KT, default: _VT | None = None) -> OrderedMultiDict[_KT, _VT]: ... # type: ignore[override]
20-
def getlist(self, k: _KT, default: _VT | None = ...) -> list[object]: ...
21-
def inverted(self) -> OrderedMultiDict[_KT, _VT]: ...
17+
def fromkeys(cls, keys: _KT, default: _VT | None = None) -> Self: ... # type: ignore[override]
18+
@overload # type: ignore[override]
19+
def get(self, k: _KT, default: None = None) -> _VT | None: ...
20+
@overload
21+
def get(self, k: _KT, default: _VT) -> _VT: ...
22+
def getlist(self, k: _KT, default: list[_VT] = ...) -> list[_VT]: ...
23+
def inverted(self) -> Self: ...
2224
def items(self, multi: bool = False) -> list[tuple[_KT, _VT]]: ... # type: ignore[override]
2325
def iteritems(self, multi: bool = False) -> Generator[tuple[_KT, _VT], None, None]: ...
2426
def iterkeys(self, multi: bool = False) -> Generator[_KT, None, None]: ...
2527
def itervalues(self, multi: bool = False) -> Generator[_VT, None, None]: ...
2628
def keys(self, multi: bool = False) -> list[_KT]: ... # type: ignore[override]
27-
def pop(self, k: _KT, default: _VT | None = ...) -> _VT: ... # type: ignore[override]
28-
def popall(self, k: _KT, default: _VT | None = ...) -> list[_VT]: ...
29-
def poplast(self, k: _KT | None = ..., default: _VT | None = ...) -> _VT: ...
30-
def setdefault(self, k: _KT, default: _VT | None = ...) -> _VT: ...
31-
def sorted(self, key: _KT | None = None, reverse: bool = False) -> OrderedMultiDict[_KT, _VT]: ...
32-
def sortedvalues(self, key: _KT | None = None, reverse: bool = False) -> OrderedMultiDict[_KT, _VT]: ...
29+
def pop(self, k: _KT, default: _VT = ...) -> _VT: ... # type: ignore[override]
30+
def popall(self, k: _KT, default: _VT = ...) -> list[_VT]: ...
31+
def poplast(self, k: _KT = ..., default: _VT = ...) -> _VT: ...
32+
@overload # type: ignore[override]
33+
def setdefault(self, k: _KT, default: None = ...) -> _VT | None: ...
34+
@overload
35+
def setdefault(self, k: _KT, default: _VT) -> _VT: ...
36+
def sorted(self, key: _KT | None = None, reverse: bool = False) -> Self: ...
37+
def sortedvalues(self, key: _KT | None = None, reverse: bool = False) -> Self: ...
3338
def todict(self, multi: bool = False) -> dict[_KT, _VT]: ...
34-
def update(self, E: dict[_KT, _VT] | Iterable[object], **F) -> None: ... # type: ignore[override]
35-
def update_extend(self, E: dict[_KT, _VT] | Iterable[object], **F) -> None: ...
39+
def update(self, E: SupportsKeysAndGetItem[_KT, _VT] | Iterable[tuple[_KT, _VT]], **F) -> None: ... # type: ignore[override]
40+
def update_extend(self, E: SupportsKeysAndGetItem[_KT, _VT] | Iterable[tuple[_KT, _VT]], **F) -> None: ...
3641
def values(self, multi: bool = False) -> list[_VT]: ... # type: ignore[override]
3742
def viewitems(self) -> ItemsView[_KT, _VT]: ...
3843
def viewkeys(self) -> KeysView[_KT]: ...
@@ -46,7 +51,7 @@ class FastIterOrderedMultiDict(OrderedMultiDict[_KT, _VT]): # undocumented
4651
def iterkeys(self, multi: bool = False) -> Generator[_KT, None, None]: ...
4752

4853
class OneToOne(dict[_KT, _VT]):
49-
inv: dict[_VT, _KT]
54+
inv: OneToOne[_VT, _KT]
5055
def clear(self) -> None: ...
5156
def copy(self) -> Self: ...
5257
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
@@ -63,7 +68,9 @@ class ManyToMany(dict[_KT, frozenset[_VT]]):
6368
def __delitem__(self, key: _KT) -> None: ...
6469
def __eq__(self, other): ...
6570
def __getitem__(self, key: _KT): ...
66-
def __init__(self, items: Iterable[Incomplete] | None = None) -> None: ...
71+
def __init__(
72+
self, items: ManyToMany[_KT, _VT] | SupportsKeysAndGetItem[_KT, _VT] | tuple[_KT, _VT] | None = None
73+
) -> None: ...
6774
def __iter__(self): ...
6875
def __len__(self): ...
6976
def __setitem__(self, key: _KT, vals: Iterable[_VT]) -> None: ...
@@ -81,10 +88,16 @@ class FrozenHashError(TypeError): ... # undocumented
8188

8289
class FrozenDict(dict[_KT, _VT]):
8390
def __copy__(self) -> Self: ...
84-
def clear(self, *a, **kw) -> None: ...
8591
@classmethod
86-
def fromkeys(cls, keys: Iterable[_KT], value: _VT | None = None) -> FrozenDict[_KT, _VT]: ... # type: ignore[override]
92+
def fromkeys(cls, keys: Iterable[_KT], value: _VT | None = None) -> Self: ... # type: ignore[override]
93+
def updated(self, *a, **kw) -> Self: ...
94+
# Can't noqa because of https://github.com/plinss/flake8-noqa/pull/30
95+
# Signature conflicts with superclass, so let's just omit it
96+
# def __ior__(self, *a, **kw) -> NoReturn: ...
97+
def __setitem__(self, *a, **kw) -> NoReturn: ...
98+
def __delitem__(self, *a, **kw) -> NoReturn: ...
99+
def update(self, *a, **kw) -> NoReturn: ...
87100
def pop(self, *a, **kw) -> NoReturn: ...
88101
def popitem(self, *a, **kw) -> NoReturn: ...
89102
def setdefault(self, *a, **kw) -> NoReturn: ...
90-
def updated(self, *a, **kw) -> Self: ...
103+
def clear(self, *a, **kw) -> NoReturn: ...

stubs/boltons/boltons/fileutils.pyi

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
from _typeshed import StrOrBytesPath
1+
from _typeshed import BytesPath, StrOrBytesPath, StrPath
22
from collections.abc import Callable, Generator, Iterable
33
from os import PathLike
44
from types import TracebackType
5-
from typing import IO, Any, NoReturn
5+
from typing import IO, Any, NoReturn, TypeVar, overload
66
from typing_extensions import Self
77

8+
_StrPathT = TypeVar("_StrPathT", bound=StrPath)
9+
_BytesPathT = TypeVar("_BytesPathT", bound=BytesPath)
10+
811
def mkdir_p(path: StrOrBytesPath) -> None: ...
912
def rotate_file(filename: PathLike[str], *, keep: int = 5) -> None: ...
1013

@@ -24,9 +27,9 @@ def atomic_save(dest_path: str, **kwargs) -> AtomicSaver: ...
2427
class AtomicSaver:
2528
dest_path: str
2629
overwrite: bool
27-
file_perms: int
30+
file_perms: int | None
2831
overwrite_part: bool
29-
part_filename: str
32+
part_filename: str | None
3033
rm_part_on_exc: bool
3134
text_mode: bool
3235
buffering: int
@@ -49,17 +52,22 @@ def iter_find_files(
4952
include_dirs: bool = False,
5053
max_depth: int | None = None,
5154
) -> Generator[str, None, None]: ...
55+
@overload
56+
def copy_tree(
57+
src: _StrPathT, dst: _StrPathT, symlinks: bool = False, ignore: Callable[[_StrPathT, list[str]], Iterable[str]] | None = None
58+
) -> None: ...
59+
@overload
5260
def copy_tree(
53-
src: StrOrBytesPath,
54-
dst: StrOrBytesPath,
61+
src: _BytesPathT,
62+
dst: _BytesPathT,
5563
symlinks: bool = False,
56-
ignore: None | Callable[[str, list[str]], Iterable[str]] | Callable[[StrOrBytesPath, list[str]], Iterable[str]] = None,
64+
ignore: Callable[[_BytesPathT, list[bytes]], Iterable[bytes]] | None = None,
5765
) -> None: ...
5866

5967
copytree = copy_tree
6068

6169
class DummyFile:
62-
name: str
70+
name: StrOrBytesPath
6371
mode: str
6472
closed: bool
6573
errors: None

stubs/boltons/boltons/formatutils.pyi

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections.abc import Callable
2-
from typing import Any
2+
from typing import Generic, TypeVar
33

4-
def construct_format_field_str(fname: str, fspec: str, conv: str) -> str: ...
4+
_T = TypeVar("_T")
5+
6+
def construct_format_field_str(fname: str | None, fspec: str | None, conv: str | None) -> str: ...
57
def infer_positional_format_args(fstr: str) -> str: ...
68
def get_format_args(fstr: str) -> tuple[list[tuple[int, type]], list[tuple[str, type]]]: ...
79
def tokenize_format_str(fstr: str, resolve_pos: bool = True) -> list[str | BaseFormatField]: ...
@@ -18,17 +20,17 @@ class BaseFormatField:
1820
type_char: str
1921
type_func: str
2022
def set_fspec(self, fspec) -> None: ...
21-
conv: str
23+
conv: str | None
2224
conv_func: str | None
23-
def set_conv(self, conv: str) -> None: ...
25+
def set_conv(self, conv: str | None) -> None: ...
2426
@property
2527
def fstr(self) -> str: ...
2628

27-
class DeferredValue:
28-
func: Callable[..., Any]
29+
class DeferredValue(Generic[_T]):
30+
func: Callable[[], _T]
2931
cache_value: bool
30-
def __init__(self, func: Callable[..., Any], cache_value: bool = True) -> None: ...
31-
def get_value(self) -> Any: ...
32+
def __init__(self, func: Callable[[], _T], cache_value: bool = True) -> None: ...
33+
def get_value(self) -> _T: ...
3234
def __int__(self) -> int: ...
3335
def __float__(self) -> float: ...
3436
def __unicode__(self) -> str: ...
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
from collections.abc import Iterable
2-
from typing import SupportsIndex, TypeVar, overload
2+
from typing import SupportsIndex, TypeVar
33
from typing_extensions import Self, TypeAlias
44

55
_T = TypeVar("_T")
66

77
class BarrelList(list[_T]):
88
lists: list[list[_T]]
9-
@overload
10-
def __init__(self, iterable: None = None) -> None: ...
11-
@overload
12-
def __init__(self, iterable: Iterable[_T]) -> None: ...
9+
def __init__(self, iterable: Iterable[_T] | None = None) -> None: ...
1310
def insert(self, index: SupportsIndex, item: _T) -> None: ...
1411
def append(self, item: _T) -> None: ...
1512
def extend(self, iterable: Iterable[_T]) -> None: ...
1613
def pop(self, *a) -> _T: ...
17-
def iter_slice(self, start: int, stop: int, step: int | None = None) -> Iterable[_T]: ...
14+
def iter_slice(self, start: int | None, stop: int | None, step: int | None = None) -> Iterable[_T]: ...
1815
def del_slice(self, start: int, stop: int, step: int | None = None) -> None: ...
1916
__delslice__ = del_slice
2017
@classmethod
2118
def from_iterable(cls, it: Iterable[_T]) -> Self: ...
2219
def __getslice__(self, start: int, stop: int): ...
23-
def __setslice__(self, start: int, stop: int, sequence: int) -> None: ...
20+
def __setslice__(self, start: SupportsIndex, stop: SupportsIndex, sequence: Iterable[_T]) -> None: ...
2421
def sort(self) -> None: ... # type: ignore[override]
2522
def reverse(self) -> None: ...
2623
def count(self, item: _T) -> int: ...
@@ -30,4 +27,4 @@ BList: TypeAlias = BarrelList[_T]
3027

3128
class SplayList(list[_T]):
3229
def shift(self, item_index: int, dest_index: int = 0) -> None: ...
33-
def swap(self, item_index: int, dest_index: int) -> None: ...
30+
def swap(self, item_index: SupportsIndex, dest_index: SupportsIndex) -> None: ...

stubs/boltons/boltons/mathutils.pyi

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Iterable
2+
from typing import overload
23

34
def clamp(x: float, lower: float = ..., upper: float = ...) -> float: ...
4-
def ceil(x: float, options: Sequence[float] | None = None) -> float: ...
5-
def floor(x: float, options: Sequence[float] | None = None) -> float: ...
5+
@overload
6+
def ceil(x: float, options: None = None) -> int: ...
7+
@overload
8+
def ceil(x: float, options: Iterable[float]) -> float: ...
9+
@overload
10+
def floor(x: float, options: None = None) -> int: ...
11+
@overload
12+
def floor(x: float, options: Iterable[float]) -> float: ...
613

714
class Bits:
815
val: int
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import mailbox
2+
from _typeshed import StrPath
3+
from collections.abc import Callable
4+
from typing import IO, Any
25

36
DEFAULT_MAXMEM: int
47

58
class mbox_readonlydir(mailbox.mbox):
69
maxmem: int
7-
def __init__(self, path: str, factory: type | None = None, create: bool = True, maxmem: int = 1048576) -> None: ...
10+
def __init__(
11+
self,
12+
path: StrPath,
13+
factory: Callable[[IO[Any]], mailbox.mboxMessage] | None = None,
14+
create: bool = True,
15+
maxmem: int = 1048576,
16+
) -> None: ...
817
def flush(self) -> None: ...
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Sequence
1+
from collections.abc import Iterable
22

3-
def namedtuple(typename: str, field_names: Sequence[str], verbose: bool = False, rename: bool = False): ...
4-
def namedlist(typename: str, field_names: Sequence[str], verbose: bool = False, rename: bool = False): ...
3+
def namedtuple(typename: str, field_names: str | Iterable[str], verbose: bool = False, rename: bool = False): ...
4+
def namedlist(typename: str, field_names: str | Iterable[str], verbose: bool = False, rename: bool = False): ...
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
from _typeshed import StrPath
2+
13
def augpath(
2-
path: str,
4+
path: StrPath,
35
suffix: str = "",
46
prefix: str = "",
57
ext: str | None = None,
68
base: str | None = None,
79
dpath: str | None = None,
810
multidot: bool = False,
911
) -> str: ...
10-
def shrinkuser(path: str, home: str = "~") -> str: ...
11-
def expandpath(path: str) -> str: ...
12+
def shrinkuser(path: StrPath, home: str = "~") -> str: ...
13+
def expandpath(path: StrPath) -> str: ...

0 commit comments

Comments
 (0)