Skip to content

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Sep 18, 2025

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:

foldable_builtins = {
    "builtins.str": str,
    "builtins.int": int,
    "builtins.bool": bool,
    "builtins.float": float,
    "builtins.complex": complex,
    "builtins.repr": repr,
    "builtins.len": len,
    "builtins.hasattr": hasattr,
    "builtins.hex": hex,
    "builtins.min": min,
    "builtins.max": max,
    "builtins.oct": oct,
    "builtins.pow": pow,
    "builtins.round": round,
    "builtins.abs": abs,
    "builtins.ascii": ascii,
    "builtins.bin": bin,
    "builtins.chr": chr,
}

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.

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.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

graphql-core (https://github.com/graphql-python/graphql-core)
- version: 1.19.0+dev.19697af9051707b4db55bc2d5301436d872f452c
+ version: 1.19.0+dev.92d603df08b25321357ed32d54e880a38a5aa89d

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.

1 participant