Skip to content

Commit

Permalink
feat: Add properties to objects
Browse files Browse the repository at this point in the history
- source
- modules
- classes
- functions
- attributes
  • Loading branch information
pawamoy committed Nov 13, 2021
1 parent 548f72e commit 0ec301a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import inspect
from functools import cached_property
from pathlib import Path
from textwrap import dedent
from typing import Any

from griffe.collections import lines_collection
from griffe.docstrings.dataclasses import DocstringSection
from griffe.docstrings.parsers import Parser, parse # noqa: WPS347

Expand Down Expand Up @@ -391,6 +393,31 @@ def path(self) -> str:
return self.name
return ".".join((self.parent.path, self.name))

@cached_property
def source(self) -> str:
"""Return the source code of this object.
Returns:
The source code.
"""
return dedent("\n".join(lines_collection[self.filepath][self.lineno - 1 : self.endlineno]))

@property
def modules(self):
return {name: member for name, member in self.members.items() if member.kind is Kind.MODULE}

@property
def classes(self):
return {name: member for name, member in self.members.items() if member.kind is Kind.CLASS}

@property
def functions(self):
return {name: member for name, member in self.members.items() if member.kind is Kind.FUNCTION}

@property
def attributes(self):
return {name: member for name, member in self.members.items() if member.kind is Kind.ATTRIBUTE}

def as_dict(self, full: bool = False, **kwargs: Any) -> dict[str, Any]:
"""Return this object's data as a dictionary.
Expand Down

0 comments on commit 0ec301a

Please sign in to comment.