Skip to content

ast.class._field_types causes Exception when ast.class._fields does not cause Exception #135797

Open

Description

@hunterhogan

Bug report

Bug description:

_field and _field_types behave differently

Accessing the _field_types attribute of an ast.AST subclass will cause an exception if the class has no _fields, but accessing the _fields attribute never causes an exception.

Example: ast.mod

(.venv) C:\clones\cpython>py
Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.mod._fields
()
>>> ast.mod._field_types
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    ast.mod._field_types
AttributeError: type object 'mod' has no attribute '_field_types'
>>> 

_field and _field_types for all ast.AST subclasses

Details

(.venv) C:\clones\cpython>C:\clones\cpython\.venv\Scripts\python.exe c:/clones/cpython/field_types.py
3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)]

astClass._fields
AST ()
mod ()
stmt ()
expr ()
expr_context ()
boolop ()
operator ()
unaryop ()
cmpop ()
comprehension ('target', 'iter', 'ifs', 'is_async')
excepthandler ()
arguments ('posonlyargs', 'args', 'vararg', 'kwonlyargs', 'kw_defaults', 'kwarg', 'defaults')
arg ('arg', 'annotation', 'type_comment')
keyword ('arg', 'value')
alias ('name', 'asname')
withitem ('context_expr', 'optional_vars')
match_case ('pattern', 'guard', 'body')
pattern ()
type_ignore ()
type_param ()
slice ()
Num ('n',)
Str ('s',)
Bytes ('s',)
NameConstant ('value', 'kind')
Ellipsis ()
Module ('body', 'type_ignores')
Interactive ('body',)
Expression ('body',)
FunctionType ('argtypes', 'returns')
Suite ()
FunctionDef ('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment', 'type_params')
AsyncFunctionDef ('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment', 'type_params')
ClassDef ('name', 'bases', 'keywords', 'body', 'decorator_list', 'type_params')
Return ('value',)
Delete ('targets',)
Assign ('targets', 'value', 'type_comment')
TypeAlias ('name', 'type_params', 'value')
AugAssign ('target', 'op', 'value')
AnnAssign ('target', 'annotation', 'value', 'simple')
For ('target', 'iter', 'body', 'orelse', 'type_comment')
AsyncFor ('target', 'iter', 'body', 'orelse', 'type_comment')
While ('test', 'body', 'orelse')
If ('test', 'body', 'orelse')
With ('items', 'body', 'type_comment')
AsyncWith ('items', 'body', 'type_comment')
Match ('subject', 'cases')
Raise ('exc', 'cause')
Try ('body', 'handlers', 'orelse', 'finalbody')
TryStar ('body', 'handlers', 'orelse', 'finalbody')
Assert ('test', 'msg')
Import ('names',)
ImportFrom ('module', 'names', 'level')
Global ('names',)
Nonlocal ('names',)
Expr ('value',)
Pass ()
Break ()
Continue ()
BoolOp ('op', 'values')
NamedExpr ('target', 'value')
BinOp ('left', 'op', 'right')
UnaryOp ('op', 'operand')
Lambda ('args', 'body')
IfExp ('test', 'body', 'orelse')
Dict ('keys', 'values')
Set ('elts',)
ListComp ('elt', 'generators')
SetComp ('elt', 'generators')
DictComp ('key', 'value', 'generators')
GeneratorExp ('elt', 'generators')
Await ('value',)
Yield ('value',)
YieldFrom ('value',)
Compare ('left', 'ops', 'comparators')
Call ('func', 'args', 'keywords')
FormattedValue ('value', 'conversion', 'format_spec')
JoinedStr ('values',)
Constant ('value', 'kind')
Attribute ('value', 'attr', 'ctx')
Subscript ('value', 'slice', 'ctx')
Starred ('value', 'ctx')
Name ('id', 'ctx')
List ('elts', 'ctx')
Tuple ('elts', 'ctx')
Slice ('lower', 'upper', 'step')
Load ()
Store ()
Del ()
AugLoad ()
AugStore ()
Param ()
And ()
Or ()
Add ()
Sub ()
Mult ()
MatMult ()
Div ()
Mod ()
Pow ()
LShift ()
RShift ()
BitOr ()
BitXor ()
BitAnd ()
FloorDiv ()
Invert ()
Not ()
UAdd ()
USub ()
Eq ()
NotEq ()
Lt ()
LtE ()
Gt ()
GtE ()
Is ()
IsNot ()
In ()
NotIn ()
ExceptHandler ('type', 'name', 'body')
MatchValue ('value',)
MatchSingleton ('value',)
MatchSequence ('patterns',)
MatchMapping ('keys', 'patterns', 'rest')
MatchClass ('cls', 'patterns', 'kwd_attrs', 'kwd_patterns')
MatchStar ('name',)
MatchAs ('pattern', 'name')
MatchOr ('patterns',)
TypeIgnore ('lineno', 'tag')
TypeVar ('name', 'bound', 'default_value')
ParamSpec ('name', 'default_value')
TypeVarTuple ('name', 'default_value')
Index ()
ExtSlice ()
countExceptions = 0

astClass._field_types
AttributeError: AST
AttributeError: mod
AttributeError: stmt
AttributeError: expr
AttributeError: expr_context
AttributeError: boolop
AttributeError: operator
AttributeError: unaryop
AttributeError: cmpop
comprehension {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'ifs': list[ast.expr], 'is_async': <class 'int'>}
AttributeError: excepthandler
arguments {'posonlyargs': list[ast.arg], 'args': list[ast.arg], 'vararg': ast.arg | None, 'kwonlyargs': list[ast.arg], 'kw_defaults': list[ast.expr], 'kwarg': ast.arg | None, 'defaults': list[ast.expr]}
arg {'arg': <class 'str'>, 'annotation': ast.expr | None, 'type_comment': str | None}
keyword {'arg': str | None, 'value': <class 'ast.expr'>}
alias {'name': <class 'str'>, 'asname': str | None}
withitem {'context_expr': <class 'ast.expr'>, 'optional_vars': ast.expr | None}
match_case {'pattern': <class 'ast.pattern'>, 'guard': ast.expr | None, 'body': list[ast.stmt]}
AttributeError: pattern
AttributeError: type_ignore
AttributeError: type_param
AttributeError: slice
Num {'value': <class 'object'>, 'kind': str | None}
Str {'value': <class 'object'>, 'kind': str | None}
Bytes {'value': <class 'object'>, 'kind': str | None}
NameConstant {'value': <class 'object'>, 'kind': str | None}
Ellipsis {'value': <class 'object'>, 'kind': str | None}
Module {'body': list[ast.stmt], 'type_ignores': list[ast.type_ignore]}
Interactive {'body': list[ast.stmt]}
Expression {'body': <class 'ast.expr'>}
FunctionType {'argtypes': list[ast.expr], 'returns': <class 'ast.expr'>}
AttributeError: Suite
FunctionDef {'name': <class 'str'>, 'args': <class 'ast.arguments'>, 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'returns': ast.expr | None, 'type_comment': str | None, 'type_params': list[ast.type_param]}
AsyncFunctionDef {'name': <class 'str'>, 'args': <class 'ast.arguments'>, 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'returns': ast.expr | None, 'type_comment': str | None, 'type_params': list[ast.type_param]}
ClassDef {'name': <class 'str'>, 'bases': list[ast.expr], 'keywords': list[ast.keyword], 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'type_params': list[ast.type_param]}
Return {'value': ast.expr | None}
Delete {'targets': list[ast.expr]}
Assign {'targets': list[ast.expr], 'value': <class 'ast.expr'>, 'type_comment': str | None}
TypeAlias {'name': <class 'ast.expr'>, 'type_params': list[ast.type_param], 'value': <class 'ast.expr'>}
AugAssign {'target': <class 'ast.expr'>, 'op': <class 'ast.operator'>, 'value': <class 'ast.expr'>}
AnnAssign {'target': <class 'ast.expr'>, 'annotation': <class 'ast.expr'>, 'value': ast.expr | None, 'simple': <class 'int'>}
For {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt], 'type_comment': str | None}
AsyncFor {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt], 'type_comment': str | None}
While {'test': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt]}
If {'test': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt]}
With {'items': list[ast.withitem], 'body': list[ast.stmt], 'type_comment': str | None}
AsyncWith {'items': list[ast.withitem], 'body': list[ast.stmt], 'type_comment': str | None}
Match {'subject': <class 'ast.expr'>, 'cases': list[ast.match_case]}
Raise {'exc': ast.expr | None, 'cause': ast.expr | None}
Try {'body': list[ast.stmt], 'handlers': list[ast.excepthandler], 'orelse': list[ast.stmt], 'finalbody': list[ast.stmt]}
TryStar {'body': list[ast.stmt], 'handlers': list[ast.excepthandler], 'orelse': list[ast.stmt], 'finalbody': list[ast.stmt]}
Assert {'test': <class 'ast.expr'>, 'msg': ast.expr | None}
Import {'names': list[ast.alias]}
ImportFrom {'module': str | None, 'names': list[ast.alias], 'level': int | None}
Global {'names': list[str]}
Nonlocal {'names': list[str]}
Expr {'value': <class 'ast.expr'>}
Pass {}
Break {}
Continue {}
BoolOp {'op': <class 'ast.boolop'>, 'values': list[ast.expr]}
NamedExpr {'target': <class 'ast.expr'>, 'value': <class 'ast.expr'>}
BinOp {'left': <class 'ast.expr'>, 'op': <class 'ast.operator'>, 'right': <class 'ast.expr'>}
UnaryOp {'op': <class 'ast.unaryop'>, 'operand': <class 'ast.expr'>}
Lambda {'args': <class 'ast.arguments'>, 'body': <class 'ast.expr'>}
IfExp {'test': <class 'ast.expr'>, 'body': <class 'ast.expr'>, 'orelse': <class 'ast.expr'>}
Dict {'keys': list[ast.expr], 'values': list[ast.expr]}
Set {'elts': list[ast.expr]}
ListComp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
SetComp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
DictComp {'key': <class 'ast.expr'>, 'value': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
GeneratorExp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
Await {'value': <class 'ast.expr'>}
Yield {'value': ast.expr | None}
YieldFrom {'value': <class 'ast.expr'>}
Compare {'left': <class 'ast.expr'>, 'ops': list[ast.cmpop], 'comparators': list[ast.expr]}
Call {'func': <class 'ast.expr'>, 'args': list[ast.expr], 'keywords': list[ast.keyword]}
FormattedValue {'value': <class 'ast.expr'>, 'conversion': <class 'int'>, 'format_spec': ast.expr | None}
JoinedStr {'values': list[ast.expr]}
Constant {'value': <class 'object'>, 'kind': str | None}
Attribute {'value': <class 'ast.expr'>, 'attr': <class 'str'>, 'ctx': <class 'ast.expr_context'>}
Subscript {'value': <class 'ast.expr'>, 'slice': <class 'ast.expr'>, 'ctx': <class 'ast.expr_context'>}
Starred {'value': <class 'ast.expr'>, 'ctx': <class 'ast.expr_context'>}
Name {'id': <class 'str'>, 'ctx': <class 'ast.expr_context'>}
List {'elts': list[ast.expr], 'ctx': <class 'ast.expr_context'>}
Tuple {'elts': list[ast.expr], 'ctx': <class 'ast.expr_context'>}
Slice {'lower': ast.expr | None, 'upper': ast.expr | None, 'step': ast.expr | None}
Load {}
Store {}
Del {}
AttributeError: AugLoad
AttributeError: AugStore
AttributeError: Param
And {}
Or {}
Add {}
Sub {}
Mult {}
MatMult {}
Div {}
Mod {}
Pow {}
LShift {}
RShift {}
BitOr {}
BitXor {}
BitAnd {}
FloorDiv {}
Invert {}
Not {}
UAdd {}
USub {}
Eq {}
NotEq {}
Lt {}
LtE {}
Gt {}
GtE {}
Is {}
IsNot {}
In {}
NotIn {}
ExceptHandler {'type': ast.expr | None, 'name': str | None, 'body': list[ast.stmt]}
MatchValue {'value': <class 'ast.expr'>}
MatchSingleton {'value': <class 'object'>}
MatchSequence {'patterns': list[ast.pattern]}
MatchMapping {'keys': list[ast.expr], 'patterns': list[ast.pattern], 'rest': str | None}
MatchClass {'cls': <class 'ast.expr'>, 'patterns': list[ast.pattern], 'kwd_attrs': list[str], 'kwd_patterns': list[ast.pattern]}
MatchStar {'name': str | None}
MatchAs {'pattern': ast.pattern | None, 'name': str | None}
MatchOr {'patterns': list[ast.pattern]}
TypeIgnore {'lineno': <class 'int'>, 'tag': <class 'str'>}
TypeVar {'name': <class 'str'>, 'bound': ast.expr | None, 'default_value': ast.expr | None}
ParamSpec {'name': <class 'str'>, 'default_value': ast.expr | None}
TypeVarTuple {'name': <class 'str'>, 'default_value': ast.expr | None}
AttributeError: Index
AttributeError: ExtSlice
countExceptions = 20

No errors after code changes

After adding logic to "Parser/asdl_c.py" to treat _field_types similar to _fields, the exceptions were replaced with empty dictionaries.

Summary

(.venv) C:\clones\cpython>C:\clones\cpython\python c:/clones/cpython/field_types.py
Running Debug|x64 interpreter...
3.15.0a0 (heads/main-dirty:b14986c9146, Jun 21 2025, 15:02:00) [MSC v.1944 64 bit (AMD64)]

astClass._fields
countExceptions = 0

astClass._field_types
countExceptions = 0

_field_types for all ast.AST subclasses

Details

AST {}
mod {}
stmt {}
expr {}
expr_context {}
boolop {}
operator {}
unaryop {}
cmpop {}
comprehension {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'ifs': list[ast.expr], 'is_async': <class 'int'>}
excepthandler {}
arguments {'posonlyargs': list[ast.arg], 'args': list[ast.arg], 'vararg': ast.arg | None, 'kwonlyargs': list[ast.arg], 'kw_defaults': list[ast.expr], 'kwarg': ast.arg | None, 'defaults': list[ast.expr]}
arg {'arg': <class 'str'>, 'annotation': ast.expr | None, 'type_comment': str | None}
keyword {'arg': str | None, 'value': <class 'ast.expr'>}
alias {'name': <class 'str'>, 'asname': str | None}
withitem {'context_expr': <class 'ast.expr'>, 'optional_vars': ast.expr | None}
match_case {'pattern': <class 'ast.pattern'>, 'guard': ast.expr | None, 'body': list[ast.stmt]}
pattern {}
type_ignore {}
type_param {}
slice {}
Module {'body': list[ast.stmt], 'type_ignores': list[ast.type_ignore]}
Interactive {'body': list[ast.stmt]}
Expression {'body': <class 'ast.expr'>}
FunctionType {'argtypes': list[ast.expr], 'returns': <class 'ast.expr'>}
Suite {}
FunctionDef {'name': <class 'str'>, 'args': <class 'ast.arguments'>, 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'returns': ast.expr | None, 'type_comment': str | None, 'type_params': list[ast.type_param]}
AsyncFunctionDef {'name': <class 'str'>, 'args': <class 'ast.arguments'>, 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'returns': ast.expr | None, 'type_comment': str | None, 'type_params': list[ast.type_param]}
ClassDef {'name': <class 'str'>, 'bases': list[ast.expr], 'keywords': list[ast.keyword], 'body': list[ast.stmt], 'decorator_list': list[ast.expr], 'type_params': list[ast.type_param]}
Return {'value': ast.expr | None}
Delete {'targets': list[ast.expr]}
Assign {'targets': list[ast.expr], 'value': <class 'ast.expr'>, 'type_comment': str | None}
TypeAlias {'name': <class 'ast.expr'>, 'type_params': list[ast.type_param], 'value': <class 'ast.expr'>}
AugAssign {'target': <class 'ast.expr'>, 'op': <class 'ast.operator'>, 'value': <class 'ast.expr'>}
AnnAssign {'target': <class 'ast.expr'>, 'annotation': <class 'ast.expr'>, 'value': ast.expr | None, 'simple': <class 'int'>}
For {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt], 'type_comment': str | None}
AsyncFor {'target': <class 'ast.expr'>, 'iter': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt], 'type_comment': str | None}
While {'test': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt]}
If {'test': <class 'ast.expr'>, 'body': list[ast.stmt], 'orelse': list[ast.stmt]}
With {'items': list[ast.withitem], 'body': list[ast.stmt], 'type_comment': str | None}
AsyncWith {'items': list[ast.withitem], 'body': list[ast.stmt], 'type_comment': str | None}
Match {'subject': <class 'ast.expr'>, 'cases': list[ast.match_case]}
Raise {'exc': ast.expr | None, 'cause': ast.expr | None}
Try {'body': list[ast.stmt], 'handlers': list[ast.excepthandler], 'orelse': list[ast.stmt], 'finalbody': list[ast.stmt]}
TryStar {'body': list[ast.stmt], 'handlers': list[ast.excepthandler], 'orelse': list[ast.stmt], 'finalbody': list[ast.stmt]}
Assert {'test': <class 'ast.expr'>, 'msg': ast.expr | None}
Import {'names': list[ast.alias]}
ImportFrom {'module': str | None, 'names': list[ast.alias], 'level': int | None}
Global {'names': list[str]}
Nonlocal {'names': list[str]}
Expr {'value': <class 'ast.expr'>}
Pass {}
Break {}
Continue {}
BoolOp {'op': <class 'ast.boolop'>, 'values': list[ast.expr]}
NamedExpr {'target': <class 'ast.expr'>, 'value': <class 'ast.expr'>}
BinOp {'left': <class 'ast.expr'>, 'op': <class 'ast.operator'>, 'right': <class 'ast.expr'>}
UnaryOp {'op': <class 'ast.unaryop'>, 'operand': <class 'ast.expr'>}
Lambda {'args': <class 'ast.arguments'>, 'body': <class 'ast.expr'>}
IfExp {'test': <class 'ast.expr'>, 'body': <class 'ast.expr'>, 'orelse': <class 'ast.expr'>}
Dict {'keys': list[ast.expr], 'values': list[ast.expr]}
Set {'elts': list[ast.expr]}
ListComp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
SetComp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
DictComp {'key': <class 'ast.expr'>, 'value': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
GeneratorExp {'elt': <class 'ast.expr'>, 'generators': list[ast.comprehension]}
Await {'value': <class 'ast.expr'>}
Yield {'value': ast.expr | None}
YieldFrom {'value': <class 'ast.expr'>}
Compare {'left': <class 'ast.expr'>, 'ops': list[ast.cmpop], 'comparators': list[ast.expr]}
Call {'func': <class 'ast.expr'>, 'args': list[ast.expr], 'keywords': list[ast.keyword]}
FormattedValue {'value': <class 'ast.expr'>, 'conversion': <class 'int'>, 'format_spec': ast.expr | None}
Interpolation {'value': <class 'ast.expr'>, 'str': <class 'object'>, 'conversion': <class 'int'>, 'format_spec': ast.expr | None}
JoinedStr {'values': list[ast.expr]}
TemplateStr {'values': list[ast.expr]}
Constant {'value': <class 'object'>, 'kind': str | None}
Attribute {'value': <class 'ast.expr'>, 'attr': <class 'str'>, 'ctx': <class 'ast.expr_context'>}
Subscript {'value': <class 'ast.expr'>, 'slice': <class 'ast.expr'>, 'ctx': <class 'ast.expr_context'>}
Starred {'value': <class 'ast.expr'>, 'ctx': <class 'ast.expr_context'>}
Name {'id': <class 'str'>, 'ctx': <class 'ast.expr_context'>}
List {'elts': list[ast.expr], 'ctx': <class 'ast.expr_context'>}
Tuple {'elts': list[ast.expr], 'ctx': <class 'ast.expr_context'>}
Slice {'lower': ast.expr | None, 'upper': ast.expr | None, 'step': ast.expr | None}
Load {}
Store {}
Del {}
AugLoad {}
AugStore {}
Param {}
And {}
Or {}
Add {}
Sub {}
Mult {}
MatMult {}
Div {}
Mod {}
Pow {}
LShift {}
RShift {}
BitOr {}
BitXor {}
BitAnd {}
FloorDiv {}
Invert {}
Not {}
UAdd {}
USub {}
Eq {}
NotEq {}
Lt {}
LtE {}
Gt {}
GtE {}
Is {}
IsNot {}
In {}
NotIn {}
ExceptHandler {'type': ast.expr | None, 'name': str | None, 'body': list[ast.stmt]}
MatchValue {'value': <class 'ast.expr'>}
MatchSingleton {'value': <class 'object'>}
MatchSequence {'patterns': list[ast.pattern]}
MatchMapping {'keys': list[ast.expr], 'patterns': list[ast.pattern], 'rest': str | None}
MatchClass {'cls': <class 'ast.expr'>, 'patterns': list[ast.pattern], 'kwd_attrs': list[str], 'kwd_patterns': list[ast.pattern]}
MatchStar {'name': str | None}
MatchAs {'pattern': ast.pattern | None, 'name': str | None}
MatchOr {'patterns': list[ast.pattern]}
TypeIgnore {'lineno': <class 'int'>, 'tag': <class 'str'>}
TypeVar {'name': <class 'str'>, 'bound': ast.expr | None, 'default_value': ast.expr | None}
ParamSpec {'name': <class 'str'>, 'default_value': ast.expr | None}
TypeVarTuple {'name': <class 'str'>, 'default_value': ast.expr | None}
Index {}
ExtSlice {}
countExceptions = 0

Check for errors on branches

Not applicable to 3.9, 3.10, 3.11, 3.12.

Branch 3.13

Details

(.venv) C:\clones\cpython>C:\clones\cpython\python c:/clones/cpython/field_types.py
Running Debug|x64 interpreter...
3.13.5+ (heads/3.13:4eab9da960d, Jun 21 2025, 14:44:53) [MSC v.1944 64 bit (AMD64)]

astClass._fields
countExceptions = 0

astClass._field_types
AttributeError: AST
AttributeError: mod
AttributeError: stmt
AttributeError: expr
AttributeError: expr_context
AttributeError: boolop
AttributeError: operator
AttributeError: unaryop
AttributeError: cmpop
AttributeError: excepthandler
AttributeError: pattern
AttributeError: type_ignore
AttributeError: type_param
AttributeError: slice
AttributeError: Suite
AttributeError: AugLoad
AttributeError: AugStore
AttributeError: Param
AttributeError: Index
AttributeError: ExtSlice
countExceptions = 20

Branch 3.14

Details

(.venv) C:\clones\cpython>C:\clones\cpython\python c:/clones/cpython/field_types.py
Running Debug|x64 interpreter...
3.14.0b3+ (heads/3.14:73e2089ed13, Jun 21 2025, 14:57:19) [MSC v.1944 64 bit (AMD64)]

astClass._fields
countExceptions = 0

astClass._field_types
AttributeError: AST
AttributeError: mod
AttributeError: stmt
AttributeError: expr
AttributeError: expr_context
AttributeError: boolop
AttributeError: operator
AttributeError: unaryop
AttributeError: cmpop
AttributeError: excepthandler
AttributeError: pattern
AttributeError: type_ignore
AttributeError: type_param
AttributeError: slice
AttributeError: Suite
AttributeError: AugLoad
AttributeError: AugStore
AttributeError: Param
AttributeError: Index
AttributeError: ExtSlice
countExceptions = 20

Branch main

Details

(.venv) C:\clones\cpython>C:\clones\cpython\python c:/clones/cpython/field_types.py
Running Debug|x64 interpreter...
3.15.0a0 (heads/main:b14986c9146, Jun 21 2025, 14:59:45) [MSC v.1944 64 bit (AMD64)]

astClass._fields
countExceptions = 0

astClass._field_types
AttributeError: AST
AttributeError: mod
AttributeError: stmt
AttributeError: expr
AttributeError: expr_context
AttributeError: boolop
AttributeError: operator
AttributeError: unaryop
AttributeError: cmpop
AttributeError: excepthandler
AttributeError: pattern
AttributeError: type_ignore
AttributeError: type_param
AttributeError: slice
AttributeError: Suite
AttributeError: AugLoad
AttributeError: AugStore
AttributeError: Param
AttributeError: Index
AttributeError: ExtSlice
countExceptions = 20

Python code used to generate the above tests

Details

from itertools import chain
import ast
import sys

print(sys.version, end='\n\n')

countExceptions = 0
print('astClass._fields')
for astClass in [C for C in [ast.AST,*chain(*(c.__subclasses__() for c in [ast.AST,ast.Constant,*ast.AST.__subclasses__()]))] if issubclass(C,ast.AST)]:
	try:
		print(astClass.__name__, astClass._fields)
		# astClass._fields
	except AttributeError as ERRORmessage:
		print(ERRORmessage.__class__.__name__, astClass.__name__, sep=': ')
		countExceptions += 1
print(f"{countExceptions = }\n")

countExceptions = 0
print('astClass._field_types')
for astClass in [C for C in [ast.AST,*chain(*(c.__subclasses__() for c in [ast.AST,ast.Constant,*ast.AST.__subclasses__()]))] if issubclass(C,ast.AST)]:
	try:
		print(astClass.__name__, astClass._field_types)
		# astClass._field_types
	except AttributeError as ERRORmessage:
		print(ERRORmessage.__class__.__name__, astClass.__name__, sep=': ')
		countExceptions += 1
print(f"{countExceptions = }")

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 21, 2025
  2. picnixz commented on Jun 21, 2025

    @picnixz
    Member
  3. terryjreedy commented on Jun 21, 2025

    @terryjreedy
    Member

    Whether this is a bug report or feature request depends on the relevant doc, which is the section on ast.AST. It says that each concrete class has _fields and _field_types and that _field_types was added in 3.13. It says nothing about abstract classes, but the rule seems to be that abstract classes have _fields set to an empty tuple but no _field_types, which would have to be an empty dict. mod is an abstract type, so the example is not necessarily a bug.

    One can argue that omitting empty dicts is fine and they would be redundant with the empty tuples. On the other hand, one could argue the adding then would be more convenient. But it would break code that depends on getting the exception. So it is unclear to me whether we should. We might look at the issue that added _field_types for whether the omission is intentional.

    @Yhg1s Do you agree that this should be called a new feature?

  4. terryjreedy commented on Jun 22, 2025

    @terryjreedy
    Member

    The _field_types doc was added by PR #118212. This made the pre-existing private attribute 'public'. It may not exist on user subclasses. I will let the author @JelleZijlstra comment further as to whether the proposed addition should be made.

  5. hunterhogan commented on Jun 22, 2025

    @hunterhogan
    ContributorAuthor

    python/typeshed says ast.AST and its subclasses have _field_types. So, if this is a "feature request" in python/cpython, it would necessarily be a bug report in typeshed.

    class AST:
        if sys.version_info >= (3, 10):
            __match_args__ = ()
        _attributes: ClassVar[tuple[str, ...]]
        _fields: ClassVar[tuple[str, ...]]
        if sys.version_info >= (3, 13):
            _field_types: ClassVar[dict[str, Any]]
    
    
        if sys.version_info >= (3, 14):
            def __replace__(self) -> Self: ...

    https://github.com/python/typeshed/blob/707cd57e4935d36e2c812ef6c650b8faa2890162/stdlib/ast.pyi#L31-L41

  6. JelleZijlstra commented on Jun 22, 2025

    @JelleZijlstra
    Member

    Let's add this in 3.15 but leave the bugfix branches alone.

  7. added
    type-bugAn unexpected behavior, bug, or error
    type-featureA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    triagedThe issue has been accepted as valid by a triager.
    and removed
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 22, 2025
  8. picnixz commented on Jun 22, 2025

    @picnixz
    Member

    I'll mark it as a bug/feature so that we know that it's a known issue but we decided to only change it in 3.15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)triagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or errortype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions