From bd939d2579f47a7c08910fcbb5ddfc81dc108284 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Thu, 22 Mar 2018 15:35:08 -0400 Subject: [PATCH] Fix Python method calls. `identifier` in the Python assignment module was too eager to reduce a nested Attribute into a dotted identifier. Removing it fixes method calls, which now have a unit test. --- src/Language/Python/Assignment.hs | 3 --- test/Analysis/Python/Spec.hs | 7 +++++- test/fixtures/python/analysis/subclass.py | 9 ++++++++ test/fixtures/python/call.diffA-B.txt | 4 +++- test/fixtures/python/call.diffB-A.txt | 4 +++- test/fixtures/python/call.parseB.txt | 4 +++- test/fixtures/python/import-graph/main.json | 24 ++++++++++++++------- test/fixtures/python/with.diffA-B.txt | 8 +++++-- test/fixtures/python/with.diffB-A.txt | 8 +++++-- test/fixtures/python/with.parseB.txt | 8 +++++-- 10 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/python/analysis/subclass.py diff --git a/src/Language/Python/Assignment.hs b/src/Language/Python/Assignment.hs index c606aab56..3feb47e8a 100644 --- a/src/Language/Python/Assignment.hs +++ b/src/Language/Python/Assignment.hs @@ -340,12 +340,9 @@ yield = makeTerm <$> symbol Yield <*> (Statement.Yield <$> children (term ( expr -- Identifiers and qualified identifiers (e.g. `a.b.c`) from things like DottedName and Attribute identifier :: Assignment identifier = makeTerm <$> (symbol Identifier <|> symbol Identifier') <*> (Syntax.Identifier <$> (name <$> source)) - <|> makeQualifiedIdentifier <$> symbol Attribute <*> children (attribute <|> identifierPair) <|> makeQualifiedIdentifier <$> symbol DottedName <*> children (some identifier') <|> symbol DottedName *> children identifier where - attribute = (\a b -> a <> [b]) <$> (symbol Attribute *> children (attribute <|> identifierPair)) <*> identifier' - identifierPair = (\a b -> [a, b]) <$> identifier' <*> identifier' identifier' = (symbol Identifier <|> symbol Identifier') *> source makeQualifiedIdentifier loc xs = makeTerm loc (Syntax.Identifier (qualifiedName xs)) diff --git a/test/Analysis/Python/Spec.hs b/test/Analysis/Python/Spec.hs index 447a4c567..175b58207 100644 --- a/test/Analysis/Python/Spec.hs +++ b/test/Analysis/Python/Spec.hs @@ -34,10 +34,15 @@ spec = parallel $ do ] env `shouldBe` expectedEnv + it "subclasses" $ do + res <- evaluate' "subclass.py" + fst res `shouldBe` Right (injValue (String "\"bar\"")) + where addr = Address . Precise fixtures = "test/fixtures/python/analysis/" - evaluate entry = snd . fst . fst . fst . fst <$> + evaluate entry = snd <$> evaluate' entry + evaluate' entry = fst . fst . fst . fst <$> evaluateFiles pythonParser [ fixtures <> entry , fixtures <> "a.py" diff --git a/test/fixtures/python/analysis/subclass.py b/test/fixtures/python/analysis/subclass.py new file mode 100644 index 000000000..6c4f4a186 --- /dev/null +++ b/test/fixtures/python/analysis/subclass.py @@ -0,0 +1,9 @@ +class Foo(): + def dang(): + return "foo" + +class Bar(): + def dang(): + return "bar" + +Bar.dang() diff --git a/test/fixtures/python/call.diffA-B.txt b/test/fixtures/python/call.diffA-B.txt index 231b2dd57..b75a5a434 100644 --- a/test/fixtures/python/call.diffA-B.txt +++ b/test/fixtures/python/call.diffA-B.txt @@ -14,6 +14,8 @@ {+(Identifier)+} (Empty)) {+(Call - {+(Identifier)+} + {+(MemberAccess + {+(Identifier)+} + {+(Identifier)+})+} {+(Integer)+} {+(Empty)+})+}) diff --git a/test/fixtures/python/call.diffB-A.txt b/test/fixtures/python/call.diffB-A.txt index d76bba421..c86cb65b9 100644 --- a/test/fixtures/python/call.diffB-A.txt +++ b/test/fixtures/python/call.diffB-A.txt @@ -14,6 +14,8 @@ {+(Identifier)+} (Empty)) {-(Call - {-(Identifier)-} + {-(MemberAccess + {-(Identifier)-} + {-(Identifier)-})-} {-(Integer)-} {-(Empty)-})-}) diff --git a/test/fixtures/python/call.parseB.txt b/test/fixtures/python/call.parseB.txt index bb22d2b96..64a3a2d9f 100644 --- a/test/fixtures/python/call.parseB.txt +++ b/test/fixtures/python/call.parseB.txt @@ -12,6 +12,8 @@ (Identifier) (Empty)) (Call - (Identifier) + (MemberAccess + (Identifier) + (Identifier)) (Integer) (Empty))) diff --git a/test/fixtures/python/import-graph/main.json b/test/fixtures/python/import-graph/main.json index 301fa20e2..445fe55eb 100644 --- a/test/fixtures/python/import-graph/main.json +++ b/test/fixtures/python/import-graph/main.json @@ -269,8 +269,10 @@ 22 ] }, - "symbol": "os.getcwd", - "targets": [] + "symbol": "getcwd", + "targets": [ + "os" + ] }, { "span": { @@ -283,8 +285,10 @@ 31 ] }, - "symbol": "np.array", - "targets": [] + "symbol": "array", + "targets": [ + "np" + ] }, { "span": { @@ -297,8 +301,10 @@ 20 ] }, - "symbol": "x.sum", - "targets": [] + "symbol": "sum", + "targets": [ + "x" + ] }, { "span": { @@ -311,8 +317,10 @@ 19 ] }, - "symbol": "x.sum", - "targets": [] + "symbol": "sum", + "targets": [ + "x" + ] }, { "span": { diff --git a/test/fixtures/python/with.diffA-B.txt b/test/fixtures/python/with.diffA-B.txt index 5de9c62c5..cbe67a91b 100644 --- a/test/fixtures/python/with.diffA-B.txt +++ b/test/fixtures/python/with.diffA-B.txt @@ -25,7 +25,9 @@ {+(Let {+(Empty)+} {+(Call - {+(Identifier)+} + {+(MemberAccess + {+(Identifier)+} + {+(Identifier)+})+} {+(Identifier)+} {+(TextElement)+} {+(Identifier)+} @@ -34,7 +36,9 @@ {+(Let {+(Empty)+} {+(Call - {+(Identifier)+} + {+(MemberAccess + {+(Identifier)+} + {+(Identifier)+})+} {+(Identifier)+} {+(TextElement)+} {+(Identifier)+} diff --git a/test/fixtures/python/with.diffB-A.txt b/test/fixtures/python/with.diffB-A.txt index d9933850b..66585dba9 100644 --- a/test/fixtures/python/with.diffB-A.txt +++ b/test/fixtures/python/with.diffB-A.txt @@ -25,7 +25,9 @@ {-(Let {-(Empty)-} {-(Call - {-(Identifier)-} + {-(MemberAccess + {-(Identifier)-} + {-(Identifier)-})-} {-(Identifier)-} {-(TextElement)-} {-(Identifier)-} @@ -34,7 +36,9 @@ {-(Let {-(Empty)-} {-(Call - {-(Identifier)-} + {-(MemberAccess + {-(Identifier)-} + {-(Identifier)-})-} {-(Identifier)-} {-(TextElement)-} {-(Identifier)-} diff --git a/test/fixtures/python/with.parseB.txt b/test/fixtures/python/with.parseB.txt index 9f7faafa8..44dbdfaee 100644 --- a/test/fixtures/python/with.parseB.txt +++ b/test/fixtures/python/with.parseB.txt @@ -12,7 +12,9 @@ (Let (Empty) (Call - (Identifier) + (MemberAccess + (Identifier) + (Identifier)) (Identifier) (TextElement) (Identifier) @@ -21,7 +23,9 @@ (Let (Empty) (Call - (Identifier) + (MemberAccess + (Identifier) + (Identifier)) (Identifier) (TextElement) (Identifier)