diff --git a/src/gt4py/next/ffront/ast_passes/fix_missing_locations.py b/src/gt4py/next/ffront/ast_passes/fix_missing_locations.py index 639867e72a..24c1717de1 100644 --- a/src/gt4py/next/ffront/ast_passes/fix_missing_locations.py +++ b/src/gt4py/next/ffront/ast_passes/fix_missing_locations.py @@ -42,15 +42,15 @@ def generic_visit(self, node: ast.AST) -> ast.AST: node = copy.copy(node) parent_node = self._parent_nodes[-1] - node.lineno = parent_node.lineno # type: ignore[attr-defined] # this is monkey patching as not all `ast.AST` have `lineno` etc - node.col_offset = parent_node.col_offset # type: ignore[attr-defined] # this is monkey patching as not all `ast.AST` have `lineno` etc + node.lineno = parent_node.lineno # type: ignore[attr-defined] # we are adding the attribute which breaks type checking + node.col_offset = parent_node.col_offset # type: ignore[attr-defined] # the end positions are optional according to # https://docs.python.org/3/library/ast.html#ast.AST.end_col_offset if hasattr(parent_node, "end_lineno"): - node.end_lineno = parent_node.end_lineno # type: ignore[attr-defined] # this is monkey patching as not all `ast.AST` have `lineno` etc + node.end_lineno = parent_node.end_lineno # type: ignore[attr-defined] if hasattr(parent_node, "end_col_offset"): - node.end_col_offset = parent_node.end_col_offset # type: ignore[attr-defined] # this is monkey patching as not all `ast.AST` have `lineno` etc + node.end_col_offset = parent_node.end_col_offset # type: ignore[attr-defined] self._parent_nodes.append(node) result = super().generic_visit(node) diff --git a/src/gt4py/next/ffront/dialect_parser.py b/src/gt4py/next/ffront/dialect_parser.py index 9e0a2faff4..771ef72016 100644 --- a/src/gt4py/next/ffront/dialect_parser.py +++ b/src/gt4py/next/ffront/dialect_parser.py @@ -110,7 +110,7 @@ def get_location(self, node: ast.AST) -> SourceLocation: line_offset = self.source_definition.line_offset col_offset = self.source_definition.column_offset - # seems we monkey patched all AST nodes in `fix_missing_locations`, therefore we can assume all location attributes are defined + # `FixMissingLocations` ensures that all nodes have the location attributes assert hasattr(node, "lineno") line = node.lineno + line_offset if node.lineno is not None else None assert hasattr(node, "end_lineno")