diff --git a/guppylang/decorator.py b/guppylang/decorator.py index d1485a16..a2be4918 100644 --- a/guppylang/decorator.py +++ b/guppylang/decorator.py @@ -1,6 +1,6 @@ import inspect from collections.abc import Callable -from dataclasses import dataclass +from dataclasses import dataclass, field from pathlib import Path from types import ModuleType from typing import Any, TypeVar @@ -35,17 +35,11 @@ class ModuleIdentifier: """Identifier for the Python file/module that called the decorator.""" filename: Path - module: ModuleType | None - @property - def name(self) -> str: - """Returns a user-friendly name for the caller. - - If the called is not a function, uses the file name. - """ - if self.module is not None: - return str(self.module.__name__) - return self.filename.name + #: The name of the module. We only store this to have nice name to report back to + #: the user. When determining whether two `ModuleIdentifier`s correspond to the same + #: module, we only take the module path into account. + name: str = field(compare=False) class _Guppy: @@ -102,7 +96,10 @@ def _get_python_caller(self, fn: PyFunc | None = None) -> ModuleIdentifier: break else: raise GuppyError("Could not find a caller for the `@guppy` decorator") - return ModuleIdentifier(Path(filename), module) + module_path = Path(filename) + return ModuleIdentifier( + module_path, module.__name__ if module else module_path.name + ) @pretty_errors def extend_type(self, module: GuppyModule, defn: TypeDef) -> ClassDecorator: