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

Function find_member

mypy/subtypes.py:1331–1403  ·  view source on GitHub ↗
(
    name: str,
    itype: Instance,
    subtype: Type,
    *,
    is_operator: bool = False,
    class_obj: bool = False,
    is_lvalue: bool = False,
)

Source from the content-addressed store, hash-verified

1329
1330
1331def find_member(
1332 name: str,
1333 itype: Instance,
1334 subtype: Type,
1335 *,
1336 is_operator: bool = False,
1337 class_obj: bool = False,
1338 is_lvalue: bool = False,
1339) -> Type | None:
1340 type_checker = checker_state.type_checker
1341 if type_checker is None:
1342 # Unfortunately, there are many scenarios where someone calls is_subtype() before
1343 # type checking phase. In this case we fallback to old (incomplete) logic.
1344 # TODO: reduce number of such cases (e.g. semanal_typeargs, post-semanal plugins).
1345 return find_member_simple(
1346 name, itype, subtype, is_operator=is_operator, class_obj=class_obj, is_lvalue=is_lvalue
1347 )
1348
1349 # We don't use ATTR_DEFINED error code below (since missing attributes can cause various
1350 # other error codes), instead we perform quick node lookup with all the fallbacks.
1351 info = itype.type
1352 sym = info.get(name)
1353 node = sym.node if sym else None
1354 if not node:
1355 name_not_found = True
1356 if (
1357 name not in ["__getattr__", "__setattr__", "__getattribute__"]
1358 and not is_operator
1359 and not class_obj
1360 and itype.extra_attrs is None # skip ModuleType.__getattr__
1361 ):
1362 for method_name in ("__getattribute__", "__getattr__"):
1363 method = info.get_method(method_name)
1364 if method and method.info.fullname != "builtins.object":
1365 name_not_found = False
1366 break
1367 if name_not_found:
1368 if info.fallback_to_any or class_obj and info.meta_fallback_to_any:
1369 return AnyType(TypeOfAny.special_form)
1370 if itype.extra_attrs and name in itype.extra_attrs.attrs:
1371 return itype.extra_attrs.attrs[name]
1372 return None
1373
1374 from mypy.checkmember import (
1375 MemberContext,
1376 analyze_class_attribute_access,
1377 analyze_instance_member_access,
1378 )
1379
1380 mx = MemberContext(
1381 is_lvalue=is_lvalue,
1382 is_super=False,
1383 is_operator=is_operator,
1384 original_type=TypeType.make_normalized(itype) if class_obj else itype,
1385 self_type=TypeType.make_normalized(subtype) if class_obj else subtype,
1386 context=Context(), # all errors are filtered, but this is a required argument
1387 chk=type_checker,
1388 suppress_errors=True,

Callers 15

check_subtypeMethod · 0.90
get_arg_infer_passesMethod · 0.90
unpack_callback_protocolFunction · 0.90
is_overlapping_typesFunction · 0.90
get_bad_protocol_flagsFunction · 0.90
visit_instanceMethod · 0.85
visit_callable_typeMethod · 0.85
visit_overloadedMethod · 0.85

Calls 11

named_typeMethod · 0.95
AnyTypeClass · 0.90
MemberContextClass · 0.90
ContextClass · 0.90
find_member_simpleFunction · 0.85
make_normalizedMethod · 0.80
filter_errorsMethod · 0.80
getMethod · 0.45
get_methodMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…