-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
TypedDict -> dict compatibility #13122
Comments
Your second sample is just as unsafe as the first. If the assignment in the second example were allowed, then There is already a way in the Python type system to indicate that a f: Mapping[str, str] = foo() |
Indeed, but it is only unsafe if other references to However, that example was just for illustration; this feature request is not about that. Consider the Flask case. The |
This… does not appear to be the case. from typing import TypedDict, Mapping
class DefinitelyContainsStatus(TypedDict):
status: str
def foo() -> DefinitelyContainsStatus:
return {'status': 'ok'}
f: Mapping[str, str] = foo() Was this a bug reintroduced later? |
(Its beef appears to be with the |
I just encountered this when trying to remap keys in a from typing import Mapping, TypedDict, TypeVar
K = TypeVar('K')
V = TypeVar('V')
def mapkeys(dct: Mapping[K, V], keymap: dict[K, K]) -> dict[K, V]:
return {keymap.get(k, k): v for k, v in dct.items()}
class Foo(TypedDict):
foo: int
f: Foo = {'foo': 1}
b: dict[str, int] = mapkeys(f, {"foo": "bar"})
b2: dict[str, int] = mapkeys(dict(f), {"foo": "bar"})
b3: dict[str, int] = mapkeys({k: v for (k, v) in f.items()},
{"foo": "bar"}) There are errors on all three invocations to |
Feature
Allow for TypedDicts (or a new type specialized for this purpose) to be compatible with dict when the receiver declares it is not going to mutate the dict.
Pitch
When using a
TypedDict
, it is common to eventually pass it to a function that takes an arbitrarydict
or return it to the caller expecting the same. Currently this is rejected, and the reason makes sense. For example, this is correctly rejected:If it were permitted, a type checker wouldn't be able to know how the dict is going to be mutated, and then any other references that may exist would still expect it to conform to the TypedDict.
However, this presumably valid case is also rejected:
Here's a real-world example that has lead to the creation of this feature request. In Flask it is allowed to return a dict from a view function, which it then converts to JSON. This is particularly convenient in order to apply type checking to a JSON-based HTTP API. However, when the return type is declared to be some TypedDict, this is rejected:
On the Flask side of things, they specifically require a real
dict
, so they cannot change the type to acceptMapping
. It would be quite useful to be able to allow returning TypedDict from a function to a caller that accepts dict.The text was updated successfully, but these errors were encountered: