Skip to content

Narrow real math and cmath constants to float - #16886

Merged
srittau merged 4 commits into
python:mainfrom
xixi-box:fix/issue-16059-f15d56f-c970297b
Nov 21, 2026
Merged

srittau merged 4 commits into
python:mainfrom
xixi-box:fix/issue-16059-f15d56f-c970297b

Conversation

@xixi-box

Copy link
Copy Markdown
Contributor

Fixes #16059

The bulk FloatInt migration made type checkers infer float | int for e, pi, inf, nan, and tau in both modules, although CPython creates each as a float. CPython math initialization and CPython cmath initialization.

Declare these constants as Final[float] so callers get the precise type and can use float-specific methods directly.

Tests: .venv/bin/python -m pyright -p pyrightconfig.testcases.json --pythonversion 3.10 stdlib/@tests/test_cases/check_math.py stdlib/@tests/test_cases/check_cmath.py.
Result: Before adding the regression, .venv/bin/python -m pyright -p pyrightconfig.testcases.json --pythonversion 3.10 stdlib/@tests/test_cases/check_math.py passed with 0 errors.
Result: Before adding the regression, .venv/bin/python tests/regr_test.py stdlib --python-version 3.10 3.14 passed on Linux.
Result: Base regression command exited 1 with ten assert_type mismatches, five in check_math.py and five in check_cmath.py (OSSCOUT_BASE.log).
Result: Patched regression command exited 0 with 0 errors (OSSCOUT_PATCHED.log).
Result: Patched .venv/bin/python tests/regr_test.py stdlib --python-version 3.10 3.14 passed on Linux (OSSCOUT_PATCHED_MYPY.log).
Result: Patched .venv/bin/python tests/mypy_test.py stdlib/math stdlib/cmath.pyi --python-version 3.10 3.14 passed (OSSCOUT_PATCHED_STUB_MYPY.log).
Result: Patched .venv/bin/python -m pyright -p pyrightconfig.stricter.json --pythonversion 3.10 stdlib/math stdlib/cmath.pyi passed with 0 errors (OSSCOUT_PATCHED_STUB_PYRIGHT.log).
Result: Patched .venv/bin/python -m mypy.stubtest --custom-typeshed-dir . --strict-type-check-only math cmath passed with no issues in 2 modules (OSSCOUT_PATCHED_STUBTEST.log).
Result: With .venv activated, python tests/ty_test.py stdlib/cmath.pyi --python-version 3.10 --platform linux --python .venv/bin/python and python tests/pyrefly_test.py stdlib/cmath.pyi --python-version 3.10 --platform linux --python .venv/bin/python passed. Initial attempts without the virtual environment on PATH could not locate the checker executables. The retries are in OSSCOUT_PATCHED_CMATH_TY_RETRY.log and OSSCOUT_PATCHED_CMATH_PYREFLY_RETRY.log.
Result: Patched .venv/bin/pre-commit run --files stdlib/math/init.pyi stdlib/cmath.pyi stdlib/@tests/test_cases/check_math.py stdlib/@tests/test_cases/check_cmath.py passed all applicable hooks (OSSCOUT_PATCHED_PRECOMMIT.log).
Regression coverage: stdlib/@tests/test_cases/check_cmath.py.

Prepared by Codex.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>

@srittau srittau left a comment

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.

Please remove the tests. In typeshed, we only add regression tests for functions and classes which are known to have caused complex problems in the past, or where stubs are difficult to get right. 100% test coverage for typeshed is neither necessary nor desirable, as it would lead to code duplication.

See tests/REGRESSION.md for more information.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit 29aa356 into python:main Nov 21, 2026
88 checks passed
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.

Replace float with float | int

2 participants