Repository navigation
Open
Labels
project: policyOrganization of the typeshed projectOrganization of the typeshed project
Description
As discussed in python/typing#1748: I propose to (slowly) replace float with float | int and complex with complex | float | int, where applicable.
- It's more correct, even if
floatis current implicitly equivalent toint | float. - We are already prepared should the implicit promotion ever be removed.
- We give type checkers the ability to optionally disable the promotion so that type checker authors and users can experiment with it.
- Typeshed would always need to be the first step for this, so let's remove this blocker.
This issue proposes an ugly and slow, but correct and safe procedure:
- Disable flake8-pyi code Y041.
Ignore flake8-pyi Y041 (redundant float | int) #16096 - Introduce two type aliases
FloatInt = float | intandComplexInt = complex | float | intto_typeshed.
Add type aliasesFloatIntandComplexInt#16068 - Replace
complexwith eithercomplexor a union; removeComplexIntagain.
Review and replacecomplex#16743 - Globally replace all instances of
floatwithFloatIntin the stdlib.
[stdlib] ReplacefloatwithFloatInt#16788 - Globally replace all instances of
floatwithFloatIntin third-party stubs.
[stubs] Replace float with FloatInt #16789 - Review
FloatIntinstances are replace them with the proper types. (This will take a while.) - Remove the type alias once they are not needed anymore (in 10 years or so ...)Using
FloatIntandComplexIntwould be disallowed for new code and the type aliases should explicitly be marked as not to be used by non-typeshed code.
We should also probably add flake8-pyi checks that checks that argument types use float | int and complex | float | int. Cases where only float/complex is allowed are probably rare.
Reacted by Joren Hammudoglu, Alex Waygood, Jakub Kuczys and Gideon
Reacted by Carl Meyer and James Hilton-Balfe
Metadata
Metadata
Assignees
Labels
project: policyOrganization of the typeshed projectOrganization of the typeshed project
Activity
JelleZijlstra commentedon Jul 21, 2026
Should we also add a
JustFloat = floatalias to_typeshedto explicitly mark places where we really do only want float? I suspect that's not uncommon in return types.srittau commentedon Jul 21, 2026
With the plan above (which was incomplete before), we shouldn't need that. A plain
floatmeans just that. Please note that step 2-4 basically replace allfloats in typeshed, until they are fixed to be eitherfloatorint | float.JelleZijlstra commentedon Jul 21, 2026
But until we have replaced them all, we don't know whether
floatmeans "exactly float" orfloat | int. Having an explicit replacement means we can safely look for remaining float usages and know they should all be replaced.srittau commentedon Jul 21, 2026
After the steps "Globally replace all instances of float with FloatInt and of complex with ComplexInt" there shouldn't any bare
floatinstances be left. I think doing it this way around (starting with lots ofFloatInts and then slowly reducing them) is better than the alternative (starting without any type aliases and then increasing them until allfloatsare replaced and then removing them again).srittau commentedon Jul 22, 2026
#16068 introduces the type aliases. Globally replacing the types requires a bit more work, but the earlier we get the type aliases into typeshed, the earlier we can use them for third-party packages.
Ignore flake8-pyi Y041 (redundant float | int) (#16096)
srittau commentedon Nov 9, 2026
Well, #16743 reviews all
complexinstances, soComplexIntisn't really needed. I've updated the checklist accordingly.srittau commentedon Nov 13, 2026
For reference, I've attached the codemod that was used for #16788 and #16789. (By Claude, reviewed by me.)
float_to_floatint.py