GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #14606

New issue
Copy link
New issue
Copy link
Closed
#14611
Closed
pytest 9.1.0: new parametrize argvalues typing produces a spurious mypy error for a list of class objects with differing constructor signatures#14606
#14611
Copy link
Labels
topic: typingtype-annotation issuetype: regressionindicates a problem that was introduced in a release which was working previously

Description

@adamtheturtle
adamtheturtle
opened on Jun 15, 2026
Issue body actions

pytest 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: ...

Activity

  1. bluetech commented on Jun 15, 2026

    @bluetech
    bluetech
    on Jun 15, 2026
    Member
    More actions

    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.

  2. bluetech
    added
    topic: typingtype-annotation issue
    on Jun 15, 2026
  3. sobolevn commented on Jun 15, 2026

    @sobolevn
    sobolevn
    on Jun 15, 2026
    Member
    More actions

    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.

  4. adamtheturtle commented on Jun 15, 2026

    @adamtheturtle
    adamtheturtle
    on Jun 15, 2026
    Author
    More actions

    @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:
        ...
  5. bluetech commented on Jun 15, 2026

    @bluetech
    bluetech
    on Jun 15, 2026
    Member
    More actions

    @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.

  6. bluetech
    added
    type: regressionindicates a problem that was introduced in a release which was working previously
    on Jun 15, 2026
  7. bluetech
    closed this as completedin #14611on Jun 16, 2026
  8. pull
    added a commit that references this issue on Jun 16, 2026

    mark/structures: undo non-collection-argvalues type-check time deprec…

    This issue will close once commit 03116d3 is merged into the 'main' branch.
    03116d3
  9. RonnyPfannschmidt commented on Jun 17, 2026

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Jun 17, 2026
    Member
    More actions

    We need sequence to handle elements properly

    List will lock in on one type

    Reacted by sobolevn
  10. MateoGreil
    added a commit that references this issue on Jul 4, 2026

    ⬆️ Update dependency pytest to v9.1.1 (#24)

    45da729
  11. luketainton
    added a commit that references this issue on Jul 19, 2026

    [SECURITY] Update dependency pytest to v9 [SECURITY] (#2)

    fca183b
  12. Djeex
    added a commit that references this issue on Aug 31, 2026

    Update dependency pytest to v9 (#35)

    f96ce2f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: typingtype-annotation issuetype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.