Skip to content

Commit

Permalink
optimize._get_def_from_ast_container: Use last definition
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Feb 3, 2025
1 parent c8c7552 commit 01c386c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pymbolic/mapper/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,25 @@
AstDefNodeT = TypeVar("AstDefNodeT", ast.FunctionDef, ast.ClassDef)


_NOT_SEEN = object()


def _get_def_from_ast_container(
container: Iterable[ast.AST],
name: str,
node_type: type[AstDefNodeT]
) -> AstDefNodeT:
# Return the last definition in the container, not the first.
# This is relevant, e.g., in the case of @overload of methods.
result = _NOT_SEEN
for entry in container:
if isinstance(entry, node_type) and entry.name == name:
return entry
result = entry

if result is _NOT_SEEN:
raise ValueError(f"symbol '{name}' not found")

raise ValueError(f"symbol '{name}' not found")
return result


@lru_cache
Expand Down

0 comments on commit 01c386c

Please sign in to comment.