• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

domdfcoding / attr_utils / 15207148476

23 May 2025 09:34AM UTC coverage: 95.64%. Remained the same
15207148476

push

github

domdfcoding
Update pytest warningsfilter.

351 of 367 relevant lines covered (95.64%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/attr_utils/docstrings.py
1
#!/usr/bin/env python
2
#
3
#  docstrings.py
4
"""
5
Add better docstrings to attrs_ generated functions.
6

7
.. _attrs: https://www.attrs.org/en/stable/
8
"""
9
#
10
#  Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
11
#
12
#  Permission is hereby granted, free of charge, to any person obtaining a copy
13
#  of this software and associated documentation files (the "Software"), to deal
14
#  in the Software without restriction, including without limitation the rights
15
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
#  copies of the Software, and to permit persons to whom the Software is
17
#  furnished to do so, subject to the following conditions:
18
#
19
#  The above copyright notice and this permission notice shall be included in all
20
#  copies or substantial portions of the Software.
21
#
22
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
#  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26
#  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27
#  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
28
#  OR OTHER DEALINGS IN THE SOFTWARE.
29
#
30

31
# stdlib
32
import re
1✔
33
import sys
1✔
34
from types import MethodType
1✔
35
from typing import Optional, Pattern, Type, TypeVar
1✔
36

37
# 3rd party
38
from domdf_python_tools.compat import PYPY
1✔
39
from domdf_python_tools.doctools import base_new_docstrings, prettify_docstrings
1✔
40
from domdf_python_tools.typing import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType
1✔
41

42
__all__ = ["add_attrs_doc", "_T"]
1✔
43

44
attrs_docstring = "Automatically created by attrs."
1✔
45
ne_default = "Check equality and either forward a NotImplemented or return the result negated."
1✔
46
attrs_20_1_docstring: Pattern = re.compile(r"^\s*Method generated by attrs for class .*\.\s*")
1✔
47
repr_doc_template = "Return a string representation of the :class:`~.{name}`."
1✔
48
pickle_state_template = "Used for `pickling <https://docs.python.org/3/library/pickle.html>`_.\n\n{doc}"
1✔
49

50
_T = TypeVar("_T", bound=Type)
1✔
51

52

53
def add_attrs_doc(obj: _T) -> _T:
1✔
54
        """
55
        Add better docstrings to attrs generated functions.
56

57
        :param obj: The class to improve the docstrings for.
58
        """
59

60
        obj = prettify_docstrings(obj)
1✔
61

62
        new_docstrings = {
1✔
63
                        **base_new_docstrings,
64
                        "__repr__": repr_doc_template.format(name=obj.__name__),
65
                        "__setstate__": pickle_state_template.format(doc=attrs_docstring),
66
                        "__getstate__": pickle_state_template.format(doc=attrs_docstring),
67
                        }
68

69
        if hasattr(obj, "__ne__"):
1✔
70
                if PYPY or not isinstance(obj.__ne__, (WrapperDescriptorType, MethodDescriptorType, MethodWrapperType)):
1✔
71
                        if obj.__ne__.__doc__ is None or re.sub("\\s+", ' ', obj.__ne__.__doc__).strip() in {
1✔
72
                                        object.__ne__.__doc__,
73
                                        ne_default,
74
                                        }:
75
                                obj.__ne__.__doc__ = new_docstrings["__ne__"]
1✔
76

77
        if hasattr(obj, "__repr__"):
1✔
78
                if (
1✔
79
                                obj.__repr__.__doc__ is None or obj.__repr__.__doc__.strip() == attrs_docstring
80
                                or attrs_20_1_docstring.match(obj.__repr__.__doc__)
81
                                ):
82
                        _new_doc = f"{new_docstrings['__repr__']}"  # \n\n{attrs_docstring}
1✔
83
                        obj.__repr__.__doc__ = _new_doc  # prevents strange formatting in pycharm
1✔
84

85
        for attr_name in new_docstrings:
1✔
86

87
                if not hasattr(obj, attr_name):
1✔
88
                        continue
1✔
89

90
                attribute = getattr(obj, attr_name)
1✔
91

92
                if not PYPY and isinstance(
1✔
93
                                attribute,
94
                                (WrapperDescriptorType, MethodDescriptorType, MethodWrapperType, MethodType),
95
                                ):
96
                        continue  # pragma: no cover (!PyPy)
97
                elif PYPY and isinstance(attribute, MethodType):
1✔
98
                        continue  # pragma: no cover
99
                elif PYPY and sys.version_info >= (3, 7):  # pragma: no cover (not (PyPy and py37+))
100
                        if attribute is getattr(object, attr_name, None):
101
                                continue
102
                        elif attribute is getattr(float, attr_name, None):
103
                                continue
104
                        elif attribute is getattr(str, attr_name, None):
105
                                continue
106

107
                if attribute is None:
1✔
108
                        continue
1✔
109

110
                doc: Optional[str] = attribute.__doc__
1✔
111

112
                if doc is None or doc.strip() == attrs_docstring or attrs_20_1_docstring.match(doc):
1✔
113
                        _new_doc = f"{new_docstrings[attr_name]}"  # \n\n{attrs_docstring}
1✔
114
                        attribute.__doc__ = _new_doc  # prevents strange formatting in pycharm
1✔
115

116
        return obj
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc