fixed PY-6283 Introscpection- "This dictionary creation could be rewritten as a dictionary literal" produces invalid code

This commit is contained in:
Ekaterina Tuzova
2012-04-11 16:04:09 +04:00
parent 214b8ca76a
commit 7fe3c39b63
4 changed files with 19 additions and 1 deletions
@@ -62,7 +62,18 @@ public class DictCreationQuickFix implements LocalQuickFix {
final PySubscriptionExpression subscription = (PySubscriptionExpression)targetToValue.first;
final PyExpression indexExpression = subscription.getIndexExpression();
assert indexExpression != null;
statements.add(indexExpression.getText() + ": " + targetToValue.second.getText());
final String indexText;
if (indexExpression instanceof PyTupleExpression)
indexText = "("+indexExpression.getText()+")";
else
indexText = indexExpression.getText();
final String valueText;
if (targetToValue.second instanceof PyTupleExpression)
valueText = "("+targetToValue.second.getText()+")";
else
valueText = targetToValue.second.getText();
statements.add(indexText + ": " + valueText);
statement.delete();
}
statement = nextStatement;
@@ -0,0 +1,2 @@
<caret><warning descr="This dictionary creation could be rewritten as a dictionary literal">dict = {}</warning>
dict[1,2] = 3,4
@@ -0,0 +1 @@
dict = {(1, 2): (3, 4)}
@@ -123,6 +123,10 @@ public class PyQuickFixTest extends PyTestCase {
doInspectionTest("DictCreation.py", PyDictCreationInspection.class, PyBundle.message("QFIX.dict.creation"), true, true);
}
public void testDictCreationTuple() { //PY-6283
doInspectionTest("DictCreationTuple.py", PyDictCreationInspection.class, PyBundle.message("QFIX.dict.creation"), true, true);
}
public void testTransformClassicClass() {
doInspectionTest("TransformClassicClass.py", PyClassicStyleClassInspection.class,
PyBundle.message("QFIX.classic.class.transform"), true, true);