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)
| 1529 | |
| 1530 | |
| 1531 | def 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 | |
| 1566 | def instance_fallback(typ: ProperType) -> Instance: |
no test coverage detected
searching dependent graphs…