[python] PY-83215 Renaming a pytest fixture leads to unresolved references

Merge-request: IJ-MR-181100
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: ed0ae783d8f1caadc1686fde42f4bba27fe35f95
This commit is contained in:
chbndrhnns
2025-11-03 15:54:29 +00:00
committed by intellij-monorepo-bot
parent 7454cc8bb7
commit 57dc355ada
4 changed files with 37 additions and 5 deletions
@@ -87,9 +87,12 @@ class PyTestFixtureReference(pyElement: PsiElement, fixture: PyTestFixture, priv
if (myElement is PyStringLiteralExpression) {
return myElement.replace(PyElementGenerator.getInstance(myElement.project).createStringLiteralFromString(newElementName))
}
val annotationString = (myElement as? PyNamedParameter)?.annotation?.value?.name
return myElement.replace(PyElementGenerator.getInstance(myElement.project)
.createParameter(newElementName, null, annotationString, LanguageLevel.getDefault()))!!
(myElement as? PyNamedParameter)?.let {
it.setName(newElementName)
return it
}
return super.handleElementRename(newElementName)
}
}
@@ -137,8 +140,9 @@ private object PyTestReferenceAsParameterProvider : PyTestReferenceProvider() {
val namedParam = element as? PyNamedParameter ?: return emptyArray()
val namedFixtureParameterLink = getFixtureLink(namedParam, TypeEvalContext.codeAnalysis(element.project, element.containingFile))
?: return emptyArray()
val annotationLength = namedParam.annotation?.textLength ?: 0
return arrayOf(PyTestFixtureReference(namedParam, namedFixtureParameterLink.fixture, namedFixtureParameterLink.importElement, TextRange(0, element.textLength - annotationLength)))
val nameId = namedParam.nameIdentifier
val range = nameId?.textRangeInParent ?: TextRange.from(0, namedParam.textLength)
return arrayOf(PyTestFixtureReference(namedParam, namedFixtureParameterLink.fixture, namedFixtureParameterLink.importElement, range))
}
}
@@ -0,0 +1,11 @@
import types
import pytest
@pytest.fixture()
def abc():
return types.SimpleNamespace(get=lambda: None)
def test(abc: types.SimpleNamespace):
assert True
@@ -0,0 +1,11 @@
import types
import pytest
@pytest.fixture()
def client<caret>():
return types.SimpleNamespace(get=lambda: None)
def test(client: types.SimpleNamespace):
assert True
@@ -143,4 +143,10 @@ class PyTestFixtureAndParametrizedTest : PyTestCase() {
myFixture.renameElementAtCaret("second")
myFixture.checkResultByFile("after_rename_multiple_parametrization_second_param.txt")
}
fun testRenamePreserveQualifiedAnnotation() {
myFixture.configureByFile("test_rename_preserve_qualified_annotation.py")
myFixture.renameElementAtCaret("abc")
myFixture.checkResultByFile("after_rename_preserve_qualified_annotation.txt")
}
}