allow type guard on unbounded type variables

This commit is contained in:
pca006132 2020-12-23 17:08:04 +08:00 committed by pca006132
parent 07bc512e59
commit a46edc8a47

View File

@ -126,12 +126,12 @@ def parse_if_stmt(ctx: Context,
(isinstance(node.test.ops[0], ast.Eq) or\ (isinstance(node.test.ops[0], ast.Eq) or\
isinstance(node.test.ops[0], ast.NotEq)): isinstance(node.test.ops[0], ast.NotEq)):
t = parse_expr(ctx, sym_table, node.test.left.args[0]) t = parse_expr(ctx, sym_table, node.test.left.args[0])
if not isinstance(t, TypeVariable) or len(t.constraints) < 2: if not isinstance(t, TypeVariable):
raise CustomError( raise CustomError(
'type guard only support basic bounded type variables', 'type guard only support type variables',
node.test) node.test)
t1, _ = parse_type(ctx, node.test.comparators[0]) t1, _ = parse_type(ctx, node.test.comparators[0])
if t1 not in t.constraints: if len(t.constraints) > 0 and t1 not in t.constraints:
raise CustomError( raise CustomError(
f'{t1} is not a possible instance of {t}', f'{t1} is not a possible instance of {t}',
node.test) node.test)