Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List item has incompatible type when passed to pytest.mark.parametrize #9787

Closed
roniemartinez opened this issue Dec 9, 2020 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@roniemartinez
Copy link

roniemartinez commented Dec 9, 2020

Bug Report

When a list of NamedTuple is passed to pytest.mark.parametrize, the following error are thrown:

tests/test_mypy.py:25: error: List item 0 has incompatible type "A"; expected "Tuple[Union[str, int], ...]"
tests/test_mypy.py:26: error: List item 1 has incompatible type "B"; expected "Tuple[Union[str, int], ...]"
Found 2 errors in 1 file (checked 1 source file)

pytest.mark.parametrize is using Sequence so a list should be valid.

To Reproduce

Use the following example code and run the latest mypy version (as of this writing, 0.790)

from typing import NamedTuple, Optional

import pytest


class A(NamedTuple):
    first: int
    second: Optional[str] = None
    third: Optional[int] = None


class B(NamedTuple):
    second: Optional[str] = None
    third: Optional[int] = None


@pytest.mark.parametrize(
    "param",
    [
        # bug
        [
            A(first=1),
            B(),
        ],
        # solution is to use tuple as param
        (
            A(first=1),
            B(),
        ),
    ],
)
def test_same_named_tuple(param):
    pass

Expected Behavior

No errors.

Actual Behavior

Throws:

tests/test_mypy.py:25: error: List item 0 has incompatible type "A"; expected "Tuple[Union[str, int], ...]"
tests/test_mypy.py:26: error: List item 1 has incompatible type "B"; expected "Tuple[Union[str, int], ...]"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.8.5
  • Operating system and version: OSX 10.15.7 (Catalina)
@roniemartinez roniemartinez added the bug mypy got something wrong label Dec 9, 2020
@nmay231
Copy link

nmay231 commented Dec 30, 2020

I don't use pytest extensively, but looking through pytest's own test suite, I think you might be using parameterize incorrectly?

Try using two parameters to see if it at least fixes it.

@pytest.mark.parametrize(
  ("param1", "param2"),
    [
        (A(first=1), B()),
        (A(first=2), B()),
    ],
)

Pytest seems to allow the weird option of supporting comma delimited strings to pass multiple parameters (for example, "param1,param2" instead of ("param1", "param2")) and the error might be misleading because of that.

Otherwise, you might want to open an issue at PyTest to see if their types need to change to work with your use-case. 😄

@hauntsaninja
Copy link
Collaborator

Yeah, pytest ships its own type stubs, so there's no action item for mypy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants