File: error_in_def.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 (618 lines) | stat: -rw-r--r-- 21,782 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# type: ignore
"""In this module, we try to identify a remedy to a syntax error
associated with a 'def' statement.
"""
import platform
import sys

from .. import debug_helper, utils
from ..ft_gettext import current_lang, internal_error
from . import fixers
from . import syntax_utils as su

STATEMENT_ANALYZERS = []
_ = current_lang.translate
ASYNC = 0


def more_errors():
    return "\n" + _(
        "However, making such a change would still not correct\n"
        "all the syntax errors in the code you wrote.\n"
    )


def def_correct_syntax():
    async_ = "" if ASYNC == 0 else "async "
    # fmt: off
    return _(
        "The correct syntax is:\n\n"
        "    {async_}def name ( ... ):"
    ).format(async_=async_) + "\n"
    # fmt: on


def add_statement_analyzer(func):
    """A simple decorator that adds a function to the list
    of all functions that analyze a single statement."""
    STATEMENT_ANALYZERS.append(func)


# ========================================================
# Main calling function
# ========================================================


def analyze_def_statement(statement):
    """Analyzes the statement as identified by Python as that
    on which the error occurred."""
    global ASYNC
    if not statement.tokens:  # pragma: no cover
        debug_helper.log("Statement with no tokens in error_in_def.py")
        return {"cause": internal_error("No tokens")}

    ASYNC = 0
    if statement.tokens[0] == "async":
        ASYNC = 1

    if len(statement.tokens) > 1 + ASYNC and str(statement.tokens[1 + ASYNC]) in (
        "=",
        ":=",
    ):
        # Let the generic method handle the wrong assignment case
        return {}

    for analyzer in STATEMENT_ANALYZERS:
        cause = analyzer(statement)
        if cause:
            return cause
    return {}


@add_statement_analyzer
def def_begin_code_block(statement):  #
    # Thinking of trying to use def to begin a code block, i.e.
    # def : ...
    if statement.nb_tokens > 2 + ASYNC or statement.bad_token != ":":
        return {}

    if statement.first_token.start_col == 0:
        hint = _("A function needs a name.\n")
        cause = _(
            "You tried to define a function and did not use the correct syntax.\n"
        )
    else:
        hint = _("Functions and methods need a name.\n")
        cause = _(
            "You tried to define a function or method and did not use the correct syntax.\n"
        )

    return {"cause": cause + def_correct_syntax(), "suggest": hint}


