Skip to content

Commit cc53f54

Browse files
authored
[assertpy] Replace or explain Anys (#15144)
1 parent 1be2c3f commit cc53f54

File tree

9 files changed

+55
-45
lines changed

9 files changed

+55
-45
lines changed
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# Python 2 compatibility cruft:
2-
assertpy.collection.Iterable
2+
assertpy\..+\.Iterable(\.__class_getitem__)?
33
assertpy.contains.str_types
44
assertpy.contains.xrange
5-
assertpy.dynamic.Iterable
6-
assertpy.exception.Iterable
7-
assertpy.extracting.Iterable
85
assertpy.extracting.str_types
96
assertpy.file.str_types
10-
assertpy.helpers.Iterable
11-
assertpy.string.Iterable
127
assertpy.string.str_types

stubs/assertpy/assertpy/assertpy.pyi

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from collections.abc import Callable, Generator
3-
from typing import Any
3+
from typing import Any, TypeVar
44
from typing_extensions import Self
55

66
from .base import BaseMixin
@@ -17,11 +17,14 @@ from .numeric import NumericMixin
1717
from .snapshot import SnapshotMixin
1818
from .string import StringMixin
1919

20+
_T = TypeVar("_T")
21+
_V = TypeVar("_V", default=Any)
22+
2023
__version__: str
2124
__tracebackhide__: bool
2225

2326
class WarningLoggingAdapter(logging.LoggerAdapter[logging.Logger]):
24-
def process(self, msg: str, kwargs: Any) -> tuple[str, Any]: ...
27+
def process(self, msg: str, kwargs: _T) -> tuple[str, _T]: ...
2528

2629
class AssertionBuilder(
2730
StringMixin,
@@ -34,36 +37,36 @@ class AssertionBuilder(
3437
DynamicMixin,
3538
DictMixin,
3639
DateMixin,
37-
ContainsMixin,
38-
CollectionMixin,
40+
ContainsMixin[_V],
41+
CollectionMixin[_V],
3942
BaseMixin,
4043
):
41-
val: Any
44+
val: _V
4245
description: str
4346
kind: str | None
4447
expected: BaseException | None
4548
logger: logging.Logger
4649
def __init__(
4750
self,
48-
val: Any,
51+
val: _V,
4952
description: str = "",
5053
kind: str | None = None,
5154
expected: BaseException | None = None,
5255
logger: logging.Logger | None = None,
5356
) -> None: ...
5457
def builder(
5558
self,
56-
val: Any,
59+
val: _V,
5760
description: str = "",
5861
kind: str | None = None,
5962
expected: BaseException | None = None,
6063
logger: logging.Logger | None = None,
6164
) -> Self: ...
6265
def error(self, msg: str) -> Self: ...
6366

64-
def soft_assertions() -> Generator[None, None, None]: ...
65-
def assert_that(val: Any, description: str = "") -> AssertionBuilder: ...
66-
def assert_warn(val: Any, description: str = "", logger: logging.Logger | None = None) -> AssertionBuilder: ...
67+
def soft_assertions() -> Generator[None]: ...
68+
def assert_that(val: _V, description: str = "") -> AssertionBuilder[_V]: ...
69+
def assert_warn(val: _V, description: str = "", logger: logging.Logger | None = None) -> AssertionBuilder: ...
6770
def fail(msg: str = "") -> None: ...
6871
def soft_fail(msg: str = "") -> None: ...
6972
def add_extension(func: Callable[[AssertionBuilder], AssertionBuilder]) -> None: ...

stubs/assertpy/assertpy/base.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from typing import Any
21
from typing_extensions import Self, TypeAlias
32

43
__tracebackhide__: bool
@@ -8,10 +7,10 @@ _IncludeIgnore: TypeAlias = str | list[str] | list[tuple[str, ...]] | None
87
class BaseMixin:
98
description: str
109
def described_as(self, description: str) -> Self: ...
11-
def is_equal_to(self, other: Any, *, include: _IncludeIgnore = None, ignore: _IncludeIgnore = None) -> Self: ...
12-
def is_not_equal_to(self, other: Any) -> Self: ...
13-
def is_same_as(self, other: Any) -> Self: ...
14-
def is_not_same_as(self, other: Any) -> Self: ...
10+
def is_equal_to(self, other: object, *, include: _IncludeIgnore = None, ignore: _IncludeIgnore = None) -> Self: ...
11+
def is_not_equal_to(self, other: object) -> Self: ...
12+
def is_same_as(self, other: object) -> Self: ...
13+
def is_not_same_as(self, other: object) -> Self: ...
1514
def is_true(self) -> Self: ...
1615
def is_false(self) -> Self: ...
1716
def is_none(self) -> Self: ...
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
from _typeshed import SupportsRichComparison
12
from collections.abc import Callable
2-
from typing import Any
3+
from typing import Any, Generic, Literal, TypeVar, overload
34
from typing_extensions import Self
45

56
__tracebackhide__: bool
67

7-
class CollectionMixin:
8+
_V = TypeVar("_V", default=Any)
9+
10+
class CollectionMixin(Generic[_V]):
811
def is_iterable(self) -> Self: ...
912
def is_not_iterable(self) -> Self: ...
10-
def is_subset_of(self, *supersets: Any) -> Self: ...
11-
def is_sorted(self, key: Callable[[Any], Any] = ..., reverse: bool = False) -> Self: ...
13+
def is_subset_of(self, *supersets: _V) -> Self: ...
14+
@overload
15+
def is_sorted(self, key: Callable[[_V], SupportsRichComparison] = ..., reverse: Literal[False] = False) -> Self: ...
16+
@overload
17+
def is_sorted(self, *, reverse: Literal[True]) -> Self: ...
18+
@overload
19+
def is_sorted(self, key: Callable[[_V], SupportsRichComparison], reverse: Literal[True]) -> Self: ...
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
from typing import Any
1+
from typing import Any, Generic, TypeVar
22
from typing_extensions import Self
33

44
__tracebackhide__: bool
55

6-
class ContainsMixin:
7-
def contains(self, *items: Any) -> Self: ...
8-
def does_not_contain(self, *items: Any) -> Self: ...
9-
def contains_only(self, *items: Any) -> Self: ...
10-
def contains_sequence(self, *items: Any) -> Self: ...
6+
_V = TypeVar("_V", default=Any)
7+
8+
class ContainsMixin(Generic[_V]):
9+
def contains(self, *items: object) -> Self: ...
10+
def does_not_contain(self, *items: object) -> Self: ...
11+
def contains_only(self, *items: object) -> Self: ...
12+
def contains_sequence(self, *items: object) -> Self: ...
1113
def contains_duplicates(self) -> Self: ...
1214
def does_not_contain_duplicates(self) -> Self: ...
1315
def is_empty(self) -> Self: ...
1416
def is_not_empty(self) -> Self: ...
15-
def is_in(self, *items: Any) -> Self: ...
16-
def is_not_in(self, *items: Any) -> Self: ...
17+
def is_in(self, *items: _V) -> Self: ...
18+
def is_not_in(self, *items: _V) -> Self: ...

stubs/assertpy/assertpy/dict.pyi

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
from collections.abc import Iterable
12
from typing import Any
23
from typing_extensions import Self
34

45
__tracebackhide__: bool
56

67
class DictMixin:
7-
def contains_key(self, *keys: Any) -> Self: ...
8-
def does_not_contain_key(self, *keys: Any) -> Self: ...
9-
def contains_value(self, *values: Any) -> Self: ...
10-
def does_not_contain_value(self, *values: Any) -> Self: ...
11-
def contains_entry(self, *args: Any, **kwargs: dict[str, Any]) -> Self: ...
12-
def does_not_contain_entry(self, *args: Any, **kwargs: dict[str, Any]) -> Self: ...
8+
def contains_key(self, *keys: object) -> Self: ...
9+
def does_not_contain_key(self, *keys: object) -> Self: ...
10+
def contains_value(self, *values: object) -> Self: ...
11+
def does_not_contain_value(self, *values: object) -> Self: ...
12+
# The dicts can contain arbitrary keys and values
13+
def contains_entry(self, *args: Iterable[dict[Any, Any]], **kwargs: Any) -> Self: ...
14+
def does_not_contain_entry(self, *args: Iterable[dict[Any, Any]], **kwargs: Any) -> Self: ...

stubs/assertpy/assertpy/dynamic.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ from typing_extensions import Self
44
__tracebackhide__: bool
55

66
class DynamicMixin:
7-
def __getattr__(self, attr: str) -> Callable[..., Self]: ...
7+
def __getattr__(self, attr: str) -> Callable[[object], Self]: ...
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from collections.abc import Callable, Iterable as _Iterable, Mapping
1+
from _typeshed import SupportsRichComparison
2+
from collections.abc import Callable, Iterable, Mapping
23
from typing import Any
34
from typing_extensions import Self
45

@@ -8,6 +9,7 @@ class ExtractingMixin:
89
def extracting(
910
self,
1011
*names: str,
12+
# The callable must accept the type of the items in the self.val collection.
1113
filter: str | Mapping[str, Any] | Callable[[Any], bool] = ...,
12-
sort: str | _Iterable[str] | Callable[[Any], Any] = ...,
14+
sort: str | Iterable[str] | Callable[[Any], SupportsRichComparison] = ...,
1315
) -> Self: ...

stubs/assertpy/assertpy/file.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from _typeshed import StrPath
2-
from typing import IO, AnyStr
1+
from _typeshed import StrPath, SupportsRead
32
from typing_extensions import Self
43

54
__tracebackhide__: bool
65

7-
def contents_of(file: IO[AnyStr] | StrPath, encoding: str = "utf-8") -> str: ...
6+
def contents_of(file: SupportsRead[str] | StrPath, encoding: str = "utf-8") -> str: ...
87

98
class FileMixin:
109
def exists(self) -> Self: ...

0 commit comments

Comments
 (0)