MCPcopy
hub / github.com/python/mypy / report_protocol_problems

Method report_protocol_problems

mypy/messages.py:2190–2417  ·  view source on GitHub ↗

Report possible protocol conflicts between 'subtype' and 'supertype'. This includes missing members, incompatible types, and incompatible attribute flags, such as settable vs read-only or class variable vs instance variable.

(
        self,
        subtype: Instance | TupleType | TypedDictType | TypeType | CallableType,
        supertype: Instance,
        context: Context,
        *,
        parent_error: ErrorInfo,
    )

Source from the content-addressed store, hash-verified

2188 )
2189
2190 def report_protocol_problems(
2191 self,
2192 subtype: Instance | TupleType | TypedDictType | TypeType | CallableType,
2193 supertype: Instance,
2194 context: Context,
2195 *,
2196 parent_error: ErrorInfo,
2197 ) -> None:
2198 """Report possible protocol conflicts between 'subtype' and 'supertype'.
2199
2200 This includes missing members, incompatible types, and incompatible
2201 attribute flags, such as settable vs read-only or class variable vs
2202 instance variable.
2203 """
2204 OFFSET = 4 # Four spaces, so that notes will look like this:
2205 # note: 'Cls' is missing following 'Proto' members:
2206 # note: method, attr
2207 MAX_ITEMS = 2 # Maximum number of conflicts, missing members, and overloads shown
2208 # List of special situations where we don't want to report additional problems
2209 exclusions: dict[type, list[str]] = {
2210 TypedDictType: ["typing.Mapping"],
2211 TupleType: ["typing.Iterable", "typing.Sequence"],
2212 }
2213 if supertype.type.fullname in exclusions.get(type(subtype), []):
2214 return
2215 if any(isinstance(tp, UninhabitedType) for tp in get_proper_types(supertype.args)):
2216 # We don't want to add notes for failed inference (e.g. Iterable[Never]).
2217 # This will be only confusing a user even more.
2218 return
2219
2220 class_obj = False
2221 is_module = False
2222 skip = []
2223 if isinstance(subtype, TupleType):
2224 subtype = subtype.partial_fallback
2225 elif isinstance(subtype, TypedDictType):
2226 subtype = subtype.fallback
2227 elif isinstance(subtype, TypeType):
2228 if not isinstance(subtype.item, Instance):
2229 return
2230 class_obj = True
2231 subtype = subtype.item
2232 elif isinstance(subtype, CallableType):
2233 if subtype.is_type_obj():
2234 ret_type = get_proper_type(subtype.ret_type)
2235 if isinstance(ret_type, TupleType):
2236 ret_type = ret_type.partial_fallback
2237 if not isinstance(ret_type, Instance):
2238 return
2239 class_obj = True
2240 subtype = ret_type
2241 else:
2242 subtype = subtype.fallback
2243 skip = ["__call__"]
2244 if subtype.extra_attrs and subtype.extra_attrs.mod_name:
2245 is_module = True
2246
2247 # Report missing members

Callers 2

check_subtypeMethod · 0.80

Calls 15

noteMethod · 0.95
pretty_overloadMethod · 0.95
print_moreMethod · 0.95
get_proper_typesFunction · 0.90
get_proper_typeFunction · 0.90
plural_sFunction · 0.90
is_subtypeFunction · 0.90
erase_typeFunction · 0.90
typeClass · 0.85
anyFunction · 0.85
isinstanceFunction · 0.85

Tested by

no test coverage detected