Skip to content

Commit

Permalink
Require explicit annotations for List[object] and Dict[*, object]
Browse files Browse the repository at this point in the history
  • Loading branch information
jxcl committed May 7, 2019
1 parent a202b42 commit 1923819
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1902,8 +1902,20 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
rvalue,
in_final_declaration=inferred.is_final,
)
self.check_forbidden_inference_types(rvalue_type, rvalue)
self.infer_variable_type(inferred, lvalue, rvalue_type, rvalue)

def check_forbidden_inference_types(self, rvalue_type: Type, rvalue: Context) -> None:
if isinstance(rvalue_type, Instance):
if rvalue_type.type.fullname() == 'builtins.list':
list_arg_type = rvalue_type.args[0]
if isinstance(list_arg_type, Instance) and list_arg_type.type.fullname() == 'builtins.object':
self.msg.forbidden_inference_of_object(rvalue_type, rvalue)
elif rvalue_type.type.fullname() == 'builtins.dict':
dict_value_type = rvalue_type.args[1]
if isinstance(dict_value_type, Instance) and dict_value_type.type.fullname() == 'builtins.object':
self.msg.forbidden_inference_of_object(rvalue_type, rvalue)

def check_compatibility_all_supers(self, lvalue: RefExpr, lvalue_type: Optional[Type],
rvalue: Expression) -> bool:
lvalue_node = lvalue.node
Expand Down
6 changes: 6 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,12 @@ def operator_method_signatures_overlap(
forward_method, self.format(forward_class)),
context)

def forbidden_inference_of_object(self, expr: Type, context: Context) -> None:
self.fail('Variable inferred to {}, which is forbidden.'.format(
self.format(expr)),
context
)

def forward_operator_not_callable(
self, forward_method: str, context: Context) -> None:
self.fail('Forward operator "{}" is not callable'.format(
Expand Down

0 comments on commit 1923819

Please sign in to comment.