Skip to content

Commit

Permalink
less restricted library type variables
Browse files Browse the repository at this point in the history
  • Loading branch information
fredokun committed Jun 13, 2023
1 parent c5cb24a commit 382067b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 8 additions & 6 deletions mrpython/typechecking/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,7 @@ def type_compare_SetType(expected_type, ctx, expr, expr_type, raise_error=True):
SetType.type_compare = type_compare_SetType

def type_compare_IterableType(expected_type, ctx, expr, expr_type, raise_error=True):

if isinstance(expr_type, OptionType):
return check_option_type(type_compare_IterableType, expected_type, ctx, expr, expr_type, raise_error)

Expand Down Expand Up @@ -2300,12 +2301,13 @@ def type_compare_TypeVariable(expected_type, ctx, expr, expr_type, raise_error=T
ctx.call_type_env[-1][expected_type.var_name] = expr_type
return True
else: # not a call variable
if expected_type == expr_type:
return True
else:
if raise_error:
ctx.add_type_error(TypeComparisonError(ctx.function_def, expected_type, expr, expr_type, tr("Type mismatch for parameter #{} in call, expecting {} found: {}").format(expected_type.var_name[1:], expected_type, expr_type)))
return False
# XXX: is it always safe to accept the comparison ?
# or an error should be returned
return True

# if raise_error:
# ctx.add_type_error(TypeComparisonError(ctx.function_def, expected_type, expr, expr_type, tr("Type mismatch for parameter #{} in call, expecting {} found: {}").format(expected_type.var_name[1:], expected_type, expr_type)))
# return False

TypeVariable.type_compare = type_compare_TypeVariable

Expand Down
20 changes: 20 additions & 0 deletions test/progs/53_iterable_OK.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

def groupes(f : Callable[[T], K], lst : List[T]) -> Dict[K, List[T]]:
"""
"""
dico : Dict[K,List[T]] = dict()

e : T
for e in lst:
k : K = f(e)
if k in dico:
dico[k].append(e)
else:
dico[k] = [e]

return dico

# Jeu de tests
# TODO : str devrait être compatible avec Iterable[str]
assert groupes(len, ["a", "as", "asd", "aa", "asdf", "qwer", "aa"]) \
== {1 : ["a"], 2 : ["as", "aa", "aa"], 3 : ["asd"], 4 : ["asdf", "qwer"]}

0 comments on commit 382067b

Please sign in to comment.