File: value_error.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (395 lines) | stat: -rw-r--r-- 13,910 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""value_error.py

Collection of functions useful in parsing ValueError messages and
providing a more detailed explanation.
"""

import inspect
import re
import unicodedata
from types import FrameType
from typing import Any, Optional, Tuple

from .. import debug_helper, info_variables, token_utils, utils
from ..ft_gettext import current_lang
from ..message_parser import get_parser
from ..tb_data import TracebackData  # for type checking only
from ..typing_info import CauseInfo  # for type checking only

convert_type = info_variables.convert_type
parser = get_parser(ValueError)
_ = current_lang.translate


def _unpacking() -> str:
    return _(
        "Unpacking is a convenient way to assign a name,\n"
        "to each item of an iterable.\n"
    )


def get_iterable(code: str, frame: FrameType) -> Tuple[Any, Optional[str]]:
    """gets an iterable object and its type as a string."""
    try:
        # As a ValueError exception has been raised, Python has already evaluated
        # all the relevant code parts. Thus, using eval should be completely safe.
        obj = utils.eval_expr(code, frame)
    except Exception:  # noqa
        return None, None

    if isinstance(obj, dict):
        iterable = "dict"
    elif isinstance(obj, list):
        iterable = "list"
    elif isinstance(obj, set):
        iterable = "set"
    elif isinstance(obj, str):
        iterable = "str"
    elif isinstance(obj, tuple):
        iterable = "tuple"
    else:
        iterable = None
    return obj, iterable


@parser._add
def not_enough_values_to_unpack(message: str, tb_data: TracebackData) -> CauseInfo:
    pattern1 = re.compile(r"not enough values to unpack \(expected (\d+), got (\d+)\)")
    match1 = re.search(pattern1, message)
    pattern2 = re.compile(
        r"not enough values to unpack \(expected at least (\d+), got (\d+)\)"
    )
    match2 = re.search(pattern2, message)
    if match1 is None and match2 is None:
        return {}

    match = match1 if match2 is None else match2
    frame = tb_data.exception_frame
    nb_names = match[1]
    length = match[2]

    if tb_data.bad_line.count("=") != 1:
        cause = _unpacking() + _(
            "In this instance, there are more names ({nb_names})\n"
            "than {length}, the length of the iterable.\n"
        ).format(nb_names=nb_names, length=length)
        return {"cause": cause}

    _lhs, rhs = tb_data.bad_line.split("=")
    obj, iterable = get_iterable(rhs, frame)
    if obj is None or iterable is None:
        cause = _unpacking() + _(
            "In this instance, there are more names ({nb_names})\n"
            "than {length}, the length of the iterable.\n"
        ).format(nb_names=nb_names, length=length)
        return {"cause": cause}

    cause = _unpacking() + _(
        "In this instance, there are more names ({nb_names})\n"
        "than the length of the iterable, {iter_type} of length {length}.\n"
    ).format(nb_names=nb_names, iter_type=convert_type(iterable), length=length)
    return {"cause": cause}


@parser._add
def too_many_values_to_unpack(message: str, tb_data: TracebackData) -> CauseInfo:
    pattern = re.compile(r"too many values to unpack \(expected (\d+)\)")
    match = re.search(pattern, message)
    if match is None:
        return {}

    nb_names = match[1]
    frame = tb_data.exception_frame

    if tb_data.bad_line.count("=") != 1:
        cause = _unpacking() + _(
            "In this instance, there are fewer names ({nb_names})\n"
            "than the length of the iterable.\n"
        ).format(nb_names=nb_names)
        return {"cause": cause}

    _lhs, rhs = tb_data.bad_line.split("=")

    obj, iterable = get_iterable(rhs, frame)
    if obj is None or iterable is None or not hasattr(obj, "__len__"):
        cause = _unpacking() + _(
            "In this instance, there are fewer names ({nb_names})\n"
            "than the length of the iterable.\n"
        ).format(nb_names=nb_names)
        return {"cause": cause}

    cause = _unpacking() + _(
        "In this instance, there are fewer names ({nb_names})\n"
        "than the length of the iterable, {iter_type} of length {length}.\n"
    ).format(nb_names=nb_names, iter_type=convert_type(iterable), length=len(obj))
    return {"cause": cause}


@parser._add
def invalid_literal_for_int(message: str, _tb_data: TracebackData) -> CauseInfo:
    pattern = re.compile(r"invalid literal for int\(\) with base (\d+): '(.*)'")
    match = re.search(pattern, message)
    if match is None:
        return {}

    base, value = int(match[1]), match[2]
    if not value.strip():
        cause = _(
            "`int()` expects an argument that looks like a number in base `{base}`\n"
            "but you gave it an empty string.\n"
        ).format(base=base)
        return {"cause": cause}

    begin_cause = _(
        "`{value}` is an invalid argument for `int()` in base `{base}`.\n"
    ).format(value=repr(value), base=base)

    valid = "0123456789abcdefghijiklmnopqrstuvwxyz"[:base]
    invalid = []
    for index, char in enumerate(value.strip()):
        if index == 0 and char in ["+", "-"]:
            continue
        if char.isalpha():
            convert = char.lower()
        else:
            convert = unicodedata.numeric(char, None)
            convert = str(int(convert)) if convert is not None else char
        if convert not in valid and char not in invalid:
            invalid.append(char)
    invalid = _("The following characters are not allowed: `{invalid}`.\n").format(
        invalid=utils.list_to_string(invalid)
    )
    if base == 10:
        try:
            int(float(value))
        except ValueError:
            pass
        else:
            return _convert_to_float(value)

    if base == 0:
        cause = _(
            "When base `0` is specified, `int()` expects the argument\n"
            "to be an integer literal, written in\n"
            "base 2 (`0b...`), 8 (`0o...`), 10, or 16 (`0x...`).\n"
        )
    elif 2 <= base <= 10:
        cause = _(
            "In base `{base}`, `int()` is most often use to convert a string\n"
            "containing the digits `0` to `{max_n}` into an integer.\n"
        ).format(base=base, max_n=valid[base - 1])
    elif base == 11:
        cause = _(
            "In base `11`, `int()` is most often use to convert a string\n"
            "containing the digits `0` to `9` and the letter `'a'` into an integer.\n"
        )
    elif base <= 36:
        cause = _(
            "In base `base`, `int()` is most often use to convert a string\n"
            "containing the digits `0` to `9` and the letters\n"
            "from `'a'` to `'{max_n}'` into an integer.\n"
        ).format(base=base, max_n=valid[base - 1])
    else:  # pragma: no cover
        debug_helper.log(f"Invalid base argument caught by mistake {base}")
        return {}

    if base != 0:
        cause += invalid

    return {"cause": begin_cause + cause}


def _convert_to_float(value: Any) -> CauseInfo:
    hint = _("You need to convert `'{value}'` to a float first.\n").format(value=value)
    cause = _(
        "The string `'{value}'` needs to be first converted using `float()`\n"
        "before the result can be converted into an integer using `int()`.\n"
    ).format(value=value)
    return {"cause": cause, "suggest": hint}


@parser._add
def base_for_int(message: str, tb_data: TracebackData) -> CauseInfo:
    if message != "int() base must be >= 2 and <= 36, or 0":
        return {}
    pattern = re.compile(r",\s*base\s*=(\d+)\s*\)")
    cause = _(
        "The argument `base` of `int()` must be either zero\n"
        "or any integer from 2 to 36.\n"
    )
    match = re.search(pattern, tb_data.bad_line)
    if match is None:
        return {"cause": cause}
    base_arg = match[1]
    cause += _("You wrote {base} which is not allowed.\n").format(base=base_arg)
    return {"cause": cause}


@parser._add
def date_month_must_be_between_1_and_12(
    message: str, _tb_data: TracebackData
) -> CauseInfo:
    if message != "month must be in 1..12":
        return {}

    hint = _("Did you specify an invalid month?\n")
    cause = _(
        "I am guessing that you specify an invalid value for a month\n"
        "in a `date` object. Valid values are integers, from 1 to 12.\n"
    )
    return {"cause": cause, "suggest": hint}


@parser._add
def could_not_convert_to_float(message: str, _tb_data: TracebackData) -> CauseInfo:
    if not message.startswith("could not convert string to float: "):
        return {}
    pattern = re.compile(r"could not convert string to float: '(.*)'")
    match = re.search(pattern, message)
    if match is None:
        debug_helper.log("Could not find match in could_not_convert_to_float.")
        return {}
    string = match[1]
    cause = _(
        "The string `{string}` cannot be converted to a `float`\n"
        "as it does not represent a number.\n"
    ).format(string=string)
    return {"cause": cause}


@parser._add
def slots_conflicts_with_class_variable(
    message: str, _tb_data: TracebackData
) -> CauseInfo:
    pattern = r"'(.*)' in __slots__ conflicts with class variable"
    match = re.search(pattern, message)
    if not match:
        return {}
    var = match[1]
    cause = _(
        "The name `{var}` is used both as the name of a class variable\n"
        "and as a string item in the class `__slots__`;\n"
        "this is not allowed.\n"
    ).format(var=var)
    return {"cause": cause}


@parser._add
def pow_third_arg_cannot_be_zero(message: str, _tb_data: TracebackData) -> CauseInfo:
    if message != "pow() 3rd argument cannot be 0":
        return {}
    return {"cause": _("The third argument of the function `pow()` cannot be zero.\n")}


@parser._add
def unrecognized_message(_message: str, tb_data: TracebackData) -> CauseInfo:
    """This attempts to provide some help when a message is not recognized."""
    bad_line = tb_data.bad_line.strip()
    frame = tb_data.exception_frame

    if bad_line.startswith("raise ") or bad_line.startswith("raise\t"):
        try:
            name = inspect.getframeinfo(frame).function
            fn_obj = frame.f_globals[name]
        except Exception:  # noqa
            return {}
    else:
        all_objects = info_variables.get_all_objects(bad_line, frame)["name, obj"]
        callables = []
        for name, obj in all_objects:
            if callable(obj):
                callables.append((name, obj))
        if not callables:
            return {}

        tokens = token_utils.get_significant_tokens(tb_data.bad_line)
        name, fn_obj = callables[0]
        if name != tokens[0]:
            return {}

    cause = _(
        "I do not recognize this error message.\n"
        "I am guessing that the problem is with the function `{name}`.\n"
    ).format(name=name)

    if hasattr(fn_obj, "__doc__") and fn_obj.__doc__ is not None:
        cause += _("Its docstring is:\n\n{docstring}\n").format(
            docstring="'''" + fn_obj.__doc__ + "'''"
        )
    else:
        cause += _("I have no more information.\n")
    return {"cause": cause}


@parser._add
def time_strftime_incorrect_format(message: str, _tb_data: TracebackData) -> CauseInfo:
    pattern = r"time data '(.*)' does not match format '(.*)'"
    match = re.search(pattern, message)
    if not match:
        return {}
    cause = _(
        "The value you gave for the time is not in the format you specified.\n"
        "Make sure to use the same separator between items\n"
        "(for example, between day and month) and keep the order the same\n"
        "in both the data provided and the format you specified.\n"
        "The following table might be useful:\n"
        "https://docs.python.org/3/library/time.html#time.strftime\n"
        "The following site might also be useful: https://www.strfti.me/\n"
    )
    return {"cause": cause}


@parser._add
def list_remove_x_not_in_list(message: str, tb_data: TracebackData) -> CauseInfo:
    if message != "list.remove(x): x not in list":
        return {}
    bad_line = tb_data.bad_line
    frame = tb_data.exception_frame
    all_objects = info_variables.get_all_objects(bad_line, frame)["name, obj"]
    dot_remove = 0
    list_remove = the_list = ""
    for name, obj in all_objects:
        if "." in name and "remove" in name:
            dot_remove += 1
            list_remove = name
        elif isinstance(obj, list):
            the_list = name

    if dot_remove == 1 and not the_list:
        # for a literal [1, 2].remove(3)
        the_list = list_remove.replace("remove", "").strip()[:-1]

    if dot_remove == 1 and the_list:
        item = bad_line.replace(list_remove, "").strip()[1:-1].strip()
        the_list = list_remove.replace("remove", "").strip()[:-1]
        cause = _(
            "You have attempted to remove `{item}` from the list `{the_list}`.\n"
            "However, `{the_list}` does not contain `{item}`.\n"
        ).format(the_list=the_list, item=item)
    elif the_list:
        cause = _(
            "You have attempted to remove an item from the list `{the_list}`.\n"
            "However, `{the_list}` does not contain that item.\n"
        ).format(the_list=the_list)
    else:
        cause = _(
            "You have attempted to remove an item a list that does not contain such an item.\n"
        )
    return {"cause": cause} if cause else {}


@parser._add
def generic_explanation_already_exist(
    message: str, _tb_data: TracebackData
) -> CauseInfo:
    # See info_generic.py
    # TODO: add unit test
    pattern = "A description of `(.*)` means already exists:"
    match = re.search(pattern, message)
    if not match:
        return {}
    cause = _(
        "An explanation of `{exception}` already exists.\n"
        "If you think that a better explanation can be given, please file an issue.\n"
    ).format(exception=match[1])
    return {"cause": cause}