MCPcopy Index your code
hub / github.com/python/mypy / has_operator

Function has_operator

mypy/checkmember.py:1531–1563  ·  view source on GitHub ↗

Does type have operator with the given name? Note: this follows the rules for operator access, in particular: * __getattr__ is not considered * for class objects we only look in metaclass * instance level attributes (i.e. extra_attrs) are not considered

(typ: Type, op_method: str)

Source from the content-addressed store, hash-verified

1529
1530
1531def has_operator(typ: Type, op_method: str) -> bool:
1532 """Does type have operator with the given name?
1533
1534 Note: this follows the rules for operator access, in particular:
1535 * __getattr__ is not considered
1536 * for class objects we only look in metaclass
1537 * instance level attributes (i.e. extra_attrs) are not considered
1538 """
1539 # This is much faster than analyze_member_access, and so using
1540 # it first as a filter is important for performance. This is mostly relevant
1541 # in situations where we can't expect that method is likely present,
1542 # e.g. for __OP__ vs __rOP__.
1543 typ = get_proper_type(typ)
1544
1545 if isinstance(typ, TypeVarLikeType):
1546 typ = typ.values_or_bound()
1547 if isinstance(typ, AnyType):
1548 return True
1549 if isinstance(typ, UnionType):
1550 return all(has_operator(x, op_method) for x in typ.relevant_items())
1551 if isinstance(typ, FunctionLike) and typ.is_type_obj():
1552 return typ.fallback.type.has_readable_member(op_method)
1553 if isinstance(typ, TypeType):
1554 # Type[Union[X, ...]] is always normalized to Union[Type[X], ...],
1555 # so we don't need to care about unions here, but we need to care about
1556 # Type[T], where upper bound of T is a union.
1557 item = typ.item
1558 if isinstance(item, TypeVarType):
1559 item = item.values_or_bound()
1560 if isinstance(item, UnionType):
1561 return all(meta_has_operator(x, op_method) for x in item.relevant_items())
1562 return meta_has_operator(item, op_method)
1563 return instance_fallback(typ).type.has_readable_member(op_method)
1564
1565
1566def instance_fallback(typ: ProperType) -> Instance:

Callers 1

lookup_operatorMethod · 0.90

Calls 9

get_proper_typeFunction · 0.90
isinstanceFunction · 0.85
allFunction · 0.85
meta_has_operatorFunction · 0.85
instance_fallbackFunction · 0.85
values_or_boundMethod · 0.80
relevant_itemsMethod · 0.80
is_type_objMethod · 0.45
has_readable_memberMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…