fix some variable inference

This commit is contained in:
pca006132 2020-12-23 15:39:39 +08:00 committed by pca006132
parent 461d403cce
commit 3f4f23c0ab
3 changed files with 11 additions and 9 deletions

View File

@ -264,6 +264,9 @@ def parse_list_comprehension(ctx: Context,
if node.generators[0].is_async:
raise CustomError('async list comprehension is not supported', node)
ty = parse_expr(ctx, sym_table, node.generators[0].iter)
if isinstance(ty, TypeVariable) and \
len(ty.constraints) == 1:
ty = ty.constraints[0]
if not isinstance(ty, ListType):
raise CustomError(f'unable to iterate over {ty}', node)
try:

View File

@ -159,7 +159,11 @@ def parse_for_stmt(ctx: Context,
return_ty: Type,
node):
ty = parse_expr(ctx, sym_table, node.iter)
if isinstance(ty, TypeVariable) and \
len(ty.constraints) == 1:
ty = ty.constraints[0]
if not isinstance(ty, ListType):
raise CustomError('only iteration over list is supported', node.iter)
binding = parse_simple_binding(node.target, ty.params[0])
for key, value in binding.items():

View File

@ -126,17 +126,12 @@ def parse_type_var(ctx: Context, node):
raise CustomError('redefining type variable is not allowed', node)
constraints = []
for v in node.value.args[1:]:
if isinstance(v, ast.Constant):
value = v.value
elif isinstance(v, ast.Name):
value = v.id
else:
value, var = parse_type(ctx, v)
if len(var) > 0:
raise CustomError(
'TypeVar constraints must be either string or type name',
'constraints cannot contain type variables',
node)
if value not in ctx.types:
raise CustomError(f'unbounded type {value}', node)
constraints.append(ctx.types[value])
constraints.append(value)
ctx.variables[name] = TypeVariable(node.value.args[0].value, constraints)