Skip to content

Conversation

@BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Oct 15, 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:

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:

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

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`
def get_length():
r0 :: str
L0:
r0 = 'https://w3id.org/cwl/salad#'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to figure out how to avoid loading the constant at all, but I couldn't figure it out when I was implementing this PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can add an optimization pass at some point that removes unused pure values.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good.

def get_length():
r0 :: str
L0:
r0 = 'https://w3id.org/cwl/salad#'
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can add an optimization pass at some point that removes unused pure values.

@JukkaL JukkaL merged commit 34373e3 into python:master Dec 11, 2025
16 checks passed
@BobTheBuidler BobTheBuidler deleted the patch-18 branch December 11, 2025 19:01
@github-project-automation github-project-automation bot moved this from Todo to Done in GC-Content-Calculator Dec 12, 2025
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