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

abravalheri / validate-pyproject / 11034501584

25 Sep 2024 01:49PM CUT coverage: 97.823% (-0.2%) from 97.976%
11034501584

push

github

abravalheri
Prevent Github action for ignoring files for cache

551 of 571 branches covered (96.5%)

Branch coverage included in aggregate %.

932 of 945 relevant lines covered (98.62%)

5.91 hits per line

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

98.89
/src/validate_pyproject/error_reporting.py
1
import io
6✔
2
import json
6✔
3
import logging
6✔
4
import os
6✔
5
import re
6✔
6
import typing
6✔
7
from contextlib import contextmanager
6✔
8
from textwrap import indent, wrap
6✔
9
from typing import Any, Dict, Generator, Iterator, List, Optional, Sequence, Union
6✔
10

11
from fastjsonschema import JsonSchemaValueException
6✔
12

13
if typing.TYPE_CHECKING:
14
    import sys
15

16
    if sys.version_info < (3, 11):
17
        from typing_extensions import Self
18
    else:
19
        from typing import Self
20

21
_logger = logging.getLogger(__name__)
6✔
22

23
_MESSAGE_REPLACEMENTS = {
6✔
24
    "must be named by propertyName definition": "keys must be named by",
25
    "one of contains definition": "at least one item that matches",
26
    " same as const definition:": "",
27
    "only specified items": "only items matching the definition",
28
}
29

30
_SKIP_DETAILS = (
6✔
31
    "must not be empty",
32
    "is always invalid",
33
    "must not be there",
34
)
35

36
_NEED_DETAILS = {"anyOf", "oneOf", "allOf", "contains", "propertyNames", "not", "items"}
6✔
37

38
_CAMEL_CASE_SPLITTER = re.compile(r"\W+|([A-Z][^A-Z\W]*)")
6✔
39
_IDENTIFIER = re.compile(r"^[\w_]+$", re.I)
6✔
40

41
_TOML_JARGON = {
6✔
42
    "object": "table",
43
    "property": "key",
44
    "properties": "keys",
45
    "property names": "keys",
46
}
47

48
_FORMATS_HELP = """
6✔
49
For more details about `format` see
50
https://validate-pyproject.readthedocs.io/en/latest/api/validate_pyproject.formats.html
51
"""
52

53

54
class ValidationError(JsonSchemaValueException):
6✔
55
    """Report violations of a given JSON schema.
6✔
56

57
    This class extends :exc:`~fastjsonschema.JsonSchemaValueException`
58
    by adding the following properties:
59

60
    - ``summary``: an improved version of the ``JsonSchemaValueException`` error message
61
      with only the necessary information)
62

63
    - ``details``: more contextual information about the error like the failing schema
64
      itself and the value that violates the schema.
65

66
    Depending on the level of the verbosity of the ``logging`` configuration
67
    the exception message will be only ``summary`` (default) or a combination of
68
    ``summary`` and ``details`` (when the logging level is set to :obj:`logging.DEBUG`).
69
    """
70

71
    summary = ""
6✔
72
    details = ""
6✔
73
    _original_message = ""
6✔
74

75
    @classmethod
6✔
76
    def _from_jsonschema(cls, ex: JsonSchemaValueException) -> "Self":
6✔
77
        formatter = _ErrorFormatting(ex)
6✔
78
        obj = cls(str(formatter), ex.value, formatter.name, ex.definition, ex.rule)
6✔
79
        debug_code = os.getenv("JSONSCHEMA_DEBUG_CODE_GENERATION", "false").lower()
6✔
80
        if debug_code != "false":  # pragma: no cover
81
            obj.__cause__, obj.__traceback__ = ex.__cause__, ex.__traceback__
82
        obj._original_message = ex.message
6✔
83
        obj.summary = formatter.summary
6✔
84
        obj.details = formatter.details
6✔
85
        return obj
6✔
86

87

88
@contextmanager
6✔
89
def detailed_errors() -> Generator[None, None, None]:
6✔
90
    try:
6✔
91
        yield
6✔
92
    except JsonSchemaValueException as ex:
6✔
93
        raise ValidationError._from_jsonschema(ex) from None
6✔
94

95

96
class _ErrorFormatting:
6✔
97
    def __init__(self, ex: JsonSchemaValueException):
6✔
98
        self.ex = ex
6✔
99
        self.name = f"`{self._simplify_name(ex.name)}`"
6✔
100
        self._original_message: str = self.ex.message.replace(ex.name, self.name)
6✔
101
        self._summary = ""
6✔
102
        self._details = ""
6✔
103

104
    def __str__(self) -> str:
6✔
105
        if _logger.getEffectiveLevel() <= logging.DEBUG and self.details:
6✔
106
            return f"{self.summary}\n\n{self.details}"
6✔
107

108
        return self.summary
6✔
109

110
    @property
6✔
111
    def summary(self) -> str:
6✔
112
        if not self._summary:
6✔
113
            self._summary = self._expand_summary()
