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
|
import re
from ..about_warnings import WarningInfo, get_warning_parser
from ..ft_gettext import current_lang
from ..info_variables import convert_type
from ..typing_info import CauseInfo # for type checking only
parser = get_warning_parser(SyntaxWarning)
_ = current_lang.translate
@parser._add
def object_is_not_callable(message: str, _warning_data: WarningInfo) -> CauseInfo:
pattern = re.compile("'(.*)' object is not callable")
match = re.match(pattern, message)
if not match:
return {}
obj_type = match[1]
if obj_type == "NoneType":
none_type = _(
"\nNote: `NoneType` means that the object has a value of `None`.\n"
)
else:
none_type = ""
cause = _(
"Python indicates that you have an object of type `{obj_type}`,\n"
"followed by something surrounded by parentheses, `(...)`,\n"
"which Python took as an indication of a function call.\n"
"Either the object of type `{obj_type}` was meant to be a function,\n"
"or you forgot a comma before `(...)`.\n"
).format(obj_type=obj_type)
return {"cause": cause + none_type}
@parser._add
def list_indices_must_be(message: str, _warning_data: WarningInfo) -> CauseInfo:
pattern = re.compile(r"(.*) indices must be integers or slices, not (.*);")
match = re.search(pattern, message)
if match is None:
return {}
container_type = match[1]
index_type = match[2]
cause = _(
"You have {container_type} followed by square brackets, `[...]`.\n"
"What is included between the square brackets, `[...]`,\n"
"must be either an integer or a slice\n"
"(`start:stop` or `start:stop:step`) \n"
"and you have used {obj_type} instead.\n"
).format(
container_type=convert_type(container_type), obj_type=convert_type(index_type)
)
return {"cause": cause}
@parser._add
def object_is_not_subscriptable(message: str, _warning_data: WarningInfo) -> CauseInfo:
pattern = re.compile(r"'(.*)' object is not subscriptable")
match = re.search(pattern, message)
if match is None:
return {}
obj_type = match[1]
if obj_type == "NoneType":
none_type = _(
"\nNote: `NoneType` means that the object has a value of `None`.\n"
)
else:
none_type = ""
cause = _(
"Subscriptable objects are typically containers from which\n"
"you can retrieve item using the notation `[...]`.\n"
)
cause += _(
"Using this notation, you attempted to retrieve an item\n"
"from an object of type `{obj_type}` which is not allowed.\n"
).format(obj_type=obj_type)
return {"cause": cause + none_type}
|