module documentation
(source)

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 ​Collect​All​Instances​Query Undocumented
Class ​Message​Builder 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
def append_invariance_notes(notes, arg_type, expected_type): (source)
Explain that the type is invariant and give notes for how to solve the issue.
Parameters
notes:List[str]Undocumented
arg​_type:InstanceUndocumented
expected​_type:InstanceUndocumented
Returns
List[str]Undocumented
def best_matches(current, options): (source)

Undocumented

Parameters
current:strUndocumented
options:Iterable[str]Undocumented
Returns
List[str]Undocumented
def callable_name(type): (source)

Undocumented

Parameters
type:FunctionLikeUndocumented
Returns
Optional[str]Undocumented
def capitalize(s): (source)
Capitalize the first character of a string.
Parameters
s:strUndocumented
Returns
strUndocumented
def collect_all_instances(t): (source)

Return all instances that t contains (including t).

This is similar to collect_all_inner_types from typeanal but only returns instances and will recurse into fallbacks.

Parameters
t:TypeUndocumented
Returns
List[Instance]Undocumented
def extract_type(name): (source)
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.
Parameters
name:strUndocumented
Returns
strUndocumented
def find_defining_module(modules, typ): (source)

Undocumented

Parameters
modules:Dict[str, MypyFile]Undocumented
typ:CallableTypeUndocumented
Returns
Optional[MypyFile]Undocumented
def find_type_overlaps(*types): (source)

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:TypeUndocumented
Returns
Set[str]Undocumented
def for_function(callee): (source)

Undocumented

Parameters
callee:CallableTypeUndocumented
Returns
strUndocumented
def format_item_name_list(s): (source)

Undocumented

Parameters
s:Iterable[str]Undocumented
Returns
strUndocumented
def format_key_list(keys, *, short=False): (source)

Undocumented

Parameters
keys:List[str]Undocumented
short:boolUndocumented
Returns
strUndocumented
def format_string_list(lst): (source)

Undocumented

Parameters
lst:List[str]Undocumented
Returns
strUndocumented
def format_type(typ, verbosity=0): (source)

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:TypeUndocumented
verbosity:intUndocumented
Returns
strUndocumented
def format_type_bare(typ, verbosity=0, fullnames=None): (source)

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:TypeUndocumented
verbosity:intUndocumented
fullnames:Optional[Set[str]]Undocumented
Returns
strUndocumented
def format_type_distinctly(*types, bare=False): (source)

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:TypeUndocumented
bare:boolUndocumented
Returns
Tuple[str, ...]Undocumented
def format_type_inner(typ, verbosity, fullnames): (source)

Convert a type to a relatively short string suitable for error messages.

Args:
verbosity: a coarse grained control on the verbosity of the type fullnames: a set of names that should be printed in full
Parameters
typ:TypeUndocumented
verbosity:intUndocumented
fullnames:Optional[Set[str]]Undocumented
Returns
strUndocumented
def get_bad_protocol_flags(left, right): (source)
Return all incompatible attribute flags for members that are present in both 'left' and 'right'.
Parameters
left:InstanceUndocumented
right:InstanceUndocumented
Returns
List[Tuple[str, Set[int], Set[int]]]Undocumented
def get_conflict_protocol_types(left, right): (source)
Find members that are defined in 'left' but have incompatible types. Return them as a list of ('member', 'got', 'expected').
Parameters
left:InstanceUndocumented
right:InstanceUndocumented
Returns
List[Tuple[str, Type, Type]]Undocumented
def get_missing_protocol_members(left, right): (source)
Find all protocol members of 'right' that are not implemented (i.e. completely missing) in 'left'.
Parameters
left:InstanceUndocumented
right:InstanceUndocumented
Returns
List[str]Undocumented
def make_inferred_type_note(context, subtype, supertype, supertype_str): (source)

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:ContextUndocumented
subtype:TypeUndocumented
supertype:TypeUndocumented
supertype​_str:strUndocumented
Returns
strUndocumented
def plural_s(s): (source)

Undocumented

Parameters
s:Union[int, Sequence[Any]]Undocumented
Returns
strUndocumented
def pretty_callable(tp): (source)

Return a nice easily-readable representation of a callable type. For example:

def [T <: int] f(self, x: int, y: T) -> None
Parameters
tp:CallableTypeUndocumented
Returns
strUndocumented
def pretty_class_or_static_decorator(tp): (source)
Return @classmethod or @staticmethod, if any, for the given callable type.
Parameters
tp:CallableTypeUndocumented
Returns
Optional[str]Undocumented
def pretty_seq(args, conjunction): (source)

Undocumented

Parameters
args:Sequence[str]Undocumented
conjunction:strUndocumented
Returns
strUndocumented
def quote_type_string(type_string): (source)
Quotes a type representation for use in messages.
Parameters
type​_string:strUndocumented
Returns
strUndocumented
def strip_quotes(s): (source)
Strip a double quote at the beginning and end of the string, if any.
Parameters
s:strUndocumented
Returns
strUndocumented
def variance_string(variance): (source)

Undocumented

Parameters
variance:intUndocumented
Returns
strUndocumented
ARG_CONSTRUCTOR_NAMES = (source)

Undocumented

Value
{ARG_POS: 'Arg',
 ARG_OPT: 'DefaultArg',
 ARG_NAMED: 'NamedArg',
 ARG_NAMED_OPT: 'DefaultNamedArg',
 ARG_STAR: 'VarArg',
 ARG_STAR2: 'KwArg'}
COMMON_MISTAKES: Dict[str, Sequence[str]] = (source)

Undocumented

Value
{'add': ('append', 'extend')}
SUGGESTED_TEST_FIXTURES: dict[str, str] = (source)

Undocumented

Value
{'builtins.list': 'list.pyi',
 'builtins.dict': 'dict.pyi',
 'builtins.set': 'set.pyi',
 'builtins.tuple': 'tuple.pyi',
 'builtins.bool': 'bool.pyi',
 'builtins.Exception': 'exception.pyi',
 'builtins.BaseException': 'exception.pyi',
...
TYPES_FOR_UNIMPORTED_HINTS: set[str] = (source)

Undocumented

Value
set(['typing.Any',
     'typing.Callable',
     'typing.Dict',
     'typing.Iterable',
     'typing.Iterator',
     'typing.List',
     'typing.Optional',
...