From b03e79886e22d4b0a258e93ab1445743107d391d Mon Sep 17 00:00:00 2001 From: David Euresti Date: Tue, 4 Apr 2017 00:21:35 -0400 Subject: [PATCH] Fix os.getenv and friends to have consistent types (#1131) --- stdlib/2/os/__init__.pyi | 34 ++++++++++++++++++++++------------ stdlib/3/os/__init__.pyi | 20 +++++++++++++------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/stdlib/2/os/__init__.pyi b/stdlib/2/os/__init__.pyi index c11d9728f..881bcf73f 100644 --- a/stdlib/2/os/__init__.pyi +++ b/stdlib/2/os/__init__.pyi @@ -1,13 +1,16 @@ # created from https://docs.python.org/2/library/os.html +from builtins import OSError as error +import sys from typing import ( Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, - Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple + Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple, TypeVar ) -from . import path +from . import path as path from mypy_extensions import NoReturn -error = OSError +_T = TypeVar('_T') + SEEK_SET = 0 SEEK_CUR = 0 SEEK_END = 0 @@ -58,13 +61,15 @@ R_OK = 0 W_OK = 0 X_OK = 0 -class _Environ(MutableMapping[str, str]): - def copy(self) -> Dict[str, str]: ... +class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): + def copy(self) -> Dict[AnyStr, AnyStr]: ... + +environ = ... # type: _Environ[str] + +confstr_names = ... # type: Dict[str, int] # Unix only +pathconf_names = ... # type: Dict[str, int] # Unix only +sysconf_names = ... # type: Dict[str, int] # Unix only -environ = ... # type: _Environ -confstr_names = ... # type: Mapping[str, int] # Unix only -pathconf_names = ... # type: Mapping[str, int] # Unix only -sysconf_names = ... # type: Mapping[str, int] # Unix only EX_OK = 0 # Unix only EX_USAGE = 0 # Unix only EX_DATAERR = 0 # Unix only @@ -112,8 +117,6 @@ def getppid() -> int: ... def getresuid() -> Tuple[int, int, int]: ... # Unix only def getresgid() -> Tuple[int, int, int]: ... # Unix only def getuid() -> int: ... # Unix only -def getenv(varname: unicode, value: unicode = ...) -> str: ... -def putenv(varname: unicode, value: unicode) -> None: ... def setegid(egid: int) -> None: ... # Unix only def seteuid(euid: int) -> None: ... # Unix only def setgid(gid: int) -> None: ... # Unix only @@ -130,7 +133,14 @@ def setuid(uid: int) -> None: ... # Unix only def strerror(code: int) -> str: ... def umask(mask: int) -> int: ... def uname() -> Tuple[str, str, str, str, str]: ... # Unix only -def unsetenv(varname: str) -> None: ... + +@overload +def getenv(key: Text) -> Optional[str]: ... +@overload +def getenv(key: Text, default: _T) -> Union[str, _T]: ... +def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... +def unsetenv(key: Union[bytes, Text]) -> None: ... + def fdopen(fd: int, *args, **kwargs) -> IO[Any]: ... def close(fd: int) -> None: ... def closerange(fd_low: int, fd_high: int) -> None: ... diff --git a/stdlib/3/os/__init__.pyi b/stdlib/3/os/__init__.pyi index 2324a9343..c35cdcb55 100644 --- a/stdlib/3/os/__init__.pyi +++ b/stdlib/3/os/__init__.pyi @@ -8,11 +8,13 @@ from io import TextIOWrapper as _TextIOWrapper import sys from typing import ( Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, - Optional, Generic, Set, Callable, Text, Sequence + Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, TypeVar ) -from . import path +from . import path as path from mypy_extensions import NoReturn +_T = TypeVar('_T') + # ----- os variables ----- supports_bytes_environ = False # TODO: True when bytes implemented? @@ -236,10 +238,6 @@ def getppid() -> int: ... def getresuid() -> Tuple[int, int, int]: ... # Unix only def getresgid() -> Tuple[int, int, int]: ... # Unix only def getuid() -> int: ... # Unix only -def getenv(key: str, default: str = ...) -> str: ... -def getenvb(key: bytes, default: bytes = ...) -> bytes: ... -# TODO mixed str/bytes putenv arguments -def putenv(key: AnyStr, value: AnyStr) -> None: ... def setegid(egid: int) -> None: ... # Unix only def seteuid(euid: int) -> None: ... # Unix only def setgid(gid: int) -> None: ... # Unix only @@ -256,7 +254,15 @@ def setuid(uid: int) -> None: ... # Unix only def strerror(code: int) -> str: ... def umask(mask: int) -> int: ... def uname() -> Tuple[str, str, str, str, str]: ... # Unix only -def unsetenv(key: _PathType) -> None: ... + +@overload +def getenv(key: Text) -> Optional[str]: ... +@overload +def getenv(key: Text, default: _T) -> Union[str, _T]: ... +def getenvb(key: bytes, default: bytes = ...) -> bytes: ... +def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... +def unsetenv(key: Union[bytes, Text]) -> None: ... + # Return IO or TextIO def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: str = ..., errors: str = ..., newline: str = ..., closefd: bool = ...) -> Any: ...