Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

"Signature of a method incompatible with supertype" vs pytest fixtures #10065

Closed
serhiy-storchaka opened this issue Jun 22, 2022 · 1 comment
Closed

Comments

@serhiy-storchaka
Copy link

I don't know whether it is idiomatic or not, but I use the following approach to parametrize pytest tests. Tests are defined as method in a class. Parameters are defined as fixtures in a class. Fixtures are overridden in subclasses to run tests with different parameters.

For example:

import pytest


class TestA:
    @pytest.fixture
    def foo(self) -> int:
        return 42

    def test_foo(self, foo: int) -> None:
        assert foo > 0


@pytest.fixture
def bar() -> str:
    return "abc"


class TestB(TestA):
    @pytest.fixture
    def foo(self, bar: str) -> int:  # line 20
        return len(bar)

TestA.test_foo and TestB.test_foo are executed with different values of foo. The problem is that fixtures can depend on other fixtures, and signatures for methods defining fixture can be different. MyPy complains:

t.py:20: error: Signature of "foo" incompatible with supertype "TestA"
t.py:20: note:      Superclass:
t.py:20: note:          def foo(self) -> int
t.py:20: note:      Subclass:
t.py:20: note:          def foo(self, bar: str) -> int

It is not real error in the user code that the methods have different signatures. These methods are not called directly. They are called by pytest which provides all arguments.

I can add "# type: ignore", but I do not want to lose static type checking completely. It is useful to check that the type of the parameter matches the returning type of corresponding fixture function.

mypy 0.961 (compiled: yes)
pytest 7.1.2
Python 3.9.13

@The-Compiler
Copy link
Member

This doesn't seem like something pytest can do anything about. Maybe it should be a discussion rather than an issue?

I've seen this message being disputed for other reasons too:

Note that mypy supports adding error codes to type ignores.

@pytest-dev pytest-dev locked and limited conversation to collaborators Jun 25, 2022
@Zac-HD Zac-HD converted this issue into discussion #10074 Jun 25, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants