Skip to content

Commit 53ff465

Browse files
authored
Cached property (#12)
* add support for cached property
1 parent 5fb1e6a commit 53ff465

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
## [v1.2.0] - 2023-07-12
6+
7+
### Added
8+
9+
- Support for `functools.cached_property`.
10+
511
## [v1.1.0] - 2023-01-26
612

713
### Added

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,16 @@ class FooClass:
14201420
def bar(self):
14211421
return "bar"
14221422

1423+
class FooClass:
1424+
"""Perform foo action.
1425+
1426+
Attrs:
1427+
"""
1428+
1429+
@functools.cached_property
1430+
def bar(self):
1431+
return "bar"
1432+
14231433
class FooClass:
14241434
"""Perform foo action.
14251435
@@ -1461,6 +1471,17 @@ class FooClass:
14611471
def bar(self):
14621472
return "bar"
14631473

1474+
class FooClass:
1475+
"""Perform foo action.
1476+
1477+
Attrs:
1478+
bar: The value to perform the foo action on.
1479+
"""
1480+
1481+
@functools.cached_property
1482+
def bar(self):
1483+
return "bar"
1484+
14641485
class FooClass:
14651486
"""Perform foo action.
14661487

flake8_docstrings_complete/attrs.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ def is_property_decorator(node: ast.expr) -> bool:
5555
Whether the node is a property decorator.
5656
"""
5757
if isinstance(node, ast.Name):
58-
return node.id == "property"
58+
return node.id in {"property", "cached_property"}
5959

6060
# Handle call
6161
if isinstance(node, ast.Call):
6262
return is_property_decorator(node=node.func)
6363

64-
return False
64+
# Handle attr
65+
if isinstance(node, ast.Attribute):
66+
value = node.value
67+
return (
68+
node.attr == "cached_property"
69+
and isinstance(value, ast.Name)
70+
and value.id == "functools"
71+
)
72+
73+
# There is no valid syntax that gets to here
74+
return False # pragma: nocover
6575

6676

6777
def _get_class_target_name(target: ast.expr) -> ast.Name | None:

tests/unit/test___init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,38 @@ def function_1(self):
569569
class Class1:
570570
"""Docstring.
571571
572+
Attrs:
573+
function_1:
574+
"""
575+
@cached_property
576+
def function_1(self):
577+
"""Docstring 1."""
578+
return 1
579+
''',
580+
(),
581+
id="cached_property return value docstring no returns section",
582+
),
583+
pytest.param(
584+
'''
585+
class Class1:
586+
"""Docstring.
587+
588+
Attrs:
589+
function_1:
590+
"""
591+
@functools.cached_property
592+
def function_1(self):
593+
"""Docstring 1."""
594+
return 1
595+
''',
596+
(),
597+
id="functools.cached_property return value docstring no returns section",
598+
),
599+
pytest.param(
600+
'''
601+
class Class1:
602+
"""Docstring.
603+
572604
Attrs:
573605
function_1:
574606
"""

tests/unit/test___init__attrs.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ def attr_1():
183183
class Class1:
184184
"""Docstring 1.
185185
186+
Attrs:
187+
"""
188+
@cached_property
189+
def attr_1():
190+
"""Docstring 2."""
191+
return "value 1"
192+
''',
193+
(f"8:4 {ATTR_NOT_IN_DOCSTR_MSG % 'attr_1'}",),
194+
id="class has single cached_property docstring no attr",
195+
),
196+
pytest.param(
197+
'''
198+
class Class1:
199+
"""Docstring 1.
200+
201+
Attrs:
202+
"""
203+
@functools.cached_property
204+
def attr_1():
205+
"""Docstring 2."""
206+
return "value 1"
207+
''',
208+
(f"8:4 {ATTR_NOT_IN_DOCSTR_MSG % 'attr_1'}",),
209+
id="class has single functools.cached_property docstring no attr",
210+
),
211+
pytest.param(
212+
'''
213+
class Class1:
214+
"""Docstring 1.
215+
186216
Attrs:
187217
"""
188218
@property
@@ -583,6 +613,38 @@ def attr_1():
583613
class Class1:
584614
"""Docstring 1.
585615
616+
Attrs:
617+
attr_1:
618+
"""
619+
@cached_property
620+
def attr_1():
621+
"""Docstring 2."""
622+
return "value 1"
623+
''',
624+
(),
625+
id="class single cached_property docstring single attr",
626+
),
627+
pytest.param(
628+
'''
629+
class Class1:
630+
"""Docstring 1.
631+
632+
Attrs:
633+
attr_1:
634+
"""
635+
@functools.cached_property
636+
def attr_1():
637+
"""Docstring 2."""
638+
return "value 1"
639+
''',
640+
(),
641+
id="class single functools.cached_property docstring single attr",
642+
),
643+
pytest.param(
644+
'''
645+
class Class1:
646+
"""Docstring 1.
647+
586648
Attrs:
587649
attr_1:
588650
"""

0 commit comments

Comments
 (0)