@add_statement_analyzer
def missing_parens(statement):
    # Something like
    # def test: ...
    if (
        statement.bad_token != ":"
        and statement.nb_tokens >= 3 + ASYNC
        and statement.bad_token != statement.tokens[2 + ASYNC]
    ):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    new_statement = fixers.replace_token(
        statement.statement_tokens,
        statement.bad_token,
        "()" + statement.bad_token.string,
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you forget parentheses?\n")
        cause = _(
            "Perhaps you forgot to include parentheses.\n"
            "You might have meant to write\n\n"
            "    {line}\n"
        ).format(line=new_statement)
        return {"cause": cause, "suggest": hint}

    return {}


@add_statement_analyzer
def missing_parens_2(statement):
    # Something like
    # def test a, b:
    if statement.bad_token_index != 2 + ASYNC and statement.last_token != ":":
        return {}
    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    new_statement = fixers.replace_two_tokens(
        statement.statement_tokens,
        statement.bad_token,
        first_string="(" + statement.bad_token.string,
        second_token=statement.last_token,
        second_string="):",
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you forget parentheses?\n")
        cause = _(
            "Perhaps you forgot to include parentheses.\n"
            "You might have meant to write\n\n"
            "    {line}\n"
        ).format(line=new_statement)
        return {"cause": cause, "suggest": hint}

    return {}


@add_statement_analyzer
def missing_colon(statement):
    """look for missing colon at the end of statement; includes the case where
    something else has been written as a typo."""
    if (
        statement.last_token == ":"
        or statement.bad_token != statement.last_token
        or statement.statement_brackets
    ):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    cause = _("A function definition statement must end with a colon.\n")

    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, ":"
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you forget to write a colon?\n")
        cause += _("You wrote `{bad}` instead of a colon.\n").format(
            bad=statement.bad_token
        )
        return {"cause": cause, "suggest": hint}

    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, " "
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you write something by mistake after the colon?\n")
        cause += _("A block of code must come after the colon.\n")
        cause += _("Removing `{bad}`, might fix the problem.\n").format(
            bad=statement.bad_token
        )
        return {"cause": cause, "suggest": hint}

    new_statement = fixers.replace_token(
        statement.statement_tokens,
        statement.bad_token,
        statement.bad_token.string + ":",
    )
    if fixers.check_statement(new_statement):
        statement.location_markers = su.highlight_added_token(statement.bad_token, ":")
        hint = _("Did you forget to write a colon?\n")
        return {"cause": cause, "suggest": hint}

    return {}


@add_statement_analyzer
def keyword_as_function_name(statement):
    # Something like
    # def pass(): ...
    def_token = statement.tokens[ASYNC]
    if not (statement.bad_token.is_keyword() and statement.prev_token == def_token):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    hint = _("You cannot use a Python keyword as a function name.\n")
    cause = _(
        "You tried to use the Python keyword `{kwd}` as a function name.\n"
    ).format(kwd=statement.bad_token)

    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, "name"
    )
    if not fixers.check_statement(new_statement):
        cause += "\n" + _("There are more syntax errors later in your code.\n")

    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def other_invalid_function_names(statement):
    def_token = statement.tokens[ASYNC]
    if statement.bad_token.is_identifier() or not (statement.prev_token == def_token):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, "name"
    )
    if not fixers.check_statement(new_statement):
        return {}

    hint = _("You wrote an invalid function name.\n")
    cause = _(
        "The name of a function must be a valid Python identifier,\n"
        "that is a name that begins with a letter or an underscore character, `_`,\n"
        "and which contains only letters, digits or the underscore character.\n"
    )
    if statement.bad_token.is_string():
        cause += _("You attempted to use a string as a function name.\n")
        hint = cause
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def function_definition_missing_name(statement):
    def_token = statement.tokens[ASYNC]
    if not (
        def_token == "def"
        and statement.bad_token == "("
        and statement.prev_token == def_token
    ):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    cause = _("You forgot to name your function.\n")

    new_statement = fixers.replace_token(
        statement.tokens, statement.bad_token, "name ("
    )
    if fixers.check_statement(new_statement):
        return {"cause": cause + def_correct_syntax()}

    cause += _("However, there are some other syntax errors in your code.\n")
    return {"cause": cause + def_correct_syntax()}


@add_statement_analyzer
def keyword_not_allowed_as_function_argument(statement):
    if not (statement.bad_token.is_keyword() and statement.begin_brackets):
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, "name"
    )
    if not fixers.check_statement(new_statement):
        return {}

    cause = _(
        "I am guessing that you tried to use the Python keyword\n"
        "`{kwd}` as an argument in the definition of a function\n"
        "where an identifier (variable name) was expected.\n"
    ).format(kwd=statement.bad_token)

    return {"cause": cause}


@add_statement_analyzer
def dotted_name_not_allowed(statement):
    if not (statement.bad_token == "." and statement.prev_token.is_identifier()):
        return {}

    if statement.bad_token_index > 3 + ASYNC:
        cause = _("You cannot use dotted names as function arguments.\n")
        if statement.next_token.is_identifier():
            cause += _("Perhaps you meant to write a comma.\n")
            hint = _("Did you mean to write a comma?\n")
        else:
            hint = cause
    else:
        hint = cause = _("You cannot use dots in function names.\n")
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def positional_arguments_in_def(statement):
    if statement.bad_token != "/" or statement.prev_token.string not in "(,":
        return {}

    meaning = _(
        "`/` indicates that the previous arguments in a function definition\n"
        "are positional arguments.\n"
    )

    if sys.version_info < (3, 8):
        hint = _(
            "Function definitions cannot include the symbol `/` in this Python version.\n"
        )
        cause = meaning + _(
            "This symbol can only be used with Python versions 3.8.0 or newer.\n"
            "You are using Python version {version}.\n"
        ).format(version=platform.python_version())
        return {"cause": cause, "suggest": hint}

    prev_tok = ""
    for tok in statement.tokens[ASYNC : statement.bad_token_index]:
        if tok == "**":
            cause = meaning + _(
                "You have unspecified keyword arguments that appear before\n"
                "the symbol `/`.\n"
            )
            hint = _("Keyword arguments must appear after the `/` symbol.\n")  # yes
            return {"cause": cause, "suggest": hint}

        if prev_tok == "*":  # might be incorrect if used in *args
            if tok == ",":
                cause = meaning + _(
                    "However, `*` indicates that the arguments\n"
                    "that follow must be keyword arguments.\n"
                    "When they are used together, `/` must appear before `*`.\n"
                )
                hint = _("`*` must appear after `/` in a function definition.\n")
            else:
                hint = _(
                    "`*{name}` must appear after `/` in a function definition.\n"
                ).format(name=tok.string)
                cause = meaning + hint
            return {"cause": cause, "suggest": hint}

        if tok == "/" and prev_tok == ",":
            cause = _("You can only use `/` once in a function definition.\n")
            return {"cause": cause, "suggest": cause}

        prev_tok = tok

    return {}


@add_statement_analyzer
def keyword_arguments_in_def(statement):
    if statement.bad_token != "*" or statement.prev_token != ",":
        return {}

    args = statement.next_token if statement.next_token.is_identifier() else ""
    hint = _("You can only use `*` once in a function definition.\n")
    tokens = statement.tokens
    for index, tok in enumerate(tokens[ASYNC : statement.bad_token_index]):
        next_token = tokens[index + 1]
        if tok == "*":
            if next_token.is_identifier() and args:
                cause = hint + _(
                    "You have used it twice, with `*{first}` and `*{second}`.\n"
                ).format(first=next_token, second=args)
                return {"cause": cause, "suggest": hint}
            elif next_token.is_identifier():
                args = next_token.string

            if args:
                cause = hint + _(
                    "It must either be used by itself, `*`,\n"
                    "or in the form `*{args}`, but not both.\n"
                ).format(args=args)
            else:
                cause = hint
            return {"cause": cause, "suggest": hint}
        elif tok == "**":
            if args:
                cause = _("`*{args}` must appear before `**{kwargs}`.\n").format(
                    args=args, kwargs=next_token
                )
            else:
                cause = _("`**{kwargs}` must appear after the `*` operator.\n").format(
                    kwargs=next_token
                )
            return {"cause": cause, "suggest": hint}

    debug_helper.log("New case to consider for * as bad token.")  # pragma: no cover
    return {"cause": hint, "suggest": hint}  # pragma: no cover


@add_statement_analyzer
def number_as_argument(statement):
    if not (statement.bad_token.is_number() and statement.prev_token.string in "(,"):
        return {}

    hint = _("You cannot use numbers as function arguments.\n")
    cause = _(
        "You used a number as an argument when defining a function.\n"
        "You can only use identifiers (variable names) as function arguments.\n"
    )
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def string_as_argument(statement):
    if not (statement.bad_token.is_string() and statement.prev_token.string in "(,"):
        return {}

    hint = _("You cannot use strings as function arguments.\n")
    cause = _(
        "You used a string as an argument when defining a function.\n"
        "You can only use identifiers (variable names) as function arguments.\n"
    )
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def tuple_as_argument(statement):
    if statement.bad_token != "(" or statement.prev_token.string not in "(,":
        return {}

    hint = _("You cannot have explicit tuples as function arguments.\n")

    cause = hint + _(
        "You can only use identifiers (variable names) as function arguments.\n"
        "Assign any tuple to a parameter and unpack it\n"
        "within the body of the function.\n"
    )
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def list_as_argument(statement):
    if statement.bad_token != "[" or statement.prev_token.string not in "(,":
        return {}

    hint = _("You cannot have explicit lists as function arguments.\n")
    cause = hint + _(
        "You can only use identifiers (variable names) as function arguments.\n"
    )
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def dict_or_set_as_argument(statement):
    if statement.bad_token != "{" or statement.prev_token.string not in "(,":
        return {}

    hint = _("You cannot have any explicit dict or set as function arguments.\n")
    cause = hint + _(
        "You can only use identifiers (variable names) as function arguments.\n"
    )
    return {"cause": cause, "suggest": hint}


@add_statement_analyzer
def operator_as_argument(statement):
    """This looks at various possible fixes when the bad token is an operator.
    The following cases are considered:
    1. operator instead of comma
    2. operator instead of equal sign
    """
    if not statement.bad_token.is_operator() or statement.prev_token == "def":
        return {}

    # Before we make any attempt at modifying the original code,
    # we make sure that our statement checker can properly identify that
    # the original code is invalid. See #205
    if fixers.check_statement(statement.bad_line):
        return {}

    no_op = _("You cannot have operators as function arguments.\n")

    # def test(a+b): -> def test(a, b):
    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, ","
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you mean to write a comma?\n")
        cause = no_op + _(
            "I suspect you made a typo and wrote `{op}` instead of a comma.\n"
            "The following statement contains no syntax error:\n\n"
            "    {new_statement}"
        ).format(op=statement.bad_token, new_statement=new_statement)
        return {"cause": cause, "suggest": hint}

    # def test(a=1, b+2):  -> def test(a=1, b=2):
    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, "="
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you mean to write an equal sign?\n")
        cause = no_op + _(
            "I suspect you made a typo and wrote `{op}` instead of an equal sign.\n"
            "The following statement contains no syntax error:\n\n"
            "    {new_statement}"
        ).format(op=statement.bad_token, new_statement=new_statement)
        return {"cause": cause, "suggest": hint}

    # def test(a,,b):  -> def test(a,b):
    new_statement = fixers.replace_token(
        statement.statement_tokens, statement.bad_token, ""
    )
    if fixers.check_statement(new_statement):
        hint = _("Did you mean to write `{op}`?\n").format(op=statement.bad_token)
        cause = _(
            "I suspect you made a typo and added `{op}` by mistake.\n"
            "The following statement contains no syntax error:\n\n"
            "    {new_statement}"
        ).format(op=statement.bad_token, new_statement=new_statement)
        if statement.bad_token != ",":
            cause = no_op + cause
        return {"cause": cause, "suggest": hint}

    # def test(a, **, b): -> def test(a, c, b):
    if statement.prev_token.string in ("(", ","):
        # prevent getting an error because of repeated argument
        unique_name = utils.unique_variable_name()
        new_statement = fixers.replace_token(
            statement.statement_tokens, statement.bad_token, unique_name
        )
        hint = _("You cannot use `{op}` as an argument.\n").format(
            op=statement.bad_token
        )
        if fixers.check_statement(new_statement):
            cause = _(
                "I suspect you made a typo and wrote `{op}` by mistake.\n"
                "If you replace it by a unique variable name, the result\n"
                "will contain no syntax error.\n"
            ).format(op=statement.bad_token)

        else:
            cause = (
                _(
                    "I suspect you made a typo and wrote `{op}` by mistake,\n"
                    "perhaps instead of writing an identifier (variable name).\n"
                )
                + more_errors()
            )
        return {"cause": cause, "suggest": hint}

    if statement.prev_token == "**":
        cause = _(
            "The `**` operator needs to be followed by an identifier (variable name).\n"
        )
        return {"cause": cause}

    return {}  # pragma: no cover


@add_statement_analyzer
def arg_after_kwarg(statement):
    """This is only for something with positional argument after **kwargs;
    the case where we have a positional argument after a named argument,
    (..., a=1, b, ...) gets a specific error message.
    """
    if not (
        statement.bad_token.is_identifier()
        and statement.prev_token == ","
        and statement.next_token in (",", ")")
    ):
        return {}

    for tok in statement.tokens[ASYNC : statement.bad_token_index]:
        if tok == "**":
            hint = _("Positional arguments must come before keyword arguments.\n")
            cause = hint + _(
                "`{arg}` is a positional argument that appears after one or more\n"
                "keyword arguments in your function definition.\n"
            ).format(arg=statement.bad_token.string)
            return {"cause": cause, "suggest": hint}

    return {}