Skip to content

Commit a4947f0

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 c516dba commit a4947f0

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/_pytest/mark/structures.py

+7-9
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
@@ -264,9 +265,9 @@ def combined_with(self, other: "Mark") -> "Mark":
264265

265266

266267
# A generic parameter designating an object to which a Mark may
267-
# be applied -- a test function (callable) or class.
268+
# be applied -- a test function (callable) or class (also callable).
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)
@@ -344,25 +345,22 @@ def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
344345
mark = Mark(self.name, args, kwargs, _ispytest=True)
345346
return MarkDecorator(self.mark.combined_with(mark), _ispytest=True)
346347

347-
# Type ignored because the overloads overlap with an incompatible
348-
# return type. Not much we can do about that. Thankfully mypy picks
349-
# the first match so it works out even if we break the rules.
350348
@overload
351-
def __call__(self, arg: Markable) -> Markable: # type: ignore[misc]
349+
def __call__(self, arg: Markable) -> Markable:
352350
pass
353351

354352
@overload
355-
def __call__(self, *args: object, **kwargs: object) -> "MarkDecorator":
353+
def __call__(self, *args: Any, **kwargs: Any) -> "MarkDecorator":
356354
pass
357355

358-
def __call__(self, *args: object, **kwargs: object):
356+
def __call__(self, *args: Any, **kwargs: Any) -> Union[Markable, "MarkDecorator"]:
359357
"""Call the MarkDecorator."""
360358
if args and not kwargs:
361359
func = args[0]
362360
is_class = inspect.isclass(func)
363361
if len(args) == 1 and (istestfunc(func) or is_class):
364362
store_mark(func, self.mark)
365-
return func
363+
return cast(Markable, func)
366364
return self.with_args(*args, **kwargs)
367365

368366

0 commit comments

Comments
 (0)