Skip to content

Commit 382067b

Browse files
author
fredokun
committed
less restricted library type variables
1 parent c5cb24a commit 382067b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

mrpython/typechecking/typechecker.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ def type_compare_SetType(expected_type, ctx, expr, expr_type, raise_error=True):
22212221
SetType.type_compare = type_compare_SetType
22222222

22232223
def type_compare_IterableType(expected_type, ctx, expr, expr_type, raise_error=True):
2224+
22242225
if isinstance(expr_type, OptionType):
22252226
return check_option_type(type_compare_IterableType, expected_type, ctx, expr, expr_type, raise_error)
22262227

@@ -2300,12 +2301,13 @@ def type_compare_TypeVariable(expected_type, ctx, expr, expr_type, raise_error=T
23002301
ctx.call_type_env[-1][expected_type.var_name] = expr_type
23012302
return True
23022303
else: # not a call variable
2303-
if expected_type == expr_type:
2304-
return True
2305-
else:
2306-
if raise_error:
2307-
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)))
2308-
return False
2304+
# XXX: is it always safe to accept the comparison ?
2305+
# or an error should be returned
2306+
return True
2307+
2308+
# if raise_error:
2309+
# 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)))
2310+
# return False
23092311

23102312
TypeVariable.type_compare = type_compare_TypeVariable
23112313

test/progs/53_iterable_OK.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
def groupes(f : Callable[[T], K], lst : List[T]) -> Dict[K, List[T]]:
3+
"""
4+
"""
5+
dico : Dict[K,List[T]] = dict()
6+
7+
e : T
8+
for e in lst:
9+
k : K = f(e)
10+
if k in dico:
11+
dico[k].append(e)
12+
else:
13+
dico[k] = [e]
14+
15+
return dico
16+
17+
# Jeu de tests
18+
# TODO : str devrait être compatible avec Iterable[str]
19+
assert groupes(len, ["a", "as", "asd", "aa", "asdf", "qwer", "aa"]) \
20+
== {1 : ["a"], 2 : ["as", "aa", "aa"], 3 : ["asd"], 4 : ["asdf", "qwer"]}

0 commit comments

Comments
 (0)