Skip to content

Commit bc88622

Browse files
committed
test(test_validate.py): add coverage for existing / expected functionality of property and dataclass objects
1 parent 0095ca0 commit bc88622

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

numpydoc/tests/test_validate.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import warnings
22
from contextlib import nullcontext
3+
from dataclasses import dataclass
34
from functools import cached_property, partial, wraps
45
from inspect import getsourcefile, getsourcelines
56

@@ -1368,6 +1369,46 @@ def __init__(self, param1: int) -> None:
13681369
pass
13691370

13701371

1372+
@dataclass
1373+
class DataclassWithDocstring:
1374+
"""
1375+
A class decorated by `dataclass`.
1376+
1377+
To check the functionality of `dataclass` objects do not break the Validator.
1378+
As param1 is not documented this class should also raise PR01.
1379+
"""
1380+
1381+
param1: int
1382+
1383+
1384+
class ClassWithPropertyObject:
1385+
"""
1386+
A class with a `property`.
1387+
1388+
To check the functionality of `property` objects do not break the Validator.
1389+
1390+
Parameters
1391+
----------
1392+
param1 : int
1393+
Description of param1.
1394+
"""
1395+
1396+
def __init__(self, param1: int) -> None:
1397+
self._param1 = param1
1398+
1399+
@property
1400+
def param1(self) -> int:
1401+
"""
1402+
Get the value of param1.
1403+
1404+
Returns
1405+
-------
1406+
int
1407+
The value of param1.
1408+
"""
1409+
return self._param1
1410+
1411+
13711412
class TestValidator:
13721413
def _import_path(self, klass=None, func=None):
13731414
"""
@@ -1757,6 +1798,34 @@ def test_constructor_docstrings(
17571798
"Test for embedded class constructor docstring not implemented yet."
17581799
)
17591800

1801+
def test_dataclass_object(self):
1802+
# Test validator methods complete execution on dataclass objects and methods
1803+
# Test case ought to be removed if dataclass objects properly supported.
1804+
result = validate_one(self._import_path(klass="DataclassWithDocstring"))
1805+
# Check codes match as expected for dataclass objects.
1806+
errs = ["ES01", "SA01", "EX01", "PR01"]
1807+
for error in result["errors"]:
1808+
assert error[0] in errs
1809+
errs.remove(error[0])
1810+
1811+
# Test initialisation method (usually undocumented in dataclass) raises any errors.
1812+
init_fn = self._import_path(klass="DataclassWithDocstring", func="__init__")
1813+
result = validate_one(init_fn)
1814+
# Check that __init__ raises GL08 when the class docstring doesn't document params.
1815+
assert result["errors"][0][0] == "GL08"
1816+
1817+
def test_property_object(self):
1818+
# Test validator methods complete execution on class property objects
1819+
# Test case ought to be removed if property objects properly supported.
1820+
result = validate_one(
1821+
self._import_path(klass="ClassWithPropertyObject", func="param1")
1822+
)
1823+
# Check codes match as expected for property objects.
1824+
errs = ["ES01", "SA01", "EX01"]
1825+
for error in result["errors"]:
1826+
assert error[0] in errs
1827+
errs.remove(error[0])
1828+
17601829

17611830
def decorator(x):
17621831
"""Test decorator."""

0 commit comments

Comments
 (0)