-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate checkstrformat to use ErrorMessage
class
#7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,10 +112,10 @@ def __str__(self) -> str: | |
VALID_NEWTYPE: Final = ErrorCode( | ||
"valid-newtype", "Check that argument 2 to NewType is valid", "General" | ||
) | ||
STRING_FORMATTING: Final = ErrorCode( | ||
STRING_FORMATTING: Final[ErrorCode] = ErrorCode( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhancement: Adding |
||
"str-format", "Check that string formatting/interpolation is type-safe", "General" | ||
) | ||
STR_BYTES_PY3: Final = ErrorCode( | ||
STR_BYTES_PY3: Final[ErrorCode] = ErrorCode( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consistency: Good job applying the same |
||
"str-bytes-safe", "Warn about implicit coercions related to bytes and string types", "General" | ||
) | ||
EXIT_RETURN: Final = ErrorCode( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,3 +316,74 @@ def with_additional_msg(self, info: str) -> ErrorMessage: | |
ARG_NAME_EXPECTED_STRING_LITERAL: Final = ErrorMessage( | ||
"Expected string literal for argument name, got {}", codes.SYNTAX | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improvement: Good refactoring to centralize error messages in |
||
FORMAT_STR_INVALID_SPECIFIER: Final = ErrorMessage( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider adding a docstring for this new section of error messages to explain that these are all related to string formatting. |
||
"Invalid conversion specifier in format string", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_BRACES_IN_SPECIFIER: Final = ErrorMessage( | ||
"Conversion value must not contain { or }", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_NESTING_ATMOST_TWO_LEVELS: Final = ErrorMessage( | ||
"Formatting nesting must be at most two levels deep", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_UNEXPECTED_RBRACE: Final = ErrorMessage( | ||
"Invalid conversion specifier in format string: unexpected }", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_UNMATCHED_LBRACE: Final = ErrorMessage( | ||
"Invalid conversion specifier in format string: unmatched {", codes.STRING_FORMATTING | ||
) | ||
UNRECOGNIZED_FORMAT_SPEC: Final = ErrorMessage( | ||
'Unrecognized format specification "{}"', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_INVALID_CONVERSION_TYPE: Final = ErrorMessage( | ||
'Invalid conversion type "{}", must be one of "r", "s" or "a"', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_BYTES_USE_REPR: Final = ErrorMessage( | ||
'If x = b\'abc\' then f"{x}" or "{}".format(x) produces "b\'abc\'", ' | ||
'not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). ' | ||
"Otherwise, decode the bytes", | ||
codes.STR_BYTES_PY3, | ||
) | ||
FORMAT_STR_BYTES_USE_REPR_OLD: Final = ErrorMessage( | ||
'If x = b\'abc\' then "%s" % x produces "b\'abc\'", not "abc". ' | ||
'If this is desired behavior use "%r" % x. Otherwise, decode the bytes', | ||
codes.STR_BYTES_PY3, | ||
) | ||
FORMAT_STR_INVALID_NUMERIC_FLAG: Final = ErrorMessage( | ||
"Numeric flags are only allowed for numeric types", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_REPLACEMENT_NOT_FOUND: Final = ErrorMessage( | ||
"Cannot find replacement for positional format specifier {}", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_NAMED_REPLACEMENT_NOT_FOUND: Final = ErrorMessage( | ||
'Cannot find replacement for named format specifier "{}"', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_PARTIAL_FIELD_NUMBERING: Final = ErrorMessage( | ||
"Cannot combine automatic field numbering and manual field specification", | ||
codes.STRING_FORMATTING, | ||
) | ||
FORMAT_STR_SYNTAX_ERROR: Final = ErrorMessage( | ||
'Syntax error in format specifier "{}"', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_INVALID_ACCESSOR_EXPR: Final = ErrorMessage( | ||
'Only index and member expressions are allowed in format field accessors; got "{}"', | ||
codes.STRING_FORMATTING, | ||
) | ||
FORMAT_STR_INVALID_INDEX_ACCESSOR: Final = ErrorMessage( | ||
'Invalid index expression in format field accessor "{}"', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_BYTES_ABOVE_PY35: Final = ErrorMessage( | ||
"Bytes formatting is only supported in Python 3.5 and later", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_BYTES_DICT_KEYS_MUST_BE_BYTES: Final = ErrorMessage( | ||
"Dictionary keys in bytes formatting must be bytes, not strings", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_BYTES_REQUIRED_PY3: Final = ErrorMessage( | ||
"On Python 3 b'%s' requires bytes, not string", codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_INVALID_BYTES_SPECIFIER_PY35: Final = ErrorMessage( | ||
'Format character "b" is only supported in Python 3.5 and later', codes.STRING_FORMATTING | ||
) | ||
FORMAT_STR_INVALID_BYTES_SPECIFIER: Final = ErrorMessage( | ||
'Format character "b" is only supported on bytes patterns', codes.STRING_FORMATTING | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
import re | ||
from contextlib import contextmanager | ||
from textwrap import dedent | ||
from typing import Any, Callable, Collection, Iterable, Iterator, List, Sequence, cast | ||
from typing import Any, Callable, Collection, Iterable, Iterator, List, Sequence, cast, overload | ||
from typing_extensions import Final | ||
|
||
import mypy.typeops | ||
|
@@ -269,6 +269,7 @@ def span_from_context(ctx: Context) -> Iterable[int]: | |
allow_dups=allow_dups, | ||
) | ||
|
||
@overload | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhancement: The |
||
def fail( | ||
self, | ||
msg: str, | ||
|
@@ -278,10 +279,40 @@ def fail( | |
file: str | None = None, | ||
allow_dups: bool = False, | ||
secondary_context: Context | None = None, | ||
) -> None: | ||
... | ||
|
||
@overload | ||
def fail( | ||
self, | ||
msg: message_registry.ErrorMessage, | ||
context: Context | None, | ||
*, | ||
file: str | None = None, | ||
allow_dups: bool = False, | ||
secondary_context: Context | None = None, | ||
) -> None: | ||
... | ||
|
||
def fail( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improvement: The implementation of |
||
self, | ||
msg: str | message_registry.ErrorMessage, | ||
context: Context | None, | ||
*, | ||
code: ErrorCode | None = None, | ||
file: str | None = None, | ||
allow_dups: bool = False, | ||
secondary_context: Context | None = None, | ||
) -> None: | ||
"""Report an error message (unless disabled).""" | ||
if isinstance(msg, message_registry.ErrorMessage): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: Consider adding a type annotation for the local variables |
||
msg_str = msg.value | ||
code = msg.code | ||
else: | ||
msg_str = msg | ||
|
||
self.report( | ||
msg, | ||
msg_str, | ||
context, | ||
"error", | ||
code=code, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a code to the
fail
call, for consistency with the other calls in this file. This will also allow users to filter this error specifically.