Skip to content

Crash when using Optional[TypedDictType] #7149

Closed

Description

@JukkaL

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

Activity

  1. ilevkivskyi commented on Jul 4, 2019

    @ilevkivskyi
    Member

    I would say this is a bug in how self types are bound, for example currently:

    [case testSelfTypeOnUnion]
    from typing import TypeVar, Union
    
    T = TypeVar('T')
    
    class A:
        same: int
    
    class C:
        def same(self: T) -> T: ...
    
    x: Union[A, C]
    reveal_type(x.same)
    

    results in Union[builtins.int, def () -> Union[__main__.A, __main__.C]]. In a sense we are "overusing" the original_type, I will try to see if there is a simple fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions