def binop_error_message(node: astroid.BinOp, constraints: TypeConstraints) -> str: op_name = BINOP_TO_ENGLISH[node.op] left_type = _get_name(constraints.resolve(node.left.inf_type).getValue()) right_type = _get_name(constraints.resolve(node.right.inf_type).getValue()) hint = binary_op_hints(node.op, [left_type, right_type]) or '' return ( f'You cannot {op_name} {_correct_article(left_type)}, {node.left.as_string()}, ' f'and {_correct_article(right_type)}, {node.right.as_string()}. ' f'{hint}')
def subscript_error_message(node: astroid.Subscript, constraints: TypeConstraints) -> str: # Accessing an element of a List with an incompatible index type (non-integers) subscript_concrete_type = constraints.resolve( node.value.inf_type).getValue() if subscript_concrete_type is type(None): return f'NoneType is not subscriptable.' if not isinstance(subscript_concrete_type, _GenericAlias): subscript_gorg = subscript_concrete_type else: subscript_gorg = _gorg(subscript_concrete_type) if subscript_gorg is list: slice_type = _get_name(node.slice.inf_type.getValue()) return f'You can only access elements of a list using an int. ' \ f'You used {_correct_article(slice_type)}, {node.slice.value.as_string()}.' elif subscript_gorg is tuple: slice_type = _get_name(node.slice.inf_type.getValue()) return f'You can only access elements of a tuple using an int. ' \ f'You used {_correct_article(slice_type)}, {node.slice.value.as_string()}.' elif subscript_gorg is dict: slice_type = _get_name(node.slice.inf_type.getValue()) return f'You tried to access an element of this dictionary using ' \ f'{_correct_article(slice_type)}, {node.slice.value.as_string()}, ' \ f'but the keys are of type {_get_name(subscript_concrete_type.__args__[0])}.'
def unaryop_error_message(node: astroid.UnaryOp, constraints: TypeConstraints) -> str: op_name = UNARY_TO_ENGLISH[node.op] operand = _get_name(constraints.resolve(node.operand.inf_type).getValue()) return ( f'You cannot {op_name} {_correct_article(operand)}, {node.operand.as_string()}.' )
from typing import * from typing import TupleMeta, CallableMeta, _ForwardRef from python_ta.typecheck.base import TypeResult, TypeInfo, TypeFail, TypeConstraints, create_Callable from nose import SkipTest from nose.tools import eq_ skip_msg = "Skipped" tc = TypeConstraints() # Helper functions def unify_helper(arg1: type, arg2: type, exp_result: Union[type, TypeFail]): unify_result = TypeInfo(arg1) >> (lambda t1: TypeInfo(arg2) >> (lambda t2: tc.unify(t1, t2))) if isinstance(exp_result, TypeFail): assert isinstance(unify_result, TypeFail) else: eq_( tc.resolve(unify_result.getValue()).getValue(), tc.resolve(exp_result).getValue()) def setup_typevar(t: type): tv = tc.fresh_tvar(None) tc.unify(tv, t) return tv def resolve_helper(t: type, exp_type: type): eq_(tc.resolve(t).getValue(), exp_type)