module documentation

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_callable_args Format a bunch of Callable arguments into a string
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 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
Function wrong_type_arg_count Undocumented
Constant ARG_CONSTRUCTOR_NAMES Undocumented
Constant COMMON_MISTAKES Undocumented
Constant SUGGESTED_TEST_FIXTURES Undocumented
Constant TYPES_FOR_UNIMPORTED_HINTS Undocumented
Function _real_quick_ratio Undocumented
def append_invariance_notes(notes: list[str], arg_type: Instance, expected_type: Instance) -> list[str]: (source)

Explain that the type is invariant and give notes for how to solve the issue.

def best_matches(current: str, options: Collection[str], n: int) -> list[str]: (source)

Undocumented

def callable_name(type: FunctionLike) -> str|None: (source)

Undocumented

def capitalize(s: str) -> str: (source)

Capitalize the first character of a string.

def collect_all_instances(t: Type) -> list[Instance]: (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.

def extract_type(name: str) -> str: (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.

def find_defining_module(modules: dict[str, MypyFile], typ: CallableType) -> MypyFile|None: (source)

Undocumented

def find_type_overlaps(*types: Type) -> set[str]: (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.

def for_function(callee: CallableType) -> str: (source)

Undocumented

def format_callable_args(arg_types: list[Type], arg_kinds: list[ArgKind], arg_names: list[str|None], format: Callable[[Type], str], verbosity: int) -> str: (source)

Format a bunch of Callable arguments into a string

def format_item_name_list(s: Iterable[str]) -> str: (source)

Undocumented

def format_key_list(keys: list[str], *, short: bool = False) -> str: (source)

Undocumented

def format_string_list(lst: list[str]) -> str: (source)

Undocumented

def format_type(typ: Type, verbosity: int = 0, module_names: bool = False) -> str: (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.

def format_type_bare(typ: Type, verbosity: int = 0, module_names: bool = False) -> str: (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.)

def format_type_distinctly(*types: Type, bare: bool = False) -> tuple[str, ...]: (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.

def format_type_inner(typ: Type, verbosity: int, fullnames: set[str]|None, module_names: bool = False) -> str: (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

def get_bad_protocol_flags(left: Instance, right: Instance, class_obj: bool = False) -> list[tuple[str, set[int], set[int]]]: (source)

Return all incompatible attribute flags for members that are present in both 'left' and 'right'.

def get_conflict_protocol_types(left: Instance, right: Instance, class_obj: bool = False) -> list[tuple[str, Type, Type]]: (source)

Find members that are defined in 'left' but have incompatible types. Return them as a list of ('member', 'got', 'expected').

def get_missing_protocol_members(left: Instance, right: Instance, skip: list[str]) -> list[str]: (source)

Find all protocol members of 'right' that are not implemented (i.e. completely missing) in 'left'.

def make_inferred_type_note(context: Context, subtype: Type, supertype: Type, supertype_str: str) -> 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.

def pretty_callable(tp: CallableType, skip_self: bool = False) -> str: (source)

Return a nice easily-readable representation of a callable type. For example: def [T <: int] f(self, x: int, y: T) -> None If skip_self is True, print an actual callable type, as it would appear when bound on an instance/class, rather than how it would appear in the defining statement.

def pretty_class_or_static_decorator(tp: CallableType) -> str|None: (source)

Return @classmethod or @staticmethod, if any, for the given callable type.

def pretty_seq(args: Sequence[str], conjunction: str) -> str: (source)

Undocumented

def quote_type_string(type_string: str) -> str: (source)

Quotes a type representation for use in messages.

def strip_quotes(s: str) -> str: (source)

Strip a double quote at the beginning and end of the string, if any.

def variance_string(variance: int) -> str: (source)

Undocumented

def wrong_type_arg_count(n: int, act: str, name: str) -> str: (source)

Undocumented

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.set': 'set.pyi',
 'builtins.tuple': 'tuple.pyi',
 'builtins.bool': 'bool.pyi',
 'builtins.Exception': 'exception.pyi',
 'builtins.BaseException': 'exception.pyi',
 'builtins.isinstance': 'isinstancelist.pyi',
 'builtins.property': 'property.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',
...
def _real_quick_ratio(a: str, b: str) -> float: (source)

Undocumented