| 1 |
"""Getting specific information for ImportError""" |
| 2 |
|
| 3 |
import re |
| 4 |
import sys |
| 5 |
from typing import List, Optional, Tuple |
| 6 |
|
| 7 |
from .. import debug_helper |
| 8 |
from ..ft_gettext import current_lang, please_report |
| 9 |
from ..message_parser import get_parser |
| 10 |
from ..path_info import path_utils |
| 11 |
from ..tb_data import TracebackData |
| 12 |
from ..typing_info import CauseInfo |
| 13 |
from ..utils import get_similar_words, list_to_string |
| 14 |
|
| 15 |
parser = get_parser(ImportError) |
| 16 |
_ = current_lang.translate |
| 17 |
|
| 18 |
|
| 19 |
@parser._add |
| 20 |
def partially_initialized_module(message: str, tb_data: TracebackData) -> CauseInfo: |
| 21 |
|
| 22 |
pattern = re.compile( |
| 23 |
r"cannot import name '(.*)' from partially initialized module '(.*)'" |
| 24 |
) |
| 25 |
match = re.search(pattern, message) |
| 26 |
if not match: |
| 27 |
return {} |
| 28 |
if "circular import" in message: |
| 29 |
return cannot_import_name_from( |
| 30 |
match[1], match[2], tb_data, add_circular_hint=False |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
return cannot_import_name_from(match[1], match[2], tb_data) |
| 35 |
|
| 36 |
|
| 37 |
@parser._add |
| 38 |
def _cannot_import_name_from(message: str, tb_data: TracebackData) -> CauseInfo: |
| 39 |
|
| 40 |
pattern = re.compile(r"cannot import name '(.*)' from '(.*)'") |
| 41 |
match = re.search(pattern, message) |
| 42 |
return cannot_import_name_from(match[1], match[2], tb_data) if match else {} |
| 43 |
|
| 44 |
|
| 45 |
@parser._add |
| 46 |
def _cannot_import_name(message: str, tb_data: TracebackData) -> CauseInfo: |
| 47 |
|
| 48 |
pattern = re.compile(r"cannot import name '(.*)'") |
| 49 |
match = re.search(pattern, message) |
| 50 |
return cannot_import_name(match[1], tb_data) if match else {} |
| 51 |
|
| 52 |
|
| 53 |
def cannot_import_name_from( |
| 54 |
name: str, module: str, tb_data: TracebackData, add_circular_hint: bool = True |
| 55 |
) -> CauseInfo: |
| 56 |
hint = None |
| 57 |
circular_info = None |
| 58 |
|
| 59 |
modules_imported = extract_import_data_from_traceback(tb_data) |
| 60 |
if modules_imported: |
| 61 |
circular_info = find_circular_import(modules_imported) |
| 62 |
if circular_info and add_circular_hint: |
| 63 |
hint = _("You have a circular import.\n") |
| 64 |
|
| 65 |
|
| 66 |
cause = _( |
| 67 |
"The object that could not be imported is `{name}`.\n" |
| 68 |
"The module or package where it was \n" |
| 69 |
"expected to be found is `{module}`.\n" |
| 70 |
).format(name=name, module=module) |
| 71 |
|
| 72 |
if circular_info: |
| 73 |
if hint is None: |
| 74 |
return {"cause": cause + "\n" + circular_info} |
| 75 |
|
| 76 |
return {"cause": cause + "\n" + circular_info, "suggest": hint} |
| 77 |
|
| 78 |
if not add_circular_hint: |
| 79 |
debug_helper.log("New example to add") |
| 80 |
return { |
| 81 |
"cause": cause |
| 82 |
+ "\n" |
| 83 |
+ _( |
| 84 |
"Python indicated that you have a circular import.\n" |
| 85 |
"This can occur if executing the code in module 'A'\n" |
| 86 |
"results in executing the code in module 'B' where\n" |
| 87 |
"an attempt to import a name from module 'A' is made\n" |
| 88 |
"before the execution of the code in module 'A' had been completed.\n" |
| 89 |
) |
| 90 |
} |
| 91 |
|
| 92 |
try: |
| 93 |
mod = sys.modules[module] |
| 94 |
except Exception: |
| 95 |
cause += "\n" + _( |
| 96 |
"Inconsistent state: `'{module}'` was apparently not imported.\n" |
| 97 |
"As a result, no further analysis can be done.\n" |
| 98 |
).format(module=module) |
| 99 |
return {"cause": cause} |
| 100 |
|
| 101 |
similar = get_similar_words(name, dir(mod)) |
| 102 |
if not similar: |
| 103 |
return {"cause": cause} |
| 104 |
|
| 105 |
if len(similar) == 1: |
| 106 |
hint = _("Did you mean `{name}`?\n").format(name=similar[0]) |
| 107 |
cause = _( |
| 108 |
"Perhaps you meant to import `{correct}` (from `{module}`) " |
| 109 |
"instead of `{typo}`\n" |
| 110 |
).format(correct=similar[0], typo=name, module=module) |
| 111 |
else: |
| 112 |
candidates = list_to_string(similar) |
| 113 |
hint = _("Did you mean one of the following: `{names}`?\n").format( |
| 114 |
names=candidates |
| 115 |
) |
| 116 |
cause = _( |
| 117 |
"Instead of trying to import `{typo}` from `{module}`, \n" |
| 118 |
"perhaps you meant to import one of \n" |
| 119 |
"the following names which are found in module `{module}`:\n" |
| 120 |
"`{candidates}`\n" |
| 121 |
).format(candidates=candidates, typo=name, module=module) |
| 122 |
|
| 123 |
return {"cause": cause, "suggest": hint} |
| 124 |
|
| 125 |
|
| 126 |
def cannot_import_name(name: str, tb_data: TracebackData) -> CauseInfo: |
| 127 |
|
| 128 |
pattern = re.compile(r"from (.*) import") |
| 129 |
match = re.search(pattern, tb_data.bad_line) |
| 130 |
|
| 131 |
if not match: |
| 132 |
debug_helper.log("New example to consider.") |
| 133 |
return { |
| 134 |
"cause": _("The object that could not be imported is `{name}`.\n").format( |
| 135 |
name=name |
| 136 |
) |
| 137 |
+ please_report() |
| 138 |
} |
| 139 |
|
| 140 |
return cannot_import_name_from(name, match[1], tb_data) |
| 141 |
|
| 142 |
|
| 143 |
Modules = List[Tuple[str, str]] |
| 144 |
|
| 145 |
|
| 146 |
def extract_import_data_from_traceback(tb_data: TracebackData) -> Modules: |
| 147 |
"""Attempts to extract the list of imported modules from the traceback information""" |
| 148 |
pattern_file = re.compile(r'^File "(.*)", line', re.M) |
| 149 |
pattern_from = re.compile(r"^from (.*) import", re.M) |
| 150 |
pattern_import = re.compile(r"^import (.*)", re.M) |
| 151 |
modules_imported = [] |
| 152 |
tb_lines = tb_data.simulated_python_traceback.split("\n") |
| 153 |
current_file = "" |
| 154 |
for line in tb_lines: |
| 155 |
line = line.strip() |
| 156 |
match_file = re.search(pattern_file, line) |
| 157 |
match_from = re.search(pattern_from, line) |
| 158 |
match_import = re.search(pattern_import, line) |
| 159 |
|
| 160 |
if match_file: |
| 161 |
current_file = path_utils.shorten_path(match_file[1]) |
| 162 |
elif match_from or match_import: |
| 163 |
if match_from: |
| 164 |
modules_imported.append((current_file, match_from[1])) |
| 165 |
else: |
| 166 |
module = match_import[1] |
| 167 |
if "," in module: |
| 168 |
modules = module.split(",") |
| 169 |
for mod in modules: |
| 170 |
modules_imported.append( |
| 171 |
(current_file, mod.replace("(", "").strip()) |
| 172 |
) |
| 173 |
else: |
| 174 |
modules_imported.append((current_file, module)) |
| 175 |
current_file = "" |
| 176 |
|
| 177 |
return modules_imported |
| 178 |
|
| 179 |
|
| 180 |
def find_circular_import(modules_imported: Modules) -> Optional[str]: |
| 181 |
"""This attempts to find circular imports.""" |
| 182 |
last_file, last_module = modules_imported[-1] |
| 183 |
|
| 184 |
for file, module in modules_imported[:-1]: |
| 185 |
if module == last_module: |
| 186 |
return _( |
| 187 |
"The problem was likely caused by what is known as a 'circular import'.\n" |
| 188 |
"First, Python imported and started executing the code in file\n" |
| 189 |
" '{file}'.\n" |
| 190 |
"which imports module `{last_module}`.\n" |
| 191 |
"During this process, the code in another file,\n" |
| 192 |
" '{last_file}'\n" |
| 193 |
"was executed. However in this last file, an attempt was made\n" |
| 194 |
"to import the original module `{last_module}`\n" |
| 195 |
"a second time, before Python had completed the first import.\n" |
| 196 |
).format( |
| 197 |
file=file, last_file=last_file, module=module, last_module=last_module |
| 198 |
) |