| 1 |
import re |
| 2 |
|
| 3 |
from ..about_warnings import WarningInfo, get_warning_parser |
| 4 |
from ..ft_gettext import current_lang |
| 5 |
from ..info_variables import convert_type |
| 6 |
from ..typing_info import CauseInfo |
| 7 |
|
| 8 |
parser = get_warning_parser(SyntaxWarning) |
| 9 |
_ = current_lang.translate |
| 10 |
|
| 11 |
|
| 12 |
@parser._add |
| 13 |
def object_is_not_callable(message: str, _warning_data: WarningInfo) -> CauseInfo: |
| 14 |
pattern = re.compile("'(.*)' object is not callable") |
| 15 |
match = re.match(pattern, message) |
| 16 |
if not match: |
| 17 |
return {} |
| 18 |
|
| 19 |
obj_type = match[1] |
| 20 |
if obj_type == "NoneType": |
| 21 |
none_type = _( |
| 22 |
"\nNote: `NoneType` means that the object has a value of `None`.\n" |
| 23 |
) |
| 24 |
else: |
| 25 |
none_type = "" |
| 26 |
|
| 27 |
cause = _( |
| 28 |
"Python indicates that you have an object of type `{obj_type}`,\n" |
| 29 |
"followed by something surrounded by parentheses, `(...)`,\n" |
| 30 |
"which Python took as an indication of a function call.\n" |
| 31 |
"Either the object of type `{obj_type}` was meant to be a function,\n" |
| 32 |
"or you forgot a comma before `(...)`.\n" |
| 33 |
).format(obj_type=obj_type) |
| 34 |
|
| 35 |
return {"cause": cause + none_type} |
| 36 |
|
| 37 |
|
| 38 |
@parser._add |
| 39 |
def list_indices_must_be(message: str, _warning_data: WarningInfo) -> CauseInfo: |
| 40 |
pattern = re.compile(r"(.*) indices must be integers or slices, not (.*);") |
| 41 |
match = re.search(pattern, message) |
| 42 |
if match is None: |
| 43 |
return {} |
| 44 |
|
| 45 |
container_type = match[1] |
| 46 |
index_type = match[2] |
| 47 |
cause = _( |
| 48 |
"You have {container_type} followed by square brackets, `[...]`.\n" |
| 49 |
"What is included between the square brackets, `[...]`,\n" |
| 50 |
"must be either an integer or a slice\n" |
| 51 |
"(`start:stop` or `start:stop:step`) \n" |
| 52 |
"and you have used {obj_type} instead.\n" |
| 53 |
).format( |
| 54 |
container_type=convert_type(container_type), obj_type=convert_type(index_type) |
| 55 |
) |
| 56 |
|
| 57 |
return {"cause": cause} |
| 58 |
|
| 59 |
|
| 60 |
@parser._add |
| 61 |
def object_is_not_subscriptable(message: str, _warning_data: WarningInfo) -> CauseInfo: |
| 62 |
pattern = re.compile(r"'(.*)' object is not subscriptable") |
| 63 |
match = re.search(pattern, message) |
| 64 |
if match is None: |
| 65 |
return {} |
| 66 |
|
| 67 |
obj_type = match[1] |
| 68 |
if obj_type == "NoneType": |
| 69 |
none_type = _( |
| 70 |
"\nNote: `NoneType` means that the object has a value of `None`.\n" |
| 71 |
) |
| 72 |
else: |
| 73 |
none_type = "" |
| 74 |
|
| 75 |
cause = _( |
| 76 |
"Subscriptable objects are typically containers from which\n" |
| 77 |
"you can retrieve item using the notation `[...]`.\n" |
| 78 |
) |
| 79 |
cause += _( |
| 80 |
"Using this notation, you attempted to retrieve an item\n" |
| 81 |
"from an object of type `{obj_type}` which is not allowed.\n" |
| 82 |
).format(obj_type=obj_type) |
| 83 |
return {"cause": cause + none_type} |