Skip to content

Commit 107a2c5

Browse files
authored
bpo-40528: fix is_simple(sum)s behavior for attributes (GH-26918)
This is something I noticed while (now discontinued) experimenting with the idea of annotating operators with location information. Unfortunately without this addition, adding any `attributes` to stuff like `unaryop` doesn't change anything since the code assumes they are singletons and caches all instances. This patch fixes this assumption with including the attributes as well as constructor fields.
1 parent 9eea201 commit 107a2c5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Parser/asdl_c.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,20 @@ def reflow_lines(s, depth):
7171
def reflow_c_string(s, depth):
7272
return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE))
7373

74-
def is_simple(sum):
74+
def is_simple(sum_type):
7575
"""Return True if a sum is a simple.
7676
77-
A sum is simple if its types have no fields, e.g.
77+
A sum is simple if it's types have no fields and itself
78+
doesn't have any attributes. Instances of these types are
79+
cached at C level, and they act like singletons when propagating
80+
parser generated nodes into Python level, e.g.
7881
unaryop = Invert | Not | UAdd | USub
7982
"""
80-
for t in sum.types:
81-
if t.fields:
82-
return False
83-
return True
83+
84+
return not (
85+
sum_type.attributes or
86+
any(constructor.fields for constructor in sum_type.types)
87+
)
8488

8589
def asdl_of(name, obj):
8690
if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor):

0 commit comments

Comments
 (0)