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
The following patterns still break mypy:
1. `type[]` at top level fails
2. `tuple[T1, T2]` at top level fails (but `tuple[T1, ...]` is fine)
3. `T1 | Callable[..., T2 | T3]` fails, but only <=3.9
This PR cleans up usage of `Union` and `Optional` outside these patterns.
As pointed out by @gvanrossum in https://github.com/python/typing/issues/1096
Improves type inference in cases when we know that mode is
OpenBinaryMode, but don't know anything more specific:
```
def my_open(name: str, write: bool):
mode: Literal['rb', 'wb'] = 'wb' if write else 'rb'
with open(name, mode) as f:
reveal_type(f) # previously typing.IO[Any], now typing.BinaryIO
```
You may be tempted into thinking this is some limitation of type
checkers. mypy does in fact have logic for detecting if we match
multiple overloads and union-ing up the return types of matched
overloads. The problem is the last overload interferes with this logic.
That is, if you remove the fallback overload (prior to this PR), you'd get
"Union[io.BufferedReader, io.BufferedWriter]" in the above example.
Co-authored-by: hauntsaninja <>
Supporting the `|=` operator for `os.environ` was introduced in Python 3.9. However,
this led to false negatives in type checking statements like `os.environ |= {"k": "v"}`
and `os.environ |= [("k", "v")]` because the definition inherited from
`typing.MutableMapping` was insufficient. This addresses that deficiency.
Fixes#6919.