[mypyc] Support iterating over a TypedDict#14747
Conversation
An optimization to make iterating over dict.keys(), dict.values() and dict.items() faster caused mypyc to crash while compiling a TypedDict. This commit fixes `Builder.get_dict_base_type` to properly handle TypedDictType.
| def update(self: _T, __m: _T) -> None: ... | ||
| def items(self) -> dict_items[str, object]: ... | ||
| def keys(self) -> dict_keys[str, object]: ... | ||
| def values(self) -> dict_values[str, object]: ... |
There was a problem hiding this comment.
The return types aren't imported anywhere. For some reason we don't generate errors even though these are undefined. Maybe we are ignoring errors in test stubs?
You should be able to use a less specific type. These are adapted from mypyc test stubs:
def keys(self) -> Iterable[str]: pass
def values(self) -> Iterable[object]: pass
def items(self) -> Iterable[Tuple[str, object]]: passThere was a problem hiding this comment.
Ah, good catch! And yeah I have no idea what's up with the lack of errors. I opened an issue to note that we should investigate this later: mypyc/mypyc#976.
| def pop(self, k: NoReturn, default: _T = ...) -> object: ... | ||
| def update(self: _T, __m: _T) -> None: ... | ||
| def items(self) -> Iterable[str, object]: ... | ||
| def keys(self) -> Iterable[str, object]: ... |
There was a problem hiding this comment.
The return types are still invalid. It seems quite important to report errors in test stubs, as otherwise tests might not be testing what we expect them to.
JukkaL
left a comment
There was a problem hiding this comment.
This looks good now! Could you also add a run test? The generated IR is pretty complex so it's a bit tricky to validate that it works right.
JukkaL
left a comment
There was a problem hiding this comment.
Thanks for the updates! Looks good.
An optimization to make iterating over dict.keys(), dict.values() and dict.items() faster caused mypyc to crash while compiling a TypedDict. This commit fixes
Builder.get_dict_base_typeto properly handleTypedDictType.Fixes mypyc/mypyc#869.