8000 Crash when using Optional[TypedDictType] · Issue #7149 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Crash when using Optional[TypedDictType] #7149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JukkaL opened this issue Jul 4, 2019 · 1 comment · Fixed by #7152
Closed

Crash when using Optional[TypedDictType] #7149

JukkaL opened this issue Jul 4, 2019 · 1 comment · Fixed by #7152

Comments

@JukkaL
Copy link
Collaborator
JukkaL commented Jul 4, 2019

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

@JukkaL JukkaL added crash topic-typed-dict priority-0-high topic-plugins The plugin API and ideas for new plugins labels Jul 4, 2019
@ilevkivskyi
Copy link
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.

ilevkivskyi added a commit that referenced this issue Jul 5, 2019
Fixes #7149

The fix is quite straightforward, self-types should be bound to each individual component, so I just added only basic tests. Please let me know if you want better test coverage (properties, class-methods) etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0