def instance_has_bool(class_def: nodes.ClassDef) -> bool: try: class_def.getattr("__bool__") return True except astroid.AttributeInferenceError: ... return False
def apply_type_shim(cls: ClassDef, _context: Any = None) -> Iterator[ClassDef]: """ Morphs model fields to representative type """ base_nodes: List[ClassDef] = [cls] # Use the type inference standard try: base_nodes.extend(list(cls.getattr("field_type")[0].infer())) except AstroidError: pass return iter(base_nodes)
def _forbid_class_getitem_access(node: nodes.ClassDef) -> None: """ Disable the access to __class_getitem__ method for the node in parameters """ def full_raiser(origin_func, attr, *args, **kwargs): """ Raises an AttributeInferenceError in case of access to __class_getitem__ method. Otherwise just call origin_func. """ if attr == "__class_getitem__": raise AttributeInferenceError( "__class_getitem__ access is not allowed") return origin_func(attr, *args, **kwargs) try: node.getattr("__class_getitem__") # If we are here, then we are sure to modify object that do have __class_getitem__ method (which origin is one the # protocol defined in collections module) whereas the typing module consider it should not # We do not want __class_getitem__ to be found in the classdef partial_raiser = partial(full_raiser, node.getattr) node.getattr = partial_raiser except AttributeInferenceError: pass