[1.20 regression] _Environ.get(key, default) returns str | None when default is an alias to Any
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Since mypy 1.20, passing a value whose type is an alias to Any (e.g. Incomplete: TypeAlias = Any) as the default argument of os.environ.get causes mypy to infer str | None instead of Any, producing a spurious [arg-type] error at the use site.
Reproducer: https://mypy-play.net/?gist=7a79f64ac9733542b571cc358f6d3165
import os
from pathlib import Path
from typing import Any
from typing_extensions import TypeAlias
Incomplete: TypeAlias = Any
A: Incomplete = Path()
reveal_type(A) # Any (good)
B = os.environ.get('TEST', A)
reveal_type(B) # str | None (bad) (expected: Any)
C = Path(B) # error: Argument 1 to "Path" has incompatible
# type "str | None"; expected "str | PathLike[str]"
Bisected to 30260ca57 which added explicit _Environ.get/pop overloads where the first overload is def get(self, key, default: None = None) -> AnyStr | None. Before that sync, _Environ inherited Mapping.get, whose first overload took no default argument, so 2-arg calls couldn't match it.
Versions: broken on mypy ≥ 1.20.0; works on mypy ≤ 1.19.1.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the explicit _Environ.get and pop overloads introduced by commit 30260ca57, and reproduce the issue with the linked mypy-play example. Compare the behavior on mypy 1.20 and 1.19.1, then verify that an alias to Any passed as the default is inferred as Any and no spurious [arg-type] error occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100