fix(checker): properly check parameter defaults
This commit is contained in:
+16
-3
@@ -335,10 +335,23 @@ class PythonTyper(
|
|||||||
kw: list[Function.Parameter] = []
|
kw: list[Function.Parameter] = []
|
||||||
|
|
||||||
def eval_param_type(param: p.Function.Parameter) -> Type:
|
def eval_param_type(param: p.Function.Parameter) -> Type:
|
||||||
if param.type is not None:
|
default_type: Optional[Type] = None
|
||||||
return self.resolve_type_expr(param.type)
|
|
||||||
if param.default is not None:
|
if param.default is not None:
|
||||||
return self.type_of(param.default)
|
default_type = self.type_of(param.default)
|
||||||
|
|
||||||
|
if param.type is not None:
|
||||||
|
param_type: Type = self.resolve_type_expr(param.type)
|
||||||
|
if default_type is not None:
|
||||||
|
if not self.types.is_subtype(default_type, param_type):
|
||||||
|
self.reporter.error(
|
||||||
|
param.location or stmt.location,
|
||||||
|
f"Cannot use default value of type {default_type} for parameter of type {param_type}",
|
||||||
|
)
|
||||||
|
return param_type
|
||||||
|
|
||||||
|
if default_type is not None:
|
||||||
|
return default_type
|
||||||
|
|
||||||
return UnknownType()
|
return UnknownType()
|
||||||
|
|
||||||
position: int = 0
|
position: int = 0
|
||||||
|
|||||||
@@ -101,6 +101,10 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
|
|||||||
function (p.Function): the function to resolve
|
function (p.Function): the function to resolve
|
||||||
"""
|
"""
|
||||||
self.begin_scope()
|
self.begin_scope()
|
||||||
|
for param in function.params.all:
|
||||||
|
if param.default is not None:
|
||||||
|
self.resolve(param.default)
|
||||||
|
|
||||||
for param in function.params.all:
|
for param in function.params.all:
|
||||||
self.declare(param.name)
|
self.declare(param.name)
|
||||||
self.define(param.name)
|
self.define(param.name)
|
||||||
|
|||||||
Reference in New Issue
Block a user