diff --git a/python/src/com/jetbrains/python/testing/pyTestFixtures/PyTestUnpassedFixtureInspection.kt b/python/src/com/jetbrains/python/testing/pyTestFixtures/PyTestUnpassedFixtureInspection.kt index a2e9aa20332a..26c7e2f1f533 100644 --- a/python/src/com/jetbrains/python/testing/pyTestFixtures/PyTestUnpassedFixtureInspection.kt +++ b/python/src/com/jetbrains/python/testing/pyTestFixtures/PyTestUnpassedFixtureInspection.kt @@ -63,6 +63,20 @@ class PyTestUnpassedFixtureInspection : PyInspection() { // no warning if a fixture is in '@pytest.mark.usefixtures' arguments if (isParameterInDecorator(element, testFunction)) return + // if a reference resolves to a local binding target (assignment/with/for/walrus), + // then it's not a fixture usage and should be ignored. + element.reference.resolve()?.let { resolved -> + when (resolved) { + is PyTargetExpression -> return + is PyNamedParameter -> Unit // parameters are handled above + else -> Unit // do not early-return for functions/imports/etc. + } + } + + // Ignore all qualified attribute references (e.g., self.fixture, module.fixture) + // PyTest fixtures are not requested via qualified names, so treat them as non-fixture usages. + if (element.qualifier != null) return + // no warning if an element has type OTHER if (getType(element) == ResolveType.OTHER) return diff --git a/python/testData/inspections/PyTestUnpassedFixtureInspection/AsyncWithShadowing.py b/python/testData/inspections/PyTestUnpassedFixtureInspection/AsyncWithShadowing.py new file mode 100644 index 000000000000..dffec6dd0765 --- /dev/null +++ b/python/testData/inspections/PyTestUnpassedFixtureInspection/AsyncWithShadowing.py @@ -0,0 +1,22 @@ +import pytest +from contextlib import asynccontextmanager + + +class C: + async def bla(self): + ... + + +@asynccontextmanager +async def create(): + yield C() + + +@pytest.fixture +def client(): + pass + + +async def test_(): + async with create() as client: + assert await client.bla() == 1 # no inspection warning expected \ No newline at end of file diff --git a/python/testData/inspections/PyTestUnpassedFixtureInspection/AttributeShadowing.py b/python/testData/inspections/PyTestUnpassedFixtureInspection/AttributeShadowing.py new file mode 100644 index 000000000000..16b619ad68bc --- /dev/null +++ b/python/testData/inspections/PyTestUnpassedFixtureInspection/AttributeShadowing.py @@ -0,0 +1,14 @@ +import pytest + + +class Unrelated: + ... + + +@pytest.fixture +def fixture(): + return True + + +def test_(): + assert Unrelated().fixture # no inspection warning expected diff --git a/python/testData/inspections/PyTestUnpassedFixtureInspection/QualifiedClassFixtureCall.py b/python/testData/inspections/PyTestUnpassedFixtureInspection/QualifiedClassFixtureCall.py new file mode 100644 index 000000000000..5781c43bbabc --- /dev/null +++ b/python/testData/inspections/PyTestUnpassedFixtureInspection/QualifiedClassFixtureCall.py @@ -0,0 +1,10 @@ +import pytest + + +class TestBla: + @pytest.fixture + def my_f(self): + ... + + def test_(self): + self.my_f() # no inspection warning expected diff --git a/python/testData/inspections/PyTestUnpassedFixtureInspection/WithShadowing.py b/python/testData/inspections/PyTestUnpassedFixtureInspection/WithShadowing.py new file mode 100644 index 000000000000..a858f87c7ca5 --- /dev/null +++ b/python/testData/inspections/PyTestUnpassedFixtureInspection/WithShadowing.py @@ -0,0 +1,22 @@ +import pytest +from contextlib import contextmanager + + +class C: + def bla(self): + ... + + +@contextmanager +def create(): + yield C() + + +@pytest.fixture +def client(): + pass + + +def test_(): + with create() as client: + assert client.bla() == 1 # no inspection warning expected \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/testing/PyTestUnpassedFixtureInspectionTest.kt b/python/testSrc/com/jetbrains/python/testing/PyTestUnpassedFixtureInspectionTest.kt index 075613f58712..05816eb99362 100644 --- a/python/testSrc/com/jetbrains/python/testing/PyTestUnpassedFixtureInspectionTest.kt +++ b/python/testSrc/com/jetbrains/python/testing/PyTestUnpassedFixtureInspectionTest.kt @@ -55,4 +55,20 @@ class PyTestUnpassedFixtureInspectionTest : PyInspectionTestCase() { fun testNoInspectCorrectCode() { doTest() } + + fun testWithShadowing() { + doTest() + } + + fun testAsyncWithShadowing() { + doTest() + } + + fun testAttributeShadowing() { + doTest() + } + + fun testQualifiedClassFixtureCall() { + doTest() + } } \ No newline at end of file