feat: constant fold common builtin call expressions [3/3] - #19886
Draft
BobTheBuidler wants to merge 35 commits into
Draft
feat: constant fold common builtin call expressions [3/3]#19886BobTheBuidler wants to merge 35 commits into
BobTheBuidler wants to merge 35 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
BobTheBuidler
force-pushed
the
builtins-folding
branch
from
September 19, 2025 05:29
b91d208 to
60642c5
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
BobTheBuidler
commented
Sep 25, 2025
| y = x # E: Cannot determine type of "x" # E: Name "x" is used before definition | ||
| y() | ||
| y = x # E: Name "x" is used before definition | ||
| y() # E: "int" not callable |
Contributor
Author
There was a problem hiding this comment.
I think this might indicate a bug in mypy itself? I don't think this should change just from the constant folding
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
BobTheBuidler
marked this pull request as draft
October 1, 2025 23:23
Contributor
Author
|
One example of where this would be useful: mypyc/mypyc#1152 |
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
JukkaL
pushed a commit
that referenced
this pull request
Dec 11, 2025
Currently, `translate_len` can determine the length of an RTuple at compile time This PR extends this capability to all expressions supported by `get_expr_length_value` This will constant fold the code example displayed in mypyc/mypyc#1152 without waiting for the implementation of #19886 which has a few steps ahead of it Before: ```python def extend_and_specialize(items: list[dict[str, Any]]) -> None: types: Final[dict[str, Any]] = {i["name"]: i for i in items} types.update( { k[len("https://w3id.org/cwl/salad#") :]: v for k, v in types.items() if k.startswith("https://w3id.org/cwl/salad#") } ) ``` After: ```python def extend_and_specialize(items: list[dict[str, Any]]) -> None: types: Final[dict[str, Any]] = {i["name"]: i for i in items} types.update( { k[27 : ]: v for k, v in types.items() if k.startswith("https://w3id.org/cwl/salad#") } ) ``` Even after #19986, this will also work for some situations that constant folding cannot handle but `get_expr_length` can.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements constant folding for builtin callables where possible.
If all args are constant foldable and the callable is one of the builtin callables below, mypy can, in most cases, fold the result.
The supported builtins are:
While this PR does not build off of #19885, it does need to come afterward because it contains a refactoring that moves around the code introduced in 19885.