Skip to content

[1.20 regression] _Environ.get(key, default) returns str | None when default is an alias to Any #21344

@pablogsal

Description

@pablogsal

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions