KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
Other
134 stars 4 forks source link

Reports `no-any-expr` for parameter-less `@pytest.fixture` annotation #742

Open FichteFoll opened 3 weeks ago

FichteFoll commented 3 weeks ago

Describe the problem, ie expected/actual result (if it's not blatantly obvious)

When writing tests with pytest, any method decorated with @pytest.fixture causes no-any-expr to be reported.

$ poetry run mypy .
tests/integration/test_fix.py:3:2: error: Expression type contains "Any" (has type overloaded function)  [no-any-expr]
    @pytest.fixture
     ^~~~~~~~~~~~~~
tests/integration/test_fix.py:3:2: note: See https://kotlinisland.github.io/basedmypy/_refs.html#code-no-any-expr for more info
Found 1 error in 1 file (checked 14 source files)

Adding parentheses to the decorator, e.g. when defining (autouse=True) makes the error disappear because, most likely, another overload is matched. However, using just empty parentheses makes ruff report PT001, because this style is not recommended.

This may or may not be a problem with the pytest type stubs but I'm not deep enough into typing to debug this myself.

Gist to reproduce

import pytest

@pytest.fixture
def fix() -> None:
   return None

def test_fix(fix: None) -> None:
   pass

Basedmypy version

basedmypy 2.6.0 (compiled: yes) Based on mypy 1.11.1

KotlinIsland commented 3 weeks ago

Yeah, makes me cry that I have to set ruff to prefer the parenthesis 😢

I'm certain that it's an issue with pytest stubs, not with [based]mypy, the solutions I see are: