Skip to content

Commit 79141f6

Browse files
committed
fix: type Markable is reduced to Any by mypy
This change simplifies the definition of `Markable` so that type information is not lost and fixes the selection of the overloaded method to use.
1 parent 364bbe4 commit 79141f6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/_pytest/mark/structures.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44
from typing import Any
55
from typing import Callable
6+
from typing import cast
67
from typing import Collection
78
from typing import Iterable
89
from typing import Iterator
@@ -266,7 +267,7 @@ def combined_with(self, other: "Mark") -> "Mark":
266267
# A generic parameter designating an object to which a Mark may
267268
# be applied -- a test function (callable) or class.
268269
# Note: a lambda is not allowed, but this can't be represented.
269-
Markable = TypeVar("Markable", bound=Union[Callable[..., object], type])
270+
Markable = TypeVar("Markable", bound=Callable[..., Any])
270271

271272

272273
@attr.s(init=False, auto_attribs=True)
@@ -348,21 +349,21 @@ def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
348349
# return type. Not much we can do about that. Thankfully mypy picks
349350
# the first match so it works out even if we break the rules.
350351
@overload
351-
def __call__(self, arg: Markable) -> Markable: # type: ignore[misc]
352+
def __call__(self, arg: Markable) -> Markable:
352353
pass
353354

354355
@overload
355-
def __call__(self, *args: object, **kwargs: object) -> "MarkDecorator":
356+
def __call__(self, *args: Any, **kwargs: Any) -> "MarkDecorator":
356357
pass
357358

358-
def __call__(self, *args: object, **kwargs: object):
359+
def __call__(self, *args: Any, **kwargs: Any) -> Union[Markable, "MarkDecorator"]:
359360
"""Call the MarkDecorator."""
360361
if args and not kwargs:
361362
func = args[0]
362363
is_class = inspect.isclass(func)
363364
if len(args) == 1 and (istestfunc(func) or is_class):
364365
store_mark(func, self.mark)
365-
return func
366+
return cast(Markable, func)
366367
return self.with_args(*args, **kwargs)
367368

368369

0 commit comments

Comments
 (0)