Skip to content

Commit

Permalink
refactor: Set property label on @cached_property-decoratored methods
Browse files Browse the repository at this point in the history
Also rename `abstract` label to `abstratmethod`.
  • Loading branch information
pawamoy committed Apr 18, 2022
1 parent 4a417bc commit bc068f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,11 @@ def decorators_to_labels(self, decorators: list[Decorator]) -> set[str]: # noqa
resolved_first = self.current.resolve(names[0])
resolved_name = ".".join([resolved_first, *names[1:]])
if resolved_name in stdlib_decorators:
if "abstract" in resolved_name:
labels.add("abstract")
if "abstractmethod" in resolved_name:
labels.add("abstractmethod")
elif "cached_property" in resolved_name:
labels.add("cached")
labels.add("property")
elif "cache" in resolved_name:
labels.add("cached")
return labels
Expand Down
32 changes: 19 additions & 13 deletions tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,34 @@ def test_not_defined_at_runtime():


@pytest.mark.parametrize(
("decorator", "label"),
("decorator", "labels"),
[
("property", "property"),
("functools.cache", "cached"),
("functools.cached_property", "cached"),
("functools.lru_cache", "cached"),
("functools.lru_cache(maxsize=8)", "cached"),
("cache", "cached"),
("cached_property", "cached"),
("lru_cache", "cached"),
("lru_cache(maxsize=8)", "cached"),
("property", {"property"}),
("staticmethod", {"staticmethod"}),
("classmethod", {"classmethod"}),
("functools.cache", {"cached"}),
("functools.cached_property", {"cached", "property"}),
("functools.lru_cache", {"cached"}),
("functools.lru_cache(maxsize=8)", {"cached"}),
("cache", {"cached"}),
("cached_property", {"cached", "property"}),
("lru_cache", {"cached"}),
("lru_cache(maxsize=8)", {"cached"}),
("abc.abstractmethod", {"abstractmethod"}),
("abstractmethod", {"abstractmethod"}),
],
)
def test_set_labels_using_decorators(decorator, label):
def test_set_labels_using_decorators(decorator, labels):
"""Assert decorators are used to set labels on objects.
Parameters:
decorator: A parametrized decorator.
label: The expected, parametrized label.
labels: The parametrized set of expected labels.
"""
code = f"""
import abc
import functools
from abc import abstractmethod
from functools import cache, cached_property, lru_cache
class A:
Expand All @@ -132,7 +138,7 @@ def f(self):
return 0
"""
with temporary_visited_module(code) as module:
assert label in module["A.f"].labels
assert module["A.f"].has_labels(labels)


def test_handle_property_setter_and_deleter():
Expand Down

0 comments on commit bc068f8

Please sign in to comment.