Skip to content

[mypyc] Use get() to look up keys in mapping patterns - #22563

Open
Jahongir-Qurbonov wants to merge 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-match-mapping-get
Open

Jahongir-Qurbonov wants to merge 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-match-mapping-get

Conversation

@Jahongir-Qurbonov

@Jahongir-Qurbonov Jahongir-Qurbonov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Mapping patterns (case {"a": v}) were compiled using PyMapping_HasKey() followed by __getitem__, while CPython looks up each key using x.get(key, <sentinel>). This caused several differences:

  • With a defaultdict, the pattern always matched and the key was added to the dict, since __getitem__ calls __missing__.
  • An overridden get method was ignored, and an overridden __getitem__ was called instead.
  • PyMapping_HasKey() swallows exceptions, so an exception raised by __getitem__ was ignored (and printed as "Exception ignored ..." on 3.13+).

Now we call get with a sentinel object, as CPython does. The sentinel is the existing _CPy_ExcDummy object. It isn't otherwise visible to Python code, but a user-defined get method will see it.

If the subject has a dict type, this uses the existing dict.get primitive, so an overridden get method of a dict subclass isn't called. This is consistent with how mypyc handles dict methods in general.

CPython looks up keys in mapping patterns using get() with a sentinel
default. We used PyMapping_HasKey() and __getitem__, which called
__missing__ in defaultdict (adding the key), ignored overridden get()
methods and swallowed exceptions.
The expected output includes the 64-bit value of PY_VECTORCALL_ARGUMENTS_OFFSET.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe mention that if we dict subclass overrides get, we don't call the overriden method, in the PR summary. This is consistent with how things work in general, but seems worth mentioning here.

Comment thread mypyc/irbuild/match.py Outdated

exists = self.builder.call_c(mapping_has_key, [self.subject, key_value], pattern.line)
# Like CPython, look up the key using x.get(key, <sentinel>). The sentinel is an
# object that is private to mypyc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: If calling get of a user-defined class, the sentinel object reference becomes visible (but this seems fine).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I updated the PR description to mention both of these.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also updated the code comment.

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