KEMBAR78
[mypyc] Support __setitem__, __delitem__, __len__ and __contains__ by JukkaL · Pull Request #10451 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 9, 2021

Previously these couldn't be used in compiled classes.

__setitem__ and __delitem__ are implemented by a single wrapper function. If the value is
NULL, it should act as __delitem__. This makes the implementation non-trivial. First, we
need to support both a single variant and two variants being defined in a class. Second, when
overriding, we need to use super(), if we only override one of them.

The other dunders are straightforward.

The implementation is pretty verbose. I'll look at refactoring it in a separate PR.

This has some overlap with the previous PR #10211 by @sohailsomani.

@TH3CHARLie TH3CHARLie self-requested a review May 10, 2021 09:00
Copy link
Collaborator

@TH3CHARLie TH3CHARLie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! One minor point that doesn't block merging.

generated = {} # type: Dict[str, str]
# Sort for determinism on Python 3.5
for name, (slot, generator) in sorted(table.items()):
for name, (slot, generator) in sorted(table.items(), reverse=True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why turn on reverse?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This because the implementation depends on processing __setitem__ before __delitem__. This kind of hacky -- I'll add a comment in a follow-up PR, or see if this could be refactored.

@JukkaL JukkaL merged commit 5a105d1 into master May 14, 2021
@JukkaL JukkaL deleted the dunders branch May 14, 2021 17:46
@JukkaL
Copy link
Collaborator Author

JukkaL commented Jun 20, 2021

Related issue: mypyc/mypyc#839

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants