Repository navigation
Open
Labels
Description
Bug Report
new fancy native parser doesnt like large literal ints
To Reproduce
#!/usr/bin/env bash
# mypy 2.4.0: Literal[<BIGINT>]
cat > repro.py <<'PY'
import typing as t
I64_MAX: t.Literal[9_223_372_036_854_775_807] # ok
I64_MAX_PLUS_1: t.Literal[9_223_372_036_854_775_808] # err
I64_MIN: t.Literal[-9_223_372_036_854_775_808] # err
U64_MAX: t.Literal[18_446_744_073_709_551_615] # err
HEX: t.Literal[0xCBF29CE484222325] # err
PY
set -x
for pyver in 3.10 3.11 3.12 3.13 3.14; do
echo "__py_$pyver__"
uvx --python $pyver mypy==2.4.0 --no-incremental repro.py
uvx --python $pyver mypy==2.4.0 --no-incremental --no-native-parser repro.py
uvx --python $pyver mypy==2.3.1 --no-incremental repro.py
doneGIVES ME:
+ uvx mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx mypy==2.3.1 --no-incremental repro.py
Success: no issues found in 1 source file
+ for pyver in 3.10 3.11 3.12 3.13 3.14
+ echo __py_
__py_
+ uvx --python 3.10 mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx --python 3.10 mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx --python 3.10 mypy==2.3.1 --no-incremental repro.py
Installed 7 packages in 19ms
Success: no issues found in 1 source file
+ for pyver in 3.10 3.11 3.12 3.13 3.14
+ echo __py_
__py_
+ uvx --python 3.11 mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx --python 3.11 mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx --python 3.11 mypy==2.3.1 --no-incremental repro.py
Installed 6 packages in 21ms
Success: no issues found in 1 source file
+ for pyver in 3.10 3.11 3.12 3.13 3.14
+ echo __py_
__py_
+ uvx --python 3.12 mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx --python 3.12 mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx --python 3.12 mypy==2.3.1 --no-incremental repro.py
Installed 6 packages in 49ms
Success: no issues found in 1 source file
+ for pyver in 3.10 3.11 3.12 3.13 3.14
+ echo __py_
__py_
+ uvx --python 3.13 mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx --python 3.13 mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx --python 3.13 mypy==2.3.1 --no-incremental repro.py
Installed 6 packages in 48ms
Success: no issues found in 1 source file
+ for pyver in 3.10 3.11 3.12 3.13 3.14
+ echo __py_
__py_
+ uvx --python 3.14 mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
Found 4 errors in 1 file (checked 1 source file)
+ uvx --python 3.14 mypy==2.4.0 --no-incremental --no-native-parser repro.py
Success: no issues found in 1 source file
+ uvx --python 3.14 mypy==2.3.1 --no-incremental repro.py
Success: no issues found in 1 source fileExpected Behavior
no error reported
Actual Behavior
+ uvx mypy==2.4.0 --no-incremental repro.py
repro.py:4: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:5: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:6: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]
repro.py:7: error: Invalid type: Literal[...] cannot contain arbitrary expressions [valid-type]Your Environment
- Mypy version used:
2.4.0 - Mypy command-line flags: see above script
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10 through 3.14
Activity
ilevkivskyi commentedon Oct 9, 2026
LOL @JukkaL I told you this will happen :-) Btw @jessekrubin just curious what is the context where this is useful?
jessekrubin commentedon Oct 9, 2026
Howdy @ilevkivskyi,
Type stubs for magics/constants (and bears (oh my)).
EG: https://github.com/jessekrubin/ry/blob/92704cea43adb670e0f58c15e2b8975cff8dd4a7/python/ry/ryo3/_fnv.pyi#L12
I have a pybind11 project that prolly will also give the same error.
what was the convo you and @JukkaL had? and what was this exact thing brought up? (just curious)
ilevkivskyi commentedon Oct 9, 2026
We were talking about whether one would ever need such literals (after I noticed literal int types are stored using
i64in the new parser).jessekrubin commentedon Oct 9, 2026
interesting. I would have assumed they would be stored as strings or bigints.
bumping it to i128/u128 would fix the example above but depending on what you pick it would mess with:
MustafaKemal0146 commentedon Oct 26, 2026
I looked into this and opened PRs:
The binary format already supports arbitrary-size ints:
librt.internal.read_intdecodes the variable-length encoding (tag 15 + length/sign + little-endian magnitude) to any Python int, and ast_serialize'swrite_intalready uses it for large i64 values. The only limit was in ast_serialize:extract_int_literal_value()goes through Ruff'sInt::as_i64()and serializes an invalid type when that fails.The fix keeps the value as sign plus
ruff_python_ast::Intand writes values that don't fit in i64 with the long encoding, so values of any size work (including the u128/i128 constants mentioned above). It needs no mypy code change and nocache_versionbump, so existing mypy releases get the fix by upgrading ast-serialize. It also accepts nested+/-(e.g.Literal[-+5]) like the old parser does.