Skip to content
Closed
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
50 changes: 32 additions & 18 deletions pycparser/_ast_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ def _gen_init(self):
src = f"class {self.name}(Node):\n"

if self.all_entries:
args = ", ".join(self.all_entries)
args = ", ".join(self._get_typed_arg(entry) for entry in self.all_entries)
slots = ", ".join(f"'{e}'" for e in self.all_entries)
slots += ", 'coord', '__weakref__'"
arglist = f"(self, {args}, coord=None)"
arglist = f"(self, {args}, coord: Coord | None = None)"
else:
slots = "'coord', '__weakref__'"
arglist = "(self, coord=None)"
arglist = "(self, coord: Coord | None = None)"

src += f" __slots__ = ({slots})\n"
src += f" def __init__{arglist}:\n"
src += f" def __init__{arglist} -> None:\n"

for name in self.all_entries + ["coord"]:
src += f" self.{name} = {name}\n"

return src

def _gen_children(self):
src = " def children(self):\n"
src = " def children(self) -> tuple[tuple[str, Node], ...]:\n"

if self.all_entries:
src += " nodelist = []\n"
Expand All @@ -134,7 +134,7 @@ def _gen_children(self):
return src

def _gen_iter(self):
src = " def __iter__(self):\n"
src = " def __iter__(self) -> Generator[Node]:\n"

if self.all_entries:
for child in self.child:
Expand All @@ -158,6 +158,16 @@ def _gen_attr_names(self):
src = " attr_names = (" + "".join(f"{nm!r}, " for nm in self.attr) + ")"
return src

def _get_typed_arg(self, entry):
entry_type = (
"list[Node]"
if entry in self.seq_child
else "Node"
if entry in self.child
else "str"
)
return f"{entry}: {entry_type} | None"


_PROLOGUE_COMMENT = r"""#-----------------------------------------------------------------
# ** ATTENTION **
Expand All @@ -178,9 +188,13 @@ def _gen_attr_names(self):
"""
_PROLOGUE_CODE = r'''
import sys
from typing import Any, ClassVar, IO, Optional
from collections.abc import Callable, Generator
from typing import Any, ClassVar, IO, TYPE_CHECKING

if TYPE_CHECKING:
from .c_parser import Coord

def _repr(obj):
def _repr(obj: Any | None) -> str:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefit does "Any | None" provide?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the typing explicit; that's about it.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't Any already subsume the | None part, making it obsolete?

"""
Get the representation of an object, with dedicated pprint-like format for lists.
"""
Expand All @@ -190,12 +204,12 @@ def _repr(obj):
return repr(obj)

class Node:
__slots__ = ()
__slots__: ClassVar[tuple[str, ...]] = ()
""" Abstract base class for AST nodes.
"""
attr_names: ClassVar[tuple[str, ...]] = ()
coord: Optional[Any]
def __repr__(self):
coord: Coord | None
def __repr__(self) -> str:
""" Generates a python representation of the current node
"""
result = self.__class__.__name__ + '('
Expand All @@ -214,7 +228,7 @@ def __repr__(self):

return result

def children(self):
def children(self) -> None:
""" A sequence of all children that are Nodes
"""
pass
Expand All @@ -227,8 +241,8 @@ def show(
showemptyattrs: bool = True,
nodenames: bool = False,
showcoord: bool = False,
_my_node_name: Optional[str] = None,
):
_my_node_name: str | None = None,
) -> None:
""" Pretty print the Node and all its attributes and
children (recursively) to a buffer.

Expand Down Expand Up @@ -260,7 +274,7 @@ def show(
buf.write(lead + self.__class__.__name__+ ': ')

if self.attr_names:
def is_empty(v):
def is_empty(v: str | None) -> None:
v is None or (hasattr(v, '__len__') and len(v) == 0)
nvlist = [(n, getattr(self,n)) for n in self.attr_names \
if showemptyattrs or not is_empty(getattr(self,n))]
Expand Down Expand Up @@ -319,9 +333,9 @@ def visit_Constant(self, node):
(the ast module of Python 3.0)
"""

_method_cache = None
_method_cache: dict[str, Callable[[Node], Any | None]] | None = None

def visit(self, node: Node):
def visit(self, node: Node) -> Any | None:
""" Visit a node.
"""

Expand All @@ -336,7 +350,7 @@ def visit(self, node: Node):

return visitor(node)

def generic_visit(self, node: Node):
def generic_visit(self, node: Node) -> None:
""" Called if no explicit visitor function exists for a
node. Implements preorder visiting of the node.
"""
Expand Down
Loading