Repository navigation
Fix TypedDict subtyping - #11670
Fix TypedDict subtyping#1167097littleleaf11 wants to merge 1 commit into
Conversation
JukkaL
left a comment
There was a problem hiding this comment.
Unfortunately, I don't think that this fixes the root cause, but is hiding the symptoms of another bug.
| right_fallback = mypy.typeops.try_getting_instance_fallback(right) | ||
| if (right_fallback is not None | ||
| and ((isinstance(right, TupleType) and right_fallback.type.is_enum) | ||
| or isinstance(right, TypedDictType))): |
There was a problem hiding this comment.
IIRC the fallback of a TypedDict type is actually not a "real" type, but a sort of implementation artifact. I believe that the actual bug is that we leak the fallback as an instance type. In particular, it shouldn't be possible to bind a TypedDict type to Type[T], since a TypedDict type is not a real class, and doesn't support isinstance, among other things. I suspect that is where things go wrong, and this change would be just papering over the root cause. I'm not 100% sure what is going on here, though.
There was a problem hiding this comment.
the actual bug is that we leak the fallback as an instance type
Yes you're right. I am confused by Type[T] before and I just saw #9773 and other related discussions. If my understanding is correct, these issues should be fiexd by two steps: 1. throw errows when binding a TypedDict to Type[T]. 2. implement TypeForm[t] to support the original design pattern described in those issues.
There was a problem hiding this comment.
oops, I still need some extra investigation to catch up disscussions on Type[T] and TypeForm. It'd be great if you could provide some suggestions on implementation since it's a quite common issue.
| def fun(c: C): | ||
| pass | ||
|
|
||
| fun(foo(C)) |
There was a problem hiding this comment.
The call foo(C) should actually generate an error (see my other comment). For example, if the call would make sense, we should handle cases like these, which probably won't work correctly right now:
d = foo(C)
d['x'] = 1 # Should be ok
d['y'] = 1 # Should be an error
d['x'] = 'x' # Should be an error
Description
Fixes #5593
Fixes #10024
Fixes #9827
Partially solves #10593 and #11644
This PR moves special cases (introduced by #2407 and #11236) in
SubtypeVisitor.visit_instanceto the end of the func and also adds support forTypedDictType.Test Plan
A new test case [testTypedDictTempTestName] under the
subtypingsection ofcheck-typeddict.test. However I don't have an idea about its name.