Skip to content

Commit 8d07c45

Browse files
authored
Populate _field_types with real type objects (RustPython#6981)
- Add FieldType enum and FIELD_TYPES static table mapping all AST node classes to their ASDL field types - Resolve markers to real Python type objects (GenericAlias, Union, plain types) at module init in populate_field_types() - Set class-level None defaults for optional fields - Replace hardcoded LIST_FIELDS and class-name checks in slot_init with _field_types-based lookup - Add expr_context default to Load(), fix ImportFrom.level default (now None instead of 0) - Fix __class_getitem__ None check, compile() formatting - Remove 14 @expectedfailure decorators from test_ast
1 parent 27d70fd commit 8d07c45

3 files changed

Lines changed: 642 additions & 79 deletions

File tree

Lib/test/test_ast/test_ast.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,6 @@ def test_parse_in_error(self):
14801480
ast.literal_eval(r"'\U'")
14811481
self.assertIsNotNone(e.exception.__context__)
14821482

1483-
@unittest.expectedFailure # TODO: RUSTPYTHON; + Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')]))])
14841483
def test_dump(self):
14851484
node = ast.parse('spam(eggs, "and cheese")')
14861485
self.assertEqual(ast.dump(node),
@@ -1501,7 +1500,6 @@ def test_dump(self):
15011500
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)])"
15021501
)
15031502

1504-
@unittest.expectedFailure # TODO: RUSTPYTHON; - type_ignores=[])
15051503
def test_dump_indent(self):
15061504
node = ast.parse('spam(eggs, "and cheese")')
15071505
self.assertEqual(ast.dump(node, indent=3), """\
@@ -1557,7 +1555,6 @@ def test_dump_indent(self):
15571555
end_lineno=1,
15581556
end_col_offset=24)])""")
15591557

1560-
@unittest.expectedFailure # TODO: RUSTPYTHON; + Raise()
15611558
def test_dump_incomplete(self):
15621559
node = ast.Raise(lineno=3, col_offset=4)
15631560
self.assertEqual(ast.dump(node),
@@ -1622,7 +1619,6 @@ def test_dump_incomplete(self):
16221619
"ClassDef('T', [], [keyword('a', Constant(None))], [], [Name('dataclass', Load())])",
16231620
)
16241621

1625-
@unittest.expectedFailure # TODO: RUSTPYTHON
16261622
def test_dump_show_empty(self):
16271623
def check_node(node, empty, full, **kwargs):
16281624
with self.subTest(show_empty=False):
@@ -1743,7 +1739,6 @@ def test_copy_location(self):
17431739
self.assertEqual(new.lineno, 1)
17441740
self.assertEqual(new.col_offset, 1)
17451741

1746-
@unittest.expectedFailure # TODO: RUSTPYTHON; + Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, end_col_offset=12)], lineno=1, col_offset=0, end_lineno=1, end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), args=[Constant(value='eggs', lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)], lineno=1, col_offset=0, end_lineno=1, end_col_offset=0), lineno=1, col_offset=0, end_lineno=1, end_col_offset=0)])
17471742
def test_fix_missing_locations(self):
17481743
src = ast.parse('write("spam")')
17491744
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
@@ -1807,7 +1802,6 @@ def test_iter_fields(self):
18071802
self.assertEqual(d.pop('func').id, 'foo')
18081803
self.assertEqual(d, {'keywords': [], 'args': []})
18091804

1810-
@unittest.expectedFailure # TODO: RUSTPYTHON; + keyword(arg='eggs', value=Constant(value='leek'))
18111805
def test_iter_child_nodes(self):
18121806
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
18131807
self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
@@ -3149,7 +3143,6 @@ def assertASTTransformation(self, transformer_class,
31493143

31503144
self.assertASTEqual(result_ast, expected_ast)
31513145

3152-
@unittest.expectedFailure # TODO: RUSTPYTHON; <class 'object'> is not <class 'NoneType'>
31533146
def test_node_remove_single(self):
31543147
code = 'def func(arg) -> SomeType: ...'
31553148
expected = 'def func(arg): ...'
@@ -3376,7 +3369,6 @@ class BadFields(ast.AST):
33763369
with self.assertWarnsRegex(DeprecationWarning, r"Field b'\\xff\\xff.*' .*"):
33773370
obj = BadFields()
33783371

3379-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: None != []
33803372
def test_complete_field_types(self):
33813373
class _AllFieldTypes(ast.AST):
33823374
_fields = ('a', 'b')
@@ -3569,7 +3561,6 @@ def test_single_mode_flag(self):
35693561
with self.subTest(flag=flag):
35703562
self.check_output(source, expect, flag)
35713563

3572-
@unittest.expectedFailure # TODO: RUSTPYTHON
35733564
def test_eval_mode_flag(self):
35743565
# test 'python -m ast -m/--mode eval'
35753566
source = 'print(1, 2, 3)'
@@ -3604,7 +3595,6 @@ def test_func_type_mode_flag(self):
36043595
with self.subTest(flag=flag):
36053596
self.check_output(source, expect, flag)
36063597

3607-
@unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: type object '_ast.Module' has no attribute '_field_types'
36083598
def test_no_type_comments_flag(self):
36093599
# test 'python -m ast --no-type-comments'
36103600
source = 'x: bool = 1 # type: ignore[assignment]'
@@ -3619,7 +3609,6 @@ def test_no_type_comments_flag(self):
36193609
'''
36203610
self.check_output(source, expect, '--no-type-comments')
36213611

3622-
@unittest.expectedFailure # TODO: RUSTPYTHON
36233612
def test_include_attributes_flag(self):
36243613
# test 'python -m ast -a/--include-attributes'
36253614
source = 'pass'
@@ -3636,7 +3625,6 @@ def test_include_attributes_flag(self):
36363625
with self.subTest(flag=flag):
36373626
self.check_output(source, expect, flag)
36383627

3639-
@unittest.expectedFailure # TODO: RUSTPYTHON
36403628
def test_indent_flag(self):
36413629
# test 'python -m ast -i/--indent 0'
36423630
source = 'pass'
@@ -3673,7 +3661,6 @@ def test_feature_version_flag(self):
36733661
with self.assertRaises(SyntaxError):
36743662
self.invoke_ast('--feature-version=3.9')
36753663

3676-
@unittest.expectedFailure # TODO: RUSTPYTHON
36773664
def test_no_optimize_flag(self):
36783665
# test 'python -m ast -O/--optimize -1/0'
36793666
source = '''
@@ -3724,7 +3711,6 @@ def test_optimize_flag(self):
37243711
with self.subTest(flag=flag):
37253712
self.check_output(source, expect, flag)
37263713

3727-
@unittest.expectedFailure # TODO: RUSTPYTHON; type_ignores=[])
37283714
def test_show_empty_flag(self):
37293715
# test 'python -m ast --show-empty'
37303716
source = 'print(1, 2, 3)'

0 commit comments

Comments
 (0)