Continuing work towards #8988.
The first five commits were created using stubdefaulter on various Python versions; the following commits were all created manually by me to fix various problems. The main things this adds that weren't present in #9501 are:
- Defaults in Windows-only modules and Windows-only branches (because I'm running a Windows machine)
- Defaults in non-py311 branches
- Defaults for float parameters
- Defaults for overloads
This pull request reverts part of #2539 that brought back a bug discussed in https://github.com/python/mypy/issues/5788 and initially fixed in #2539
In short, the issue was that the following program always resulted
in an error when running mypy with the `--disallow-any-expr` flag:
from enum import Enum
class MyEnum(Enum):
FOO = 1
BAR = 2
blah = MyEnum # Error here
The root issue was that because the signature of Enum's
`__new__` method was typed as:
def __new__(self: Type[T], value: Any) -> T: ...
This caused mypy to decide that the type of `MyEnum` was
`(Any) -> MyEnum`. This is correct based on the current
type signature, but unfortunately means that it becomes
impossible to ever use enums with the `--disallow-any-expr` flag.
- Improve TypeVar names
- `object` -> `Any` in `Enum.__new__` (there _are_ restrictions on the kinds of objects that can be passed in, they're just not expressable in the stubs.
- Delete pointless `Self | int` unions, since `Self` is a subtype of `int` for these methods.
Most Python objects evaluate as falsey if they have length 0, but an enum class is truthy even if it has length 0.
Source code: 841c77d802/Lib/enum.py (L353)
I remember there are some subtleties around Enum.__new__, that might be
relevant to StrEnum, but I'm forgetting the details.
I wasn't sure how best to handle the new enum.property. I could also
re-export, or take a more literal interpretation.
Co-authored-by: hauntsaninja <>
Eliminated the use of "bare" TypeVars (i.e. a TypeVar that appears only once) within generic methods. While not considered an error in PEP 484, these are a common source of bugs in code, and some type checkers (including pytype and pyright) flag them as errors.
Co-authored-by: Eric Traut <erictr@microsoft.com>