FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[mypyc] feat: support constant folding in `translate_index_expr` [1/1] by BobTheBuidler · Pull Request #19972 · python/mypy · GitHub

Repository navigation

[mypyc] feat: support constant folding in translate_index_expr [1/1] - #19972

Merged
ilevkivskyi merged 22 commits into
python:masterfrom
BobTheBuidler:constant-fold-translate-index
Oct 10, 2025
Merged

ilevkivskyi merged 22 commits into
python:masterfrom
BobTheBuidler:constant-fold-translate-index

Conversation

BobTheBuidler commented Oct 2, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

This PR attempts to constant fold the index value in translate_index_expr

I'm not sure any test changes are warranted for a small PR of this nature.

Comment thread mypyc/irbuild/expression.py Outdated
if (
isinstance(base.type, RTuple)
and isinstance(folded_index := constant_fold_expr(builder, index), int)
and folded_index >= 0

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Check for index overflow (it may generate an error elsewhere, but we don't want to crash the compiler if it's ignored).

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Done.

I also just thought about reverse indexing, I'll implement that too

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We will need to adapt TupleGet before we can pass negative values here.

I've implemented that in a separate PR #19990 , lets pause this one until we get that merged in so we don't have to come back to this section later

BobTheBuidler and others added 10 commits October 2, 2025 13:31
maybe I should just compute the correct positive index value when we encounter a negative index, rather than doing this? 

Where's the best place to do that? Should that be with this or in a separate PR?
BobTheBuidler added a commit to BobTheBuidler/mypy that referenced this pull request Oct 2, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, but will allow us to pass negative values in python#19972 so I think we should consider this PR a prerequisite to that one
BobTheBuidler added a commit to BobTheBuidler/mypy that referenced this pull request Oct 2, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, since none of the existing calling locations can pass a negative value, but will allow us to pass negative values in python#19972 so I think we should consider this PR a prerequisite to that one

This comment was marked as off-topic.

p-sawicki added a commit that referenced this pull request Oct 7, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative
indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, since none of the existing
calling locations can pass a negative value, but will allow us to pass
negative values in #19972 so I think
we should consider this PR a prerequisite to that one

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Piotr Sawicki <sawickipiotr@outlook.com>

Copy link
Copy Markdown
Contributor Author

Now that #19990 is merged, this PR is ready for review.

ilevkivskyi merged commit 826e0ad into python:master Oct 10, 2025
13 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

3 participants


Back | FazBrowse Home | New Git URL