Skip to content

gh-131421: Fix ASDL tests #133408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/test/test_asdl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ def test_product(self):
alias = self.types['alias']
self.assertEqual(
str(alias),
'Product([Field(identifier, name), Field(identifier, asname, opt=True)], '
'Product([Field(identifier, name), Field(identifier, asname, quantifiers=[OPTIONAL])], '
'[Field(int, lineno), Field(int, col_offset), '
'Field(int, end_lineno, opt=True), Field(int, end_col_offset, opt=True)])')
'Field(int, end_lineno, quantifiers=[OPTIONAL]), Field(int, end_col_offset, quantifiers=[OPTIONAL])])')

def test_attributes(self):
stmt = self.types['stmt']
self.assertEqual(len(stmt.attributes), 4)
self.assertEqual(repr(stmt.attributes[0]), 'Field(int, lineno)')
self.assertEqual(repr(stmt.attributes[1]), 'Field(int, col_offset)')
self.assertEqual(repr(stmt.attributes[2]), 'Field(int, end_lineno, opt=True)')
self.assertEqual(repr(stmt.attributes[3]), 'Field(int, end_col_offset, opt=True)')
self.assertEqual(repr(stmt.attributes[2]), 'Field(int, end_lineno, quantifiers=[OPTIONAL])')
self.assertEqual(repr(stmt.attributes[3]), 'Field(int, end_col_offset, quantifiers=[OPTIONAL])')

def test_constructor_fields(self):
ehandler = self.types['excepthandler']
Expand Down
20 changes: 12 additions & 8 deletions Parser/asdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,21 @@ def __str__(self):
return "{}{} {}".format(self.type, extra, self.name)

def __repr__(self):
extra = ""
for mod in self.quantifiers:
if mod is Quantifier.SEQUENCE:
extra += ", SEQUENCE"
elif mod is Quantifier.OPTIONAL:
extra += ", OPTIONAL"
if self.quantifiers:
texts = []
for mod in self.quantifiers:
if mod is Quantifier.SEQUENCE:
texts.append("SEQUENCE")
elif mod is Quantifier.OPTIONAL:
texts.append("OPTIONAL")
extra = ", quantifiers=[{}]".format(", ".join(texts))
else:
extra = ""

if self.name is None:
return 'Field({0.type}, quantifiers=[{1}])'.format(self, extra)
return 'Field({0.type}{1})'.format(self, extra)
else:
return 'Field({0.type}, {0.name}, quantifiers=[{1}])'.format(self, extra)
return 'Field({0.type}, {0.name}{1})'.format(self, extra)

class Sum(AST):
def __init__(self, types, attributes=None):
Expand Down
Loading