|
1 | 1 | import warnings
|
2 | 2 | from contextlib import nullcontext
|
| 3 | +from dataclasses import dataclass |
3 | 4 | from functools import cached_property, partial, wraps
|
4 | 5 | from inspect import getsourcefile, getsourcelines
|
5 | 6 |
|
@@ -1368,6 +1369,46 @@ def __init__(self, param1: int) -> None:
|
1368 | 1369 | pass
|
1369 | 1370 |
|
1370 | 1371 |
|
| 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 | + |
1371 | 1412 | class TestValidator:
|
1372 | 1413 | def _import_path(self, klass=None, func=None):
|
1373 | 1414 | """
|
@@ -1757,6 +1798,34 @@ def test_constructor_docstrings(
|
1757 | 1798 | "Test for embedded class constructor docstring not implemented yet."
|
1758 | 1799 | )
|
1759 | 1800 |
|
| 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 | + |
1760 | 1829 |
|
1761 | 1830 | def decorator(x):
|
1762 | 1831 | """Test decorator."""
|
|
0 commit comments