Facilities for generating error messages during type checking.
Don't add any non-trivial message construction logic to the type checker, as it can compromise clarity and make messages less consistent. Add such logic to this module instead. Literal messages, including those with format args, should be defined as constants in mypy.message_registry.
Historically we tried to avoid all message string literals in the type checker but we are moving away from this convention.
Class | CollectAllInstancesQuery |
Undocumented |
Class | MessageBuilder |
Helper class for reporting type checker error messages with parameters. |
Function | append_invariance_notes |
Explain that the type is invariant and give notes for how to solve the issue. |
Function | best_matches |
Undocumented |
Function | callable_name |
Undocumented |
Function | capitalize |
Capitalize the first character of a string. |
Function | collect_all_instances |
Return all instances that t contains (including t ). |
Function | extract_type |
If the argument is the name of a method (of form C.m), return the type portion in quotes (e.g. "y"). Otherwise, return the string unmodified. |
Function | find_defining_module |
Undocumented |
Function | find_type_overlaps |
Return a set of fullnames that share a short name and appear in either type. |
Function | for_function |
Undocumented |
Function | format_item_name_list |
Undocumented |
Function | format_key_list |
Undocumented |
Function | format_string_list |
Undocumented |
Function | format_type |
Convert a type to a relatively short string suitable for error messages. |
Function | format_type_bare |
Convert a type to a relatively short string suitable for error messages. |
Function | format_type_distinctly |
Jointly format types to distinct strings. |
Function | format_type_inner |
Convert a type to a relatively short string suitable for error messages. |
Function | get_bad_protocol_flags |
Return all incompatible attribute flags for members that are present in both 'left' and 'right'. |
Function | get_conflict_protocol_types |
Find members that are defined in 'left' but have incompatible types. Return them as a list of ('member', 'got', 'expected'). |
Function | get_missing_protocol_members |
Find all protocol members of 'right' that are not implemented (i.e. completely missing) in 'left'. |
Function | make_inferred_type_note |
Explain that the user may have forgotten to type a variable. |
Function | plural_s |
Undocumented |
Function | pretty_callable |
Return a nice easily-readable representation of a callable type. For example: def [T <: int] f(self, x: int, y: T) -> None |
Function | pretty_class_or_static_decorator |
Return @classmethod or @staticmethod, if any, for the given callable type. |
Function | pretty_seq |
Undocumented |
Function | quote_type_string |
Quotes a type representation for use in messages. |
Function | strip_quotes |
Strip a double quote at the beginning and end of the string, if any. |
Function | variance_string |
Undocumented |
Constant | ARG_CONSTRUCTOR_NAMES |
Undocumented |
Constant | COMMON_MISTAKES |
Undocumented |
Constant | SUGGESTED_TEST_FIXTURES |
Undocumented |
Constant | TYPES_FOR_UNIMPORTED_HINTS |
Undocumented |
Undocumented
Parameters | |
current:str | Undocumented |
options:Iterable[ | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
name:str | Undocumented |
Returns | |
str | Undocumented |
Undocumented
Parameters | |
modules:Dict[ | Undocumented |
typ:CallableType | Undocumented |
Returns | |
Optional[ | Undocumented |
Return a set of fullnames that share a short name and appear in either type.
This is used to ensure that distinct types with the same short name are printed with their fullname.
Parameters | |
*types:Type | Undocumented |
Returns | |
Set[ | Undocumented |
Convert a type to a relatively short string suitable for error messages.
verbosity
is a coarse grained control on the verbosity of the type
This function returns a string appropriate for unmodified use in error messages; this means that it will be quoted in most cases. If modification of the formatted string is required, callers should use format_type_bare.
Parameters | |
typ:Type | Undocumented |
verbosity:int | Undocumented |
Returns | |
str | Undocumented |
Convert a type to a relatively short string suitable for error messages.
verbosity
is a coarse grained control on the verbosity of the type
fullnames
specifies a set of names that should be printed in full
This function will return an unquoted string. If a caller doesn't need to perform post-processing on the string output, format_type should be used instead. (The caller may want to use quote_type_string after processing has happened, to maintain consistent quoting in messages.)
Parameters | |
typ:Type | Undocumented |
verbosity:int | Undocumented |
fullnames:Optional[ | Undocumented |
Returns | |
str | Undocumented |
Jointly format types to distinct strings.
Increase the verbosity of the type strings until they become distinct while also requiring that distinct types with the same short name are formatted distinctly.
By default, the returned strings are created using format_type() and will be quoted accordingly. If bare is True, the returned strings will not be quoted; callers who need to do post-processing of the strings before quoting them (such as prepending * or **) should use this.
Parameters | |
*types:Type | Undocumented |
bare:bool | Undocumented |
Returns | |
Tuple[ | Undocumented |
Convert a type to a relatively short string suitable for error messages.
Parameters | |
typ:Type | Undocumented |
verbosity:int | Undocumented |
fullnames:Optional[ | Undocumented |
Returns | |
str | Undocumented |
Explain that the user may have forgotten to type a variable.
The user does not expect an error if the inferred container type is the same as the return type of a function and the argument type(s) are a subtype of the argument type(s) of the return type. This note suggests that they add a type annotation with the return type instead of relying on the inferred type.
Parameters | |
context:Context | Undocumented |
subtype:Type | Undocumented |
supertype:Type | Undocumented |
supertype_str:str | Undocumented |
Returns | |
str | Undocumented |
Return a nice easily-readable representation of a callable type. For example:
def [T <: int] f(self, x: int, y: T) -> None
Parameters | |
tp:CallableType | Undocumented |
Returns | |
str | Undocumented |
Parameters | |
tp:CallableType | Undocumented |
Returns | |
Optional[ | Undocumented |
Undocumented
Parameters | |
args:Sequence[ | Undocumented |
conjunction:str | Undocumented |
Returns | |
str | Undocumented |
Parameters | |
type_string:str | Undocumented |
Returns | |
str | Undocumented |
Parameters | |
s:str | Undocumented |
Returns | |
str | Undocumented |
Undocumented
Value |
|
Undocumented
Value |
|