basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

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

Open FichteFoll opened this issue 1 year ago • 2 comments

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

FichteFoll avatar Aug 21 '24 08:08 FichteFoll

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:

  • fix defs in pytest
  • make a wrapper that is typed properly
  • make some custom Any ignoring rules where you can specify certain calls or something
  • make Anys not report if object can be used instead

KotlinIsland avatar Aug 21 '24 10:08 KotlinIsland

i know! lets reuse untyped_calls_exclude! perfect!

KotlinIsland avatar Nov 25 '24 00:11 KotlinIsland