diff --git a/src/griffe/dataclasses.py b/src/griffe/dataclasses.py index 32486177..609f0597 100644 --- a/src/griffe/dataclasses.py +++ b/src/griffe/dataclasses.py @@ -194,7 +194,18 @@ def module(self) -> Module: return self.parent.module raise ValueError - @property + @cached_property + def package(self) -> Module: + """Return the absolute top module (the package) of this object. + + Returns: + The parent module. + """ + module = self.module + while module.parent: + module = module.parent # type: ignore + return module + def filepath(self) -> Path | None: """Return the file path where this object was defined. @@ -208,7 +219,15 @@ def filepath(self) -> Path | None: """ return self.module.filepath - @property + @cached_property + def relative_filepath(self) -> Path | None: + """Return the file path where this object was defined, relative to the top module path. + + Returns: + A file path. + """ + return self.module.filepath.relative_to(self.package.filepath.parent.parent) # type: ignore + def path(self) -> str: """Return the dotted path / import path of this object. @@ -237,7 +256,7 @@ def as_dict(self, full: bool = False) -> dict[str, Any]: base.update( { "path": self.path, - "filepath": str(self.filepath), + "relative_filepath": self.relative_filepath, } )