Exemplo n.º 1
0
    def get_type(self, value: Any) -> IType:
        """
        Returns the type of the given value.

        :param value: value to get the type
        :return: Returns the :class:`IType` of the the type of the value. `Type.none` by default.
        """
        # visits if it is a node
        if isinstance(value, ast.AST):
            fun_rtype_id: Any = ast.NodeVisitor.visit(self, value)
            if isinstance(fun_rtype_id, ast.Name):
                fun_rtype_id = fun_rtype_id.id

            if isinstance(fun_rtype_id,
                          str) and not isinstance(value, ast.Str):
                value = self.get_symbol(fun_rtype_id)
            else:
                value = fun_rtype_id

        if (isinstance(value, Attribute)
                and isinstance(value.attr_symbol, IExpression)
                and isinstance(value.attr_symbol.type, ClassType)):
            value = value.attr_symbol

        if isinstance(value, IType):
            return value
        elif isinstance(value, IExpression):
            return value.type
        elif isinstance(value, IOperation):
            return value.result
        else:
            return Type.get_type(value)
Exemplo n.º 2
0
    def get_types(cls, value: Any) -> Set[IType]:
        from boa3.model.type.type import Type
        if isinstance(value, IType):
            return {value}

        if not isinstance(value, Iterable):
            value = {value}

        types: Set[IType] = {
            val if isinstance(val, IType) else Type.get_type(val)
            for val in value
        }
        return cls.filter_types(types)
Exemplo n.º 3
0
    def get_type(self, value: Any, use_metatype: bool = False) -> IType:
        """
        Returns the type of the given value.

        :param value: value to get the type
        :param use_metatype: whether it should return `Type.type` if the value is an IType implementation.
        :return: Returns the :class:`IType` of the the type of the value. `Type.none` by default.
        """
        # visits if it is a node
        if isinstance(value, ast.AST):
            fun_rtype_id: Any = ast.NodeVisitor.visit(self, value)
            if isinstance(fun_rtype_id, ast.Name):
                fun_rtype_id = fun_rtype_id.id

            if isinstance(fun_rtype_id,
                          str) and not isinstance(value, ast.Str):
                value = self.get_symbol(fun_rtype_id, origin_node=value)
                if isinstance(value,
                              IType) and not isinstance(value, MetaType):
                    value = TypeUtils.type.build(
                        value) if use_metatype else value
            else:
                value = fun_rtype_id

        if isinstance(value, Attribute):
            if ((isinstance(value.attr_symbol, IExpression)
                 and isinstance(value.attr_symbol.type, ClassType))
                    or (isinstance(value.attr_symbol, IType))):
                value = value.attr_symbol
            elif isinstance(value.type, IType):
                value = value.type

        if isinstance(value, IType):
            final_type = value
        elif isinstance(value, IExpression):
            final_type = value.type
        elif isinstance(value, IOperation):
            final_type = value.result
        else:
            final_type = Type.get_type(value)

        if isinstance(final_type, MetaType) and not use_metatype:
            return final_type.meta_type
        else:
            return final_type