GeezerVison is showing github.com — pytest 9.1.0: new `parametrize` `argvalues` typing produces a spurious mypy error for a list of class objects with differing constructor signatures · Issue #14606 · pytest-dev/pytest
pytest 9.1.0: new parametrize argvalues typing produces a spurious mypy error for a list of class objects with differing constructor signatures #14606Descriptionpytest 9.1.0 added typed overloads for pytest.mark.parametrize, typing argvalues as Collection[ParameterSet | Sequence[object] | object] (part of the deprecation of non-Collection iterables, #13409).
Passing a list of two unrelated class objects whose constructors have incompatible parameter kinds (one keyword-only, one positional-or-keyword) now produces a false-positive mypy error:
error: List item 0 has incompatible type "type[Foo]"; expected "type[object]" [list-item]
The same call type-checked fine on pytest 9.0.3.
The usage is exactly the form the changelog recommends: a list is a Collection, and the deprecation targeted iterators (generators) that get exhausted, not lists of classes.
Environment
- pytest 9.1.0
- mypy 2.1.0
- Python 3.14
Minimal reproduction
import pytest
class KwOnly:
def __init__(self, *, x: int = 0) -> None: ...
class Positional:
def __init__(self, x: int = 0) -> None: ...
# mypy error: List item 0 has incompatible type "type[KwOnly]"; expected "type[object]"
@pytest.mark.parametrize(argnames="cls", argvalues=[KwOnly, Positional])
def test_list(cls: type) -> None: ...
# OK: a tuple keeps each element's own type, so there is no join.
# Note: ruff's PT007 defaults to expecting a list for argvalues, so ruff users
# would have to set flake8-pytest-style.parametrize-values-type = "tuple".
@pytest.mark.parametrize(argnames="cls", argvalues=(KwOnly, Positional))
def test_tuple(cls: type) -> None: ...Activitybluetech commented on Jun 15, 2026 Hmm, I am having a hard time understanding mypy's error here. I tried with pyright, ty, pyrefly (-p strict) and zuban (--strict) and they are fine with it.
If I had to guess mypy's logic:
- The type of
KwOnly is type[KwOnly]
- The type of
Positional is type[Positional]
- Their join is
type[object], because type[] is covariant (ref)
- So the type of the list is
list[type[object]]
- But then what's this about "incompatible type "type[KwOnly]"; expected "type[object]""?
I think this might be a mypy bug, since it gives this error even without pytest's involvement:
class KwOnly:
def __init__(self, *, x: int = 0) -> None: ...
class Positional:
def __init__(self, x: int = 0) -> None: ...
# error: List item 0 has incompatible type "type[KwOnly]"; expected "type[object]" [list-item]
[KwOnly, Positional]
It's interesting it wasn't a problem for you with previous pytest typing. sobolevn commented on Jun 15, 2026 I have a similar problem:
import pytest
@pytest.mark.parametrize(
('schema_path', 'expected_exception'),
[
('does.not.exist:schema', ImportError),
('server.urls:urlpatterns', AttributeError),
],
)
def test_export_schema_invalid_input(
schema_path: str,
expected_exception: type[Exception],
) -> None:
...
Error:
tests/test_integration/test_management/test_dmr_export_schema.py:99: error: List item 1 has incompatible type "tuple[str, type[AttributeError]]"; expected "tuple[str, type[Exception]]" [list-item]
Not quite sure on how to fix it. adamtheturtle commented on Jun 15, 2026 @sobolevn , I believe you can use a tuple of parameters rather than a list:
import pytest
@pytest.mark.parametrize(
('schema_path', 'expected_exception'),
(
('does.not.exist:schema', ImportError),
('server.urls:urlpatterns', AttributeError),
),
)
def test_export_schema_invalid_input(
schema_path: str,
expected_exception: type[Exception],
) -> None:
...bluetech commented on Jun 15, 2026 @sobolevn Yes it's the same thing:
# error: List item 0 has incompatible type "type[ImportError]"; expected "type[Exception]" [list-item]
[ImportError, AttributeError]
As @adamtheturtle said, it's related to the type's __init__ signature, so it doesn't happen with e.g. [TypeError, AttributeError].
It does seem that mypy devolves type[..] to the ctor signature, e.g. reveal_type(KwOnly) gives Revealed type is "def (*, x: int =) -> KwOnly".
I was curious why it worked before. Seems the difference is the addition of @overload:
def ok(x: Collection[object]) -> None: pass
ok([KwOnly, Positional])
@overload
def bad(x: Collection[object]) -> None: pass
@overload
def bad(x: object) -> None: pass
def bad(x: object) -> None: pass
# error: List item 0 has incompatible type "type[KwOnly]"; expected "type[object]" [list-item]
bad([KwOnly, Positional])
I would guess that without overload, mypy can infer the argument in the context of Collection[object], and the context makes it work, but with overloads it needs to infer the type without a context first, which gives the error.
I added the overload so I can stick a @deprecated on the non-Collection overload. If we want to workaround this mypy bug(?), we can either
1, Drop the Collection overload, leaving only the runtime deprecation warning
2. Drop the non-Collection overload, turning the deprecated usage to a static type error rather than a static deprecation warning
Strictly speaking 2 is not backward compatible so needs to be 1 until pytest 10. I will try to do it for 9.1.1. added a commit that references this issue on Jun 16, 2026This issue will close once commit 03116d3 is merged into the 'main' branch.03116d3 RonnyPfannschmidt commented on Jun 17, 2026 We need sequence to handle elements properly
List will lock in on one type
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|