-
Notifications
You must be signed in to change notification settings - Fork 235
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
Should operator |
on TypedDict
allow for creating intersection-like dicts?
#1445
Comments
Individual type checkers can decide to add this feature if they choose. I don't think it needs to be spelled out more broadly. |
Both mypy and pyright use the dummy type class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
...
if sys.version_info >= (3, 9):
def __or__(self, __value: typing_extensions.Self) -> typing_extensions.Self: ...
def __ior__(self, __value: typing_extensions.Self) -> typing_extensions.Self: ... That's why you see the behavior you do with both pyright and mypy. It would be possible to override this behavior with a custom signature for these two methods. It would need to produce an intersection type that can't be "spelled" in today's type system, but there is precedent for this (specifically in the I would be open to adding this functionality to pyright if there's sufficient interest from pyright users. So far, no one has requested it. AFAIK, no one has requested it in the mypy issue tracker either. |
FWIW to get around this at least with pyright I just use |
Ah, that's a good workaround. I don't think it's any faster, since that's effectively what the |
Indeed, the work-around seems to work starting with mypy 1.5.0, i.e., since two days ago (last time I tried it didn't work yet). This only leaves PEP 584 in a slightly awkward state, because it suggests using operator |
I'm wondering how is the relation between the following PEPs:
|
for dicts, but doesn't mention how it should behave forTypedDict
.TypedDict
, but doesn't mention the operator|
.According to PEP 589, multiple inheritance can be used to create a combined (or "intersection like") dict, and operator
|
provides the corresponding behavior at runtime. Thus it would be nice if the type system could handle the combination properly (similar to how it is possible in TypeScript):From a runtime and type-checking perspective this code looks valid, but currently mypy does not accept it (playground):
Pyright seems to have the same behavior.
Apparently operator
|
can only be used for two instances of the same typed dict, which as far as I can see has limited use cases, because using the operator|
on two dicts that already have the same fields is kind of pointless (perhaps it mostly makes sense if the type usedtotal=False
).Possible related discussions and issues I've found:
TypedDict
probably falls into the "intersection type" category as well?)TypedDict
fail if type is compatible? microsoft/pylance-release#2300|
and|=
operators forTypedDict
microsoft/pyright#2951The text was updated successfully, but these errors were encountered: