Skip to content

Commit e8db3a2

Browse files
committed
refactor: Stop caching objects' inherited members, aliases' members and inherited members, classes' resolved bases
Issue-346: #346
1 parent daadc60 commit e8db3a2

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

docs/guide/users/navigating.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,21 @@ To access an object's members, there are a few options:
8888

8989
The same way members are accessed, they can also be set:
9090

91-
- Dictionary-like item assignment: `markdown["thing"] = ...`, also supporting dotted-paths and string tuples. This will (re)assign both regular and inherited members for classes.
92-
- Safer method for extensions: `markdown.set_member("thing", ...)`, also supporting dotted-paths and string tuples. This will not (re)assign inherited members for classes.
91+
- Dictionary-like item assignment: `markdown["thing"] = ...`, also supporting dotted-paths and string tuples. This will (re)assign only regular members: inherited members (classes only) are re-computed everytime they are accessed.
92+
- Safer method for extensions: `markdown.set_member("thing", ...)`, also supporting dotted-paths and string tuples.
9393
- Regular member assignment: `markdown.members["thing"] = ...`. **This is not recommended, as the assigned member's `parent` attribute will not be automatically updated.**
9494

9595
...and deleted:
9696

97-
- Dictionary-like item deletion: `del markdown["thing"]`, also supporting dotted-paths and string tuples. This will delete both regular and inherited members for classes.
98-
- Safer method for extensions: `markdown.del_member("thing")`, also supporting dotted-paths and string tuples. This will not delete inherited members for classes.
97+
- Dictionary-like item deletion: `del markdown["thing"]`, also supporting dotted-paths and string tuples. This will delete only regular members: inherited members (classes only) are re-computed everytime they are accessed.
98+
- Safer method for extensions: `markdown.del_member("thing")`, also supporting dotted-paths and string tuples.
9999
- Regular member deletion: `del markdown.members["thing"]`. **This is not recommended, as the [`aliases`][griffe.Object.aliases] attribute of other objects in the tree will not be automatically updated.**
100100

101101
### Inherited members
102102

103103
Griffe supports class inheritance, both when visiting and inspecting modules.
104104

105-
To access members of a class that are inherited from base classes, use the [`inherited_members`][griffe.Object.inherited_members] attribute. If this is the first time you access inherited members, the base classes of the given class will be resolved and cached, then the MRO (Method Resolution Order) will be computed for these bases classes, and a dictionary of inherited members will be built and cached. Next times you access it, you'll get the cached dictionary. Make sure to only access `inherited_members` once everything is loaded by Griffe, to avoid computing things too early. Don't try to access inherited members in extensions, while visiting or inspecting modules.
105+
To access members of a class that are inherited from base classes, use the [`inherited_members`][griffe.Object.inherited_members] attribute. Everytime you access inherited members, the base classes of the given class will be resolved, then the MRO (Method Resolution Order) will be computed for these bases classes, and a dictionary of inherited members will be built. Make sure to store the result in a variable to avoid re-computing it everytime (you are responsible for the caching part). Also make sure to only access `inherited_members` once everything is loaded by Griffe, to avoid computing things too early. Don't try to access inherited members in extensions, while visiting or inspecting modules.
106106

107107
Inherited members are aliases that point at the corresponding members in parent classes. These aliases will have their [`inherited`][griffe.Alias.inherited] attribute set to true.
108108

src/_griffe/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def is_kind(self, kind: str | Kind | set[str | Kind]) -> bool:
573573
kind = Kind(kind)
574574
return self.kind is kind
575575

576-
@cached_property
576+
@property
577577
def inherited_members(self) -> dict[str, Alias]:
578578
"""Members that are inherited from base classes.
579579
@@ -1158,7 +1158,7 @@ def modules_collection(self) -> ModulesCollection:
11581158
# no need to forward to the target
11591159
return self.parent.modules_collection # type: ignore[union-attr] # we assume there's always a parent
11601160

1161-
@cached_property
1161+
@property
11621162
def members(self) -> dict[str, Object | Alias]:
11631163
"""The target's members (modules, classes, functions, attributes).
11641164
@@ -1178,7 +1178,7 @@ def members(self) -> dict[str, Object | Alias]:
11781178
for name, member in final_target.members.items()
11791179
}
11801180

1181-
@cached_property
1181+
@property
11821182
def inherited_members(self) -> dict[str, Alias]:
11831183
"""Members that are inherited from base classes.
11841184
@@ -1964,7 +1964,7 @@ def parameters(self) -> Parameters:
19641964
except KeyError:
19651965
return Parameters()
19661966

1967-
@cached_property
1967+
@property
19681968
def resolved_bases(self) -> list[Object]:
19691969
"""Resolved class bases.
19701970

0 commit comments

Comments
 (0)