6✔
114

115
        return self._summary
6✔
116

117
    @property
6✔
118
    def details(self) -> str:
6✔
119
        if not self._details:
6✔
120
            self._details = self._expand_details()
6✔
121

122
        return self._details
6✔
123

124
    @staticmethod
6✔
125
    def _simplify_name(name: str) -> str:
6✔
126
        x = len("data.")
6✔
127
        return name[x:] if name.startswith("data.") else name
6✔
128

129
    def _expand_summary(self) -> str:
6✔
130
        msg = self._original_message
6✔
131

132
        for bad, repl in _MESSAGE_REPLACEMENTS.items():
6✔
133
            msg = msg.replace(bad, repl)
6✔
134

135
        if any(substring in msg for substring in _SKIP_DETAILS):
6✔
136
            return msg
6✔
137

138
        schema = self.ex.rule_definition
6✔
139
        if self.ex.rule in _NEED_DETAILS and schema:
6✔
140
            summary = _SummaryWriter(_TOML_JARGON)
6✔
141
            return f"{msg}:\n\n{indent(summary(schema), '    ')}"
6✔
142

143
        return msg
6✔
144

145
    def _expand_details(self) -> str:
6✔
146
        optional = []
6✔
147
        definition = self.ex.definition or {}
6✔
148
        desc_lines = definition.pop("$$description", [])
6✔
149
        desc = definition.pop("description", None) or " ".join(desc_lines)
6✔
150
        if desc:
6✔
151
            description = "\n".join(
6✔
152
                wrap(
153
                    desc,
154
                    width=80,
155
                    initial_indent="    ",
156
                    subsequent_indent="    ",
157
                    break_long_words=False,
158
                )
159
            )
160
            optional.append(f"DESCRIPTION:\n{description}")
6✔
161
        schema = json.dumps(definition, indent=4)
6✔
162
        value = json.dumps(self.ex.value, indent=4)
6✔
163
        defaults = [
6✔
164
            f"GIVEN VALUE:\n{indent(value, '    ')}",
165
            f"OFFENDING RULE: {self.ex.rule!r}",
166
            f"DEFINITION:\n{indent(schema, '    ')}",
167
        ]
168
        msg = "\n\n".join(optional + defaults)
6✔
169
        epilog = f"\n{_FORMATS_HELP}" if "format" in msg.lower() else ""
6✔
170
        return msg + epilog
6✔
171

172

173
class _SummaryWriter:
6✔
174
    _IGNORE = frozenset(("description", "default", "title", "examples"))
6✔
175

176
    def __init__(self, jargon: Optional[Dict[str, str]] = None):
6✔
177
        self.jargon: Dict[str, str] = jargon or {}
6✔
178
        # Clarify confusing terms
179
        self._terms = {
6✔
180
            "anyOf": "at least one of the following",
181
            "oneOf": "exactly one of the following",
182
            "allOf": "all of the following",
183
            "not": "(*NOT* the following)",
184
            "prefixItems": f"{self._jargon('items')} (in order)",
185
            "items": "items",
186
            "contains": "contains at least one of",
187
            "propertyNames": (
188
                f"non-predefined acceptable {self._jargon('property names')}"
189
            ),
190
            "patternProperties": f"{self._jargon('properties')} named via pattern",
191
            "const": "predefined value",
192
            "enum": "one of",
193
        }
194
        # Attributes that indicate that the definition is easy and can be done
195
        # inline (e.g. string and number)
196
        self._guess_inline_defs = [
6✔
197
            "enum",
198
            "const",
199
            "maxLength",
200
            "minLength",
201
            "pattern",
202
            "format",
203
            "minimum",
204
            "maximum",
205
            "exclusiveMinimum",
206
            "exclusiveMaximum",
207
            "multipleOf",
208
        ]
209

210
    def _jargon(self, term: Union[str, List[str]]) -> Union[str, List[str]]:
6✔
211
        if isinstance(term, list):
6✔
212
            return [self.jargon.get(t, t) for t in term]
6✔
213
        return self.jargon.get(term, term)
6✔
214

215
    def __call__(
6✔
216
        self,
217
        schema: Union[dict, List[dict]],
218
        prefix: str = "",
219
        *,
220
        _path: Sequence[str] = (),
221
    ) -> str:
222
        if isinstance(schema, list):
6✔
223
            return self._handle_list(schema, prefix, _path)
6✔
224

225
        filtered = self._filter_unecessary(schema, _path)
6✔
226
        simple = self._handle_simple_dict(filtered, _path)
6✔
227
        if simple:
6✔
228
            return f"{prefix}{simple}"
6✔
229

230
        child_prefix = self._child_prefix(prefix, "  ")
6✔
231
        item_prefix = self._child_prefix(prefix, "- ")
6✔
232
        indent = len(prefix) * " "
6✔
233
        with io.StringIO() as buffer:
6✔
234
            for i, (key, value) in enumerate(filtered.items()):
6✔
235
                child_path = [*_path, key]
