Skip to content

Commit

Permalink
feat: Add relative file path and package properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 15, 2021
1 parent 03f955e commit d26ee1f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}
)

Expand Down

0 comments on commit d26ee1f

Please sign in to comment.