From f55dff80f05ec2a7649907c6bbfc17ecfb13faea Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Sat, 15 Jan 2022 17:52:31 -0600 Subject: [PATCH] Override `os._Environ.__ior__` (#6921) 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. --- stdlib/os/__init__.pyi | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/os/__init__.pyi b/stdlib/os/__init__.pyi index eadba856b..090928f67 100644 --- a/stdlib/os/__init__.pyi +++ b/stdlib/os/__init__.pyi @@ -245,6 +245,13 @@ class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): def __setitem__(self, key: AnyStr, value: AnyStr) -> None: ... def __iter__(self) -> Iterator[AnyStr]: ... def __len__(self) -> int: ... + if sys.version_info >= (3, 9): + # We use @overload instead of a Union for reasons similar to those given for + # overloading MutableMapping.update in stdlib/typing.pyi + @overload + def __ior__(self: Self, value: Mapping[AnyStr, AnyStr]) -> Self: ... + @overload + def __ior__(self: Self, value: Iterable[tuple[AnyStr, AnyStr]]) -> Self: ... environ: _Environ[str] if sys.platform != "win32":