1
1
import pytest
2
2
import sys
3
3
import warnings
4
- from inspect import getsourcelines
4
+ from functools import cached_property
5
+ from inspect import getsourcelines , getsourcefile
5
6
6
7
from numpydoc import validate
7
8
import numpydoc .tests
@@ -1541,8 +1542,12 @@ class DecoratorClass:
1541
1542
"""
1542
1543
Class and methods with decorators.
1543
1544
1544
- `DecoratorClass` has two decorators, `DecoratorClass.test_no_decorator` has no
1545
- decorator and `DecoratorClass.test_three_decorators` has three decorators.
1545
+ * `DecoratorClass` has two decorators.
1546
+ * `DecoratorClass.test_no_decorator` has no decorator.
1547
+ * `DecoratorClass.test_property` has a `@property` decorator.
1548
+ * `DecoratorClass.test_cached_property` has a `@cached_property` decorator.
1549
+ * `DecoratorClass.test_three_decorators` has three decorators.
1550
+
1546
1551
`Validator.source_file_def_line` should return the `def` or `class` line number, not
1547
1552
the line of the first decorator.
1548
1553
"""
@@ -1551,6 +1556,16 @@ def test_no_decorator(self):
1551
1556
"""Test method without decorators."""
1552
1557
pass
1553
1558
1559
+ @property
1560
+ def test_property (self ):
1561
+ """Test property method."""
1562
+ pass
1563
+
1564
+ @cached_property
1565
+ def test_cached_property (self ):
1566
+ """Test property method."""
1567
+ pass
1568
+
1554
1569
@decorator
1555
1570
@decorator
1556
1571
@decorator
@@ -1577,7 +1592,7 @@ def test_raises_for_invalid_attribute_name(self, invalid_name):
1577
1592
numpydoc .validate .Validator ._load_obj (invalid_name )
1578
1593
1579
1594
# inspect.getsourcelines does not return class decorators for Python 3.8. This was
1580
- # fixed starting with 3.9: https://github.com/python/cpython/issues/60060
1595
+ # fixed starting with 3.9: https://github.com/python/cpython/issues/60060.
1581
1596
@pytest .mark .parametrize (
1582
1597
["decorated_obj" , "def_line" ],
1583
1598
[
@@ -1590,6 +1605,14 @@ def test_raises_for_invalid_attribute_name(self, invalid_name):
1590
1605
"numpydoc.tests.test_validate.DecoratorClass.test_no_decorator" ,
1591
1606
getsourcelines (DecoratorClass .test_no_decorator )[- 1 ],
1592
1607
],
1608
+ [
1609
+ "numpydoc.tests.test_validate.DecoratorClass.test_property" ,
1610
+ getsourcelines (DecoratorClass .test_property .fget )[- 1 ] + 1 ,
1611
+ ],
1612
+ [
1613
+ "numpydoc.tests.test_validate.DecoratorClass.test_cached_property" ,
1614
+ getsourcelines (DecoratorClass .test_cached_property .func )[- 1 ] + 1 ,
1615
+ ],
1593
1616
[
1594
1617
"numpydoc.tests.test_validate.DecoratorClass.test_three_decorators" ,
1595
1618
getsourcelines (DecoratorClass .test_three_decorators )[- 1 ] + 3 ,
@@ -1603,3 +1626,24 @@ def test_source_file_def_line_with_decorators(self, decorated_obj, def_line):
1603
1626
)
1604
1627
)
1605
1628
assert doc .source_file_def_line == def_line
1629
+
1630
+ @pytest .mark .parametrize (
1631
+ ["property" , "file_name" ],
1632
+ [
1633
+ [
1634
+ "numpydoc.tests.test_validate.DecoratorClass.test_property" ,
1635
+ getsourcefile (DecoratorClass .test_property .fget ),
1636
+ ],
1637
+ [
1638
+ "numpydoc.tests.test_validate.DecoratorClass.test_cached_property" ,
1639
+ getsourcefile (DecoratorClass .test_cached_property .func ),
1640
+ ],
1641
+ ],
1642
+ )
1643
+ def test_source_file_name_with_properties (self , property , file_name ):
1644
+ doc = numpydoc .validate .Validator (
1645
+ numpydoc .docscrape .get_doc_object (
1646
+ numpydoc .validate .Validator ._load_obj (property )
1647
+ )
1648
+ )
1649
+ assert doc .source_file_name == file_name
0 commit comments