From 4ba9d71cce29273f0e0455388545cd8496b0a359 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 2 May 2025 15:39:27 +0300 Subject: [PATCH] PY-80844 Disable the intention for converting f-strings into t-strings in Python < 3.14 GitOrigin-RevId: 97162fca82d36958d61641c56315295329018304 --- .../codeInsight/intentions/PyFStringToTStringIntention.kt | 7 +++---- .../notAvailableBefore314.py | 1 + .../python/intentions/PyFStringToTStringIntentionTest.java | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 python/testData/intentions/PyFStringToTStringIntentionTest/notAvailableBefore314.py diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyFStringToTStringIntention.kt b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyFStringToTStringIntention.kt index 7223cdc17134..d5c5f1e9fa3d 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyFStringToTStringIntention.kt +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyFStringToTStringIntention.kt @@ -8,16 +8,15 @@ import com.intellij.modcommand.PsiUpdateModCommandAction import com.intellij.openapi.editor.Document import com.intellij.psi.util.parentOfType import com.jetbrains.python.PyPsiBundle -import com.jetbrains.python.psi.PyDocStringOwner -import com.jetbrains.python.psi.PyFormattedStringElement -import com.jetbrains.python.psi.PyStringLiteralExpression -import com.jetbrains.python.psi.PyUtil +import com.jetbrains.python.psi.* /** * Intention to convert between f-strings and t-strings (PEP-750) */ class PyFStringToTStringIntention : PsiUpdateModCommandAction(PyFormattedStringElement::class.java) { override fun getPresentation(context: ActionContext, stringElement: PyFormattedStringElement): Presentation? { + if (LanguageLevel.forElement(stringElement).isOlderThan(LanguageLevel.PYTHON314)) return null + val stringLiteral = stringElement.getParent() as? PyStringLiteralExpression ?: return null val docStringOwner = stringLiteral.parentOfType() if (docStringOwner != null && docStringOwner.getDocStringExpression() === stringLiteral) return null diff --git a/python/testData/intentions/PyFStringToTStringIntentionTest/notAvailableBefore314.py b/python/testData/intentions/PyFStringToTStringIntentionTest/notAvailableBefore314.py new file mode 100644 index 000000000000..8db3005d3b7a --- /dev/null +++ b/python/testData/intentions/PyFStringToTStringIntentionTest/notAvailableBefore314.py @@ -0,0 +1 @@ +s = f"foo" \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyFStringToTStringIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyFStringToTStringIntentionTest.java index 7acd858c89ac..77e3c2c22560 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyFStringToTStringIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyFStringToTStringIntentionTest.java @@ -40,6 +40,10 @@ public class PyFStringToTStringIntentionTest extends PyIntentionTestCase { doFStringToTStringTest(); } + public void testNotAvailableBefore314() { + runWithLanguageLevel(LanguageLevel.PYTHON310, this::doNegativeTest); + } + private void doFStringToTStringTest() { doTest(PyPsiBundle.message("INTN.convert.f.string.to.t.string"), LanguageLevel.getLatest()); } @@ -49,6 +53,6 @@ public class PyFStringToTStringIntentionTest extends PyIntentionTestCase { } private void doNegativeTest() { - runWithLanguageLevel(LanguageLevel.getLatest(), () -> doNegativeTest(PyPsiBundle.message("INTN.convert.f.string.to.t.string"))); + doNegativeTest(PyPsiBundle.message("INTN.convert.f.string.to.t.string")); } }