6✔
236
                line_prefix = prefix if i == 0 else indent
6✔
237
                buffer.write(f"{line_prefix}{self._label(child_path)}:")
6✔
238
                # ^  just the first item should receive the complete prefix
239
                if isinstance(value, dict):
6✔
240
                    filtered = self._filter_unecessary(value, child_path)
6✔
241
                    simple = self._handle_simple_dict(filtered, child_path)
6✔
242
                    buffer.write(
6✔
243
                        f" {simple}"
244
                        if simple
245
                        else f"\n{self(value, child_prefix, _path=child_path)}"
246
                    )
247
                elif isinstance(value, list) and (
6✔
248
                    key != "type" or self._is_property(child_path)
249
                ):
250
                    children = self._handle_list(value, item_prefix, child_path)
6✔
251
                    sep = " " if children.startswith("[") else "\n"
6✔
252
                    buffer.write(f"{sep}{children}")
6✔
253
                else:
254
                    buffer.write(f" {self._value(value, child_path)}\n")
6✔
255
            return buffer.getvalue()
6✔
256

257
    def _is_unecessary(self, path: Sequence[str]) -> bool:
6✔
258
        if self._is_property(path) or not path:  # empty path => instruction @ root
6✔
259
            return False
6✔
260
        key = path[-1]
6✔
261
        return any(key.startswith(k) for k in "$_") or key in self._IGNORE
6✔
262

263
    def _filter_unecessary(
6✔
264
        self, schema: Dict[str, Any], path: Sequence[str]
265
    ) -> Dict[str, Any]:
266
        return {
6✔
267
            key: value
268
            for key, value in schema.items()
269
            if not self._is_unecessary([*path, key])
270
        }
271

272
    def _handle_simple_dict(self, value: dict, path: Sequence[str]) -> Optional[str]:
6✔
273
        inline = any(p in value for p in self._guess_inline_defs)
6✔
274
        simple = not any(isinstance(v, (list, dict)) for v in value.values())
6✔
275
        if inline or simple:
6✔
276
            return f"{{{', '.join(self._inline_attrs(value, path))}}}\n"
6✔
277
        return None
6✔
278

279
    def _handle_list(
6✔
280
        self, schemas: list, prefix: str = "", path: Sequence[str] = ()
281
    ) -> str:
282
        if self._is_unecessary(path):
6✔
283
            return ""
×
284

285
        repr_ = repr(schemas)
6✔
286
        if all(not isinstance(e, (dict, list)) for e in schemas) and len(repr_) < 60:
6✔
287
            return f"{repr_}\n"
6✔
288

289
        item_prefix = self._child_prefix(prefix, "- ")
6✔
290
        return "".join(
6✔
291
            self(v, item_prefix, _path=[*path, f"[{i}]"]) for i, v in enumerate(schemas)
292
        )
293

294
    def _is_property(self, path: Sequence[str]) -> bool:
6✔
295
        """Check if the given path can correspond to an arbitrarily named property"""
296
        counter = 0
6✔
297
        for key in path[-2::-1]:
6✔
298
            if key not in {"properties", "patternProperties"}:
6✔
299
                break
6✔
300
            counter += 1
6✔
301

302
        # If the counter if even, the path correspond to a JSON Schema keyword
303
        # otherwise it can be any arbitrary string naming a property
304
        return counter % 2 == 1
6✔
305

306
    def _label(self, path: Sequence[str]) -> str:
6✔
307
        *parents, key = path
6✔
308
        if not self._is_property(path):
6✔
309
            norm_key = _separate_terms(key)
6✔
310
            return self._terms.get(key) or " ".join(self._jargon(norm_key))
6✔
311

312
        if parents[-1] == "patternProperties":
6✔
313
            return f"(regex {key!r})"
6✔
314
        return repr(key)  # property name
6✔
315

316
    def _value(self, value: Any, path: Sequence[str]) -> str:
6✔
317
        if path[-1] == "type" and not self._is_property(path):
6✔
318
            type_ = self._jargon(value)
6✔
319
            return f"[{', '.join(type_)}]" if isinstance(type_, list) else type_
6✔
320
        return repr(value)
6✔
321

322
    def _inline_attrs(self, schema: dict, path: Sequence[str]) -> Iterator[str]:
6✔
323
        for key, value in schema.items():
6✔
324
            child_path = [*path, key]
6✔
325
            yield f"{self._label(child_path)}: {self._value(value, child_path)}"
6✔
326

327
    def _child_prefix(self, parent_prefix: str, child_prefix: str) -> str:
6✔
328
        return len(parent_prefix) * " " + child_prefix
6✔
329

330

331
def _separate_terms(word: str) -> List[str]:
6✔
332
    """
333
    >>> _separate_terms("FooBar-foo")
334
    ['foo', 'bar', 'foo']
335
    """
336
    return [w.lower() for w in _CAMEL_CASE_SPLITTER.split(word) if w]
6✔
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

© 2025 Coveralls, Inc