Repository navigation
Crash when using Optional[TypedDictType] #7149
Copy link
Copy link
Closed
Labels
crashpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new pluginstopic-typed-dicttopic-union-types
Description
Mypy crashes when type checking this code:
from typing import Union
from mypy_extensions import TypedDict
class A(TypedDict):
x: int
d: Union[A, None]
d.update({'x': 1})Here's the traceback:
...
File "/Users/jukka/src/mypy/mypy/checker.py", line 281, in check_first_pass
self.accept(d)
File "/Users/jukka/src/mypy/mypy/checker.py", line 392, in accept
stmt.accept(self)
File "/Users/jukka/src/mypy/mypy/nodes.py", line 1002, in accept
return visitor.visit_expression_stmt(self)
File "/Users/jukka/src/mypy/mypy/checker.py", line 2788, in visit_expression_stmt
self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3351, in accept
typ = self.visit_call_expr(node, allow_none_return=True)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 234, in visit_call_expr
return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 307, in visit_call_expr_inner
object_type, member)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 660, in check_call_expr_with_callee_type
return self.check_union_call_expr(e, object_type, member)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 682, in check_union_call_expr
item_object_type))
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 657, in check_call_expr_with_callee_type
callable_name, callee_type, e.args, e.arg_kinds, e, e.arg_names, object_type)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 630, in transform_callee_type
callee, args, arg_kinds, context, arg_names, object_type, signature_hook)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 594, in apply_method_signature_hook
MethodSigContext(object_type, formal_arg_exprs, callee, context, self.chk))
File "/Users/jukka/src/mypy/mypy/plugins/default.py", line 325, in typed_dict_update_signature_callback
assert isinstance(arg_type, TypedDictType)
AssertionError:
The plugin hook gets the union type as the argument, even though it should probably only get the TypedDict type. The bug was introduced in #6560.
cc @ilevkivskyi
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
crashpriority-0-hightopic-pluginsThe plugin API and ideas for new pluginsThe plugin API and ideas for new pluginstopic-typed-dicttopic-union-types
Activity
ilevkivskyi commentedon Jul 4, 2019
I would say this is a bug in how self types are bound, for example currently:
results in
Union[builtins.int, def () -> Union[__main__.A, __main__.C]]. In a sense we are "overusing" theoriginal_type, I will try to see if there is a simple fix.Fix binding of self-types on unions (#7152)