diff --git a/pymbolic/mapper/optimize.py b/pymbolic/mapper/optimize.py index 5be7d68..c6c2a1f 100644 --- a/pymbolic/mapper/optimize.py +++ b/pymbolic/mapper/optimize.py @@ -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