From 0ab6f65d37bf9629d7fdffed62fd589e334d9b78 Mon Sep 17 00:00:00 2001 From: Valentina Kiryushkina Date: Fri, 5 May 2017 20:18:00 +0300 Subject: [PATCH] Fix test for packing generalization introduced in python 3.5: remove unreal test cases, fix false positives for packed reference and call expressions --- .../PySubstitutionChunkReference.java | 22 +++++++++++++++---- ...tWithPackedDictCallInsideDictLiteralArg.py | 2 -- ...thPackedDictLiteralInsideDictLiteralArg.py | 2 -- .../IndexElementWithPackedReferenceExpr.py | 3 +++ .../PackedDictCallInsideDictLiteral.py | 4 ++-- .../PackedDictLiteralInsideDictLiteral.py | 3 ++- .../PackedRefInsideList.py | 8 +++++++ .../PackedRefInsideTuple.py | 8 +++++++ .../Py3StringFormatInspectionTest.java | 14 +++++++----- 9 files changed, 50 insertions(+), 16 deletions(-) delete mode 100644 python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictCallInsideDictLiteralArg.py delete mode 100644 python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictLiteralInsideDictLiteralArg.py create mode 100644 python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedReferenceExpr.py create mode 100644 python/testData/inspections/PyStringFormatInspection/PackedRefInsideList.py create mode 100644 python/testData/inspections/PyStringFormatInspection/PackedRefInsideTuple.py diff --git a/python/src/com/jetbrains/python/codeInsight/PySubstitutionChunkReference.java b/python/src/com/jetbrains/python/codeInsight/PySubstitutionChunkReference.java index b61bf7f78491..8330638fd498 100644 --- a/python/src/com/jetbrains/python/codeInsight/PySubstitutionChunkReference.java +++ b/python/src/com/jetbrains/python/codeInsight/PySubstitutionChunkReference.java @@ -235,10 +235,17 @@ public class PySubstitutionChunkReference extends PsiReferenceBase expr = getElementFromDictLiteral((PyDictLiteralExpression)underStarExpr, index); + allKeysForSure = expr != null; + if (expr != null && !expr.isNull()) return expr; } else if (underStarExpr instanceof PyCallExpression) { - return getElementFromCallExpression((PyCallExpression)underStarExpr, index.toString()); + Ref expr = getElementFromCallExpression((PyCallExpression)underStarExpr, index.toString()); + allKeysForSure = expr != null; + if (expr != null && !expr.isNull()) return expr; + } + else { + allKeysForSure = false; } } } @@ -462,10 +469,17 @@ public class PySubstitutionChunkReference extends PsiReferenceBase element = getElementFromDictLiteral(underStarExpr, mappingKey); + allKeysForSure = element != null; + if (element != null && !element.isNull()) return element; } else if (underStarExpr instanceof PyCallExpression) { - return getElementFromCallExpression((PyCallExpression)underStarExpr, mappingKey); + Ref element = getElementFromCallExpression((PyCallExpression)underStarExpr, mappingKey); + allKeysForSure = element != null; + if (element != null && !element.isNull()) return element; + } + else { + allKeysForSure = false; } } } diff --git a/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictCallInsideDictLiteralArg.py b/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictCallInsideDictLiteralArg.py deleted file mode 100644 index 0d6604ab910e..000000000000 --- a/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictCallInsideDictLiteralArg.py +++ /dev/null @@ -1,2 +0,0 @@ -"{d[a]}".format(d={"b": 1, **dict(a=2)}) -"{d[c]}".format(d={"b": 1, **dict(a=2)}) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictLiteralInsideDictLiteralArg.py b/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictLiteralInsideDictLiteralArg.py deleted file mode 100644 index 49ba39c82b9d..000000000000 --- a/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedDictLiteralInsideDictLiteralArg.py +++ /dev/null @@ -1,2 +0,0 @@ -"{d[1]}".format(d={2: a, **{1: b}}) -"{d[3]}".format(d={2: a, **{1: b}}) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedReferenceExpr.py b/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedReferenceExpr.py new file mode 100644 index 000000000000..04616f3f674d --- /dev/null +++ b/python/testData/inspections/PyStringFormatInspection/IndexElementWithPackedReferenceExpr.py @@ -0,0 +1,3 @@ +old_dict = {1: "b"} +"{d[2]}".format(d={**old_dict, 2: "a"}) +"{d[1]}".format(d={**old_dict, 2: "a"}) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/PackedDictCallInsideDictLiteral.py b/python/testData/inspections/PyStringFormatInspection/PackedDictCallInsideDictLiteral.py index c37cc8f586d5..d668608472ba 100644 --- a/python/testData/inspections/PyStringFormatInspection/PackedDictCallInsideDictLiteral.py +++ b/python/testData/inspections/PyStringFormatInspection/PackedDictCallInsideDictLiteral.py @@ -1,2 +1,2 @@ -"{b}".format(**{"a": 1, **{"b": 2}}) -"{c}".format(**{"a": 1, **{"b": 2}}) \ No newline at end of file +"{b}".format(**{"a": 1, **dict(b=2)}) +"{c}".format(**{"a": 1, **dict(b=2)}) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/PackedDictLiteralInsideDictLiteral.py b/python/testData/inspections/PyStringFormatInspection/PackedDictLiteralInsideDictLiteral.py index aa78e12175e6..8c4b09cea145 100644 --- a/python/testData/inspections/PyStringFormatInspection/PackedDictLiteralInsideDictLiteral.py +++ b/python/testData/inspections/PyStringFormatInspection/PackedDictLiteralInsideDictLiteral.py @@ -1,2 +1,3 @@ "{b}".format(**{"a": 1, **{"b":2}}) -"{c}".format(**{"a": 1, **{"b":2}}) \ No newline at end of file +"{c}".format(**{**{"b":2}, "a": 1, **{"c":2}}) +"{d}".format(**{**{"b":2}, "a": 1, **{"c":2}}) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/PackedRefInsideList.py b/python/testData/inspections/PyStringFormatInspection/PackedRefInsideList.py new file mode 100644 index 000000000000..f2957c243618 --- /dev/null +++ b/python/testData/inspections/PyStringFormatInspection/PackedRefInsideList.py @@ -0,0 +1,8 @@ +list = [3, 4] +"{3}".format(*[1, 2, *list]) +"{4}".format(*[1, 2, *list]) +"{1}".format(*[1, 2, *list]) + +"{3}".format(*[*list, 1, 2]) +"{4}".format(*[*list, 1, 2]) +"{1}".format(*[*list, 1, 2]) \ No newline at end of file diff --git a/python/testData/inspections/PyStringFormatInspection/PackedRefInsideTuple.py b/python/testData/inspections/PyStringFormatInspection/PackedRefInsideTuple.py new file mode 100644 index 000000000000..8fbff7e9c747 --- /dev/null +++ b/python/testData/inspections/PyStringFormatInspection/PackedRefInsideTuple.py @@ -0,0 +1,8 @@ +tuple = (3, 4) +"{3}".format(*[1, 2, *tuple]) +"{4}".format(*[1, 2, *tuple]) +"{1}".format(*[1, 2, *tuple]) + +"{3}".format(*[*tuple, 1, 2]) +"{4}".format(*[*tuple, 1, 2]) +"{1}".format(*[*tuple, 1, 2]) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/Py3StringFormatInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/Py3StringFormatInspectionTest.java index cdf4a5586373..242f2d9a4671 100644 --- a/python/testSrc/com/jetbrains/python/inspections/Py3StringFormatInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/Py3StringFormatInspectionTest.java @@ -37,11 +37,7 @@ public class Py3StringFormatInspectionTest extends PyTestCase { runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); } - public void testIndexElementWithPackedDictLiteralInsideDictLiteralArg() { - runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); - } - - public void testIndexElementWithPackedDictCallInsideDictLiteralArg() { + public void testIndexElementWithPackedReferenceExpr() { runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); } @@ -69,6 +65,14 @@ public class Py3StringFormatInspectionTest extends PyTestCase { runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); } + public void testPackedRefInsideList() { + runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); + } + + public void testPackedRefInsideTuple() { + runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest); + } + // PY-20599 public void testPy3kAsciiFormatSpecifier() { runWithLanguageLevel(LanguageLevel.PYTHON35, () -> doTest());