Skip to content
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 augmented assignment operator on TypedDict fail if type is compatible? #2300

Closed
michaeloliverx opened this issue Jan 28, 2022 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version type checking

Comments

@michaeloliverx
Copy link

michaeloliverx commented Jan 28, 2022

from typing import TypedDict


class Person(TypedDict, total=False):
    name: str
    age: int


person: Person = {}

# Ok
person.update({"name": "Michael"})

# Expression of type "dict[str, str | object]" cannot be assigned to declared type "Person"
# "dict[str, str | object]" is incompatible with "Person"
person |= {"name": "Michael"}

To quote PEP 584:

Augmented assignment behaves identically to the update method called with a single positional argument, so it also accepts anything implementing the Mapping protocol (more specifically, anything with the keys and __getitem__ methods) or iterables of key-value pairs.

@erictraut
Copy link
Contributor

PEP 589, which introduced TypedDict, doesn't specify that the | operator is supported by TypedDict. It discusses the principles about which operations are allowed in this section. These principles do not appear to rule out support for | or |= as long as both operands are the same TypedDict type.

The typeshed type stubs also define a private class called _TypedDict that defines (in rough terms) the operations that should be permitted on all TypedDict classes. I say "rough terms" because supported methods are generally synthesized by type checkers to provide more specific parameter and return types corresponding to individual fields within the TypedDict. Interestingly, the _TypedDict class was amended just a few weeks ago by typeshed maintainers to include __or__ and __ior__ magic methods, implying that | and |= should now be allowed.

Pyright (the type checker that underlies pylance) will need to be modified to support this. I've created this issue to track that work.

@erictraut
Copy link
Contributor

This will be addressed in the next release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jan 28, 2022
@michaeloliverx
Copy link
Author

This will be addressed in the next release.

The turnaround on issues here is incredible. Fantastic work 👏 typed Python future is looking bright.

@debonte
Copy link
Contributor

debonte commented Feb 4, 2022

This issue has been fixed in version 2022.2.0, which we've just released. You can find the changelog here: CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version type checking
Projects
None yet
Development

No branches or pull requests

3 participants