| 1 |
"""UnboundLocalError cases""" |
| 2 |
|
| 3 |
import builtins |
| 4 |
import re |
| 5 |
|
| 6 |
from .. import debug_helper, info_variables |
| 7 |
from ..ft_gettext import current_lang |
| 8 |
from ..message_parser import get_parser |
| 9 |
from ..tb_data import TracebackData |
| 10 |
from ..typing_info import CauseInfo, SimilarNamesInfo |
| 11 |
|
| 12 |
parser = get_parser(UnboundLocalError) |
| 13 |
_ = current_lang.translate |
| 14 |
|
| 15 |
|
| 16 |
@parser._add |
| 17 |
def local_variable_referenced(message: str, tb_data: TracebackData) -> CauseInfo: |
| 18 |
pattern = re.compile(r"local variable '(.*)' referenced before assignment") |
| 19 |
pattern3_11 = re.compile( |
| 20 |
r"cannot access local variable '(.*)'" |
| 21 |
+ " where it is not associated with a value" |
| 22 |
) |
| 23 |
match = re.search(pattern, message) |
| 24 |
if not match: |
| 25 |
match = re.search(pattern3_11, message) |
| 26 |
if not match: |
| 27 |
return {} |
| 28 |
|
| 29 |
frame = tb_data.exception_frame |
| 30 |
unknown_name = match[1] |
| 31 |
basic_cause = _( |
| 32 |
"You're trying to use the name `{name}` identified by Python as being\n" |
| 33 |
"in the local scope of a function before having assigned it a value.\n" |
| 34 |
).format(name=unknown_name) |
| 35 |
|
| 36 |
scopes = info_variables.get_definition_scope(unknown_name, frame) |
| 37 |
if not scopes: |
| 38 |
similar = info_variables.get_similar_names(unknown_name, frame) |
| 39 |
all_similar_locals = similar["locals"] |
| 40 |
if all_similar_locals: |
| 41 |
similar_locals = [] |
| 42 |
for name in all_similar_locals: |
| 43 |
obj = info_variables.get_object_from_name(name, frame) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
if hasattr(obj, "__add__"): |
| 51 |
similar_locals.append(name) |
| 52 |
if similar_locals: |
| 53 |
first_guess = similar_locals[0] |
| 54 |
hint = _("Did you mean `{name}`?\n").format(name=first_guess) |
| 55 |
cause = format_similar_names(unknown_name, similar) |
| 56 |
return {"cause": cause, "suggest": hint} |
| 57 |
|
| 58 |
if "global" in scopes and "nonlocal" in scopes: |
| 59 |
cause = ( |
| 60 |
basic_cause |
| 61 |
+ "\n" |
| 62 |
+ _( |
| 63 |
"The name `{var_name}` exists in both the global and nonlocal scope.\n" |
| 64 |
"This can be rather confusing and is not recommended.\n" |
| 65 |
"Depending on which variable you wanted to refer to, you needed to add either\n\n" |
| 66 |
" global {var_name}\n\n" |
| 67 |
"or\n\n" |
| 68 |
" nonlocal {var_name}\n\n" |
| 69 |
"as the first line inside your function.\n" |
| 70 |
).format(var_name=unknown_name) |
| 71 |
) |
| 72 |
hint = _( |
| 73 |
"Did you forget to add either `global {var_name}` or \n" |
| 74 |
"`nonlocal {var_name}`?\n" |
| 75 |
).format(var_name=unknown_name) |
| 76 |
return {"cause": cause, "suggest": hint} |
| 77 |
|
| 78 |
if "global" in scopes: |
| 79 |
scope = "global" |
| 80 |
elif "nonlocal" in scopes: |
| 81 |
scope = "nonlocal" |
| 82 |
elif unknown_name in dir(builtins): |
| 83 |
return { |
| 84 |
"cause": _( |
| 85 |
"`{name}` is a Python builtin function.\n" |
| 86 |
"You have tried to assign a value to `{name}` inside a function\n" |
| 87 |
"while also using its original meaning in the function.\n\n" |
| 88 |
"Note that it is generally not a good idea to give a local variable\n" |
| 89 |
"the same name as a Python builtin function (like `{name}`).\n" |
| 90 |
).format(name=unknown_name) |
| 91 |
} |
| 92 |
else: |
| 93 |
debug_helper.log("problem in local_variable_referenced().") |
| 94 |
debug_helper.log("We have found variables in scopes") |
| 95 |
debug_helper.log("yet not in global nor nonlocal.") |
| 96 |
return {} |
| 97 |
|
| 98 |
cause = ( |
| 99 |
basic_cause |
| 100 |
+ "\n" |
| 101 |
+ _( |
| 102 |
"The name `{var_name}` exists in the {scope} scope.\n" |
| 103 |
"Perhaps the statement\n\n" |
| 104 |
" {scope} {var_name}\n\n" |
| 105 |
"should have been included as the first line inside your function.\n" |
| 106 |
).format(var_name=unknown_name, scope=scope) |
| 107 |
) |
| 108 |
hint = _("Did you forget to add `{scope} {var_name}`?\n").format( |
| 109 |
var_name=unknown_name, scope=scope |
| 110 |
) |
| 111 |
return {"cause": cause, "suggest": hint} |
| 112 |
|
| 113 |
|
| 114 |
def format_similar_names(unknown_name: str, similar: SimilarNamesInfo) -> str: |
| 115 |
"""This function formats the names that were found to be similar""" |
| 116 |
nb_similar_names = len(similar["locals"]) |
| 117 |
if nb_similar_names == 1: |
| 118 |
return ( |
| 119 |
_("The similar name `{name}` was found in the local scope. ").format( |
| 120 |
name=str(similar["locals"][0]).replace("'", "") |
| 121 |
) |
| 122 |
+ "\n" |
| 123 |
) |
| 124 |
message = _( |
| 125 |
"Instead of writing `{name}`, perhaps you meant one of the following:\n" |
| 126 |
).format(name=unknown_name) |
| 127 |
message += ( |
| 128 |
_("* Local scope: ") + str(similar["locals"])[1:-1].replace("'", "`") + "\n" |
| 129 |
) |
| 130 |
return message |