AnyStr cleanup (#5487)

* Replace all uses of StrPath, BytesPath, and AnyPath in Python 2 stubs.
* Add StrOrBytesPath as preferred alias for AnyPath.
* Replace all remaining AnyPath instances with StrOrBytesPath.
* Mark AnyPath as obsolete.

Part of #5470
This commit is contained in:
Sebastian Rittau
2021-05-17 20:45:48 +02:00
committed by GitHub
parent 51b7248154
commit f0bf6eebbd
67 changed files with 546 additions and 529 deletions

View File

@@ -1,5 +1,5 @@
import sys
from _typeshed import AnyPath, FileDescriptorLike
from _typeshed import FileDescriptorLike
from builtins import OSError
from posix import listdir as listdir, stat_result as stat_result # TODO: use this, see https://github.com/python/mypy/issues/3078
from typing import (
@@ -198,40 +198,40 @@ def dup2(fd: int, fd2: int) -> None: ...
def fstat(fd: int) -> Any: ...
def fsync(fd: FileDescriptorLike) -> None: ...
def lseek(fd: int, pos: int, how: int) -> int: ...
def open(file: AnyPath, flags: int, mode: int = ...) -> int: ...
def open(file: Text, flags: int, mode: int = ...) -> int: ...
def pipe() -> Tuple[int, int]: ...
def read(fd: int, n: int) -> bytes: ...
def write(fd: int, string: Union[bytes, buffer]) -> int: ...
def access(path: AnyPath, mode: int) -> bool: ...
def chdir(path: AnyPath) -> None: ...
def access(path: Text, mode: int) -> bool: ...
def chdir(path: Text) -> None: ...
def fchdir(fd: FileDescriptorLike) -> None: ...
def getcwd() -> str: ...
def getcwdu() -> unicode: ...
def chmod(path: AnyPath, mode: int) -> None: ...
def link(src: AnyPath, link_name: AnyPath) -> None: ...
def lstat(path: AnyPath) -> Any: ...
def mknod(filename: AnyPath, mode: int = ..., device: int = ...) -> None: ...
def chmod(path: Text, mode: int) -> None: ...
def link(src: Text, link_name: Text) -> None: ...
def lstat(path: Text) -> Any: ...
def mknod(filename: Text, mode: int = ..., device: int = ...) -> None: ...
def major(device: int) -> int: ...
def minor(device: int) -> int: ...
def makedev(major: int, minor: int) -> int: ...
def mkdir(path: AnyPath, mode: int = ...) -> None: ...
def makedirs(path: AnyPath, mode: int = ...) -> None: ...
def mkdir(path: Text, mode: int = ...) -> None: ...
def makedirs(path: Text, mode: int = ...) -> None: ...
def readlink(path: AnyStr) -> AnyStr: ...
def remove(path: AnyPath) -> None: ...
def removedirs(path: AnyPath) -> None: ...
def rename(src: AnyPath, dst: AnyPath) -> None: ...
def renames(old: AnyPath, new: AnyPath) -> None: ...
def rmdir(path: AnyPath) -> None: ...
def stat(path: AnyPath) -> Any: ...
def remove(path: Text) -> None: ...
def removedirs(path: Text) -> None: ...
def rename(src: Text, dst: Text) -> None: ...
def renames(old: Text, new: Text) -> None: ...
def rmdir(path: Text) -> None: ...
def stat(path: Text) -> Any: ...
@overload
def stat_float_times() -> bool: ...
@overload
def stat_float_times(newvalue: bool) -> None: ...
def symlink(source: AnyPath, link_name: AnyPath) -> None: ...
def unlink(path: AnyPath) -> None: ...
def symlink(source: Text, link_name: Text) -> None: ...
def unlink(path: Text) -> None: ...
# TODO: add ns, dir_fd, follow_symlinks argument
def utime(path: AnyPath, times: Optional[Tuple[float, float]]) -> None: ...
def utime(path: Text, times: Optional[Tuple[float, float]]) -> None: ...
if sys.platform != "win32":
# Unix only
@@ -247,15 +247,15 @@ if sys.platform != "win32":
def tcgetpgrp(fd: int) -> int: ...
def tcsetpgrp(fd: int, pg: int) -> None: ...
def ttyname(fd: int) -> str: ...
def chflags(path: AnyPath, flags: int) -> None: ...
def chroot(path: AnyPath) -> None: ...
def chown(path: AnyPath, uid: int, gid: int) -> None: ...
def lchflags(path: AnyPath, flags: int) -> None: ...
def lchmod(path: AnyPath, mode: int) -> None: ...
def lchown(path: AnyPath, uid: int, gid: int) -> None: ...
def mkfifo(path: AnyPath, mode: int = ...) -> None: ...
def pathconf(path: AnyPath, name: Union[str, int]) -> int: ...
def statvfs(path: AnyPath) -> _StatVFS: ...
def chflags(path: Text, flags: int) -> None: ...
def chroot(path: Text) -> None: ...
def chown(path: Text, uid: int, gid: int) -> None: ...
def lchflags(path: Text, flags: int) -> None: ...
def lchmod(path: Text, mode: int) -> None: ...
def lchown(path: Text, uid: int, gid: int) -> None: ...
def mkfifo(path: Text, mode: int = ...) -> None: ...
def pathconf(path: Text, name: Union[str, int]) -> int: ...
def statvfs(path: Text) -> _StatVFS: ...
def walk(
top: AnyStr, topdown: bool = ..., onerror: Optional[Callable[[OSError], Any]] = ..., followlinks: bool = ...
@@ -263,21 +263,21 @@ def walk(
def abort() -> NoReturn: ...
# These are defined as execl(file, *args) but the first *arg is mandatory.
def execl(file: AnyPath, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execlp(file: AnyPath, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execl(file: Text, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
def execlp(file: Text, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ...
# These are: execle(file, *args, env) but env is pulled from the last element of the args.
def execle(file: AnyPath, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execlpe(file: AnyPath, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execle(file: Text, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
def execlpe(file: Text, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ...
# The docs say `args: tuple or list of strings`
# The implementation enforces tuple or list so we can't use Sequence.
_ExecVArgs = Union[Tuple[Union[bytes, Text], ...], List[bytes], List[Text], List[Union[bytes, Text]]]
def execv(path: AnyPath, args: _ExecVArgs) -> NoReturn: ...
def execve(path: AnyPath, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ...
def execvp(file: AnyPath, args: _ExecVArgs) -> NoReturn: ...
def execvpe(file: AnyPath, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ...
def execv(path: Text, args: _ExecVArgs) -> NoReturn: ...
def execve(path: Text, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ...
def execvp(file: Text, args: _ExecVArgs) -> NoReturn: ...
def execvpe(file: Text, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ...
def _exit(n: int) -> NoReturn: ...
def kill(pid: int, sig: int) -> None: ...
@@ -293,24 +293,24 @@ def popen(command: str, *args, **kwargs) -> IO[Any]: ...
def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ...
def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ...
def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ...
def spawnl(mode: int, path: AnyPath, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnle(mode: int, path: AnyPath, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise sig
def spawnv(mode: int, path: AnyPath, args: List[Union[bytes, Text]]) -> int: ...
def spawnve(mode: int, path: AnyPath, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ...
def system(command: AnyPath) -> int: ...
def spawnl(mode: int, path: Text, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnle(mode: int, path: Text, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise sig
def spawnv(mode: int, path: Text, args: List[Union[bytes, Text]]) -> int: ...
def spawnve(mode: int, path: Text, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ...
def system(command: Text) -> int: ...
def times() -> Tuple[float, float, float, float, float]: ...
def waitpid(pid: int, options: int) -> Tuple[int, int]: ...
def urandom(n: int) -> bytes: ...
if sys.platform == "win32":
def startfile(path: AnyPath, operation: Optional[str] = ...) -> None: ...
def startfile(path: Text, operation: Optional[str] = ...) -> None: ...
else:
# Unix only
def spawnlp(mode: int, file: AnyPath, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnlpe(mode: int, file: AnyPath, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature
def spawnvp(mode: int, file: AnyPath, args: List[Union[bytes, Text]]) -> int: ...
def spawnvpe(mode: int, file: AnyPath, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ...
def spawnlp(mode: int, file: Text, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ...
def spawnlpe(mode: int, file: Text, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature
def spawnvp(mode: int, file: Text, args: List[Union[bytes, Text]]) -> int: ...
def spawnvpe(mode: int, file: Text, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ...
def wait() -> Tuple[int, int]: ...
def wait3(options: int) -> Tuple[int, int, Any]: ...
def wait4(pid: int, options: int) -> Tuple[int, int, Any]: ...

View File

@@ -1,6 +1,5 @@
import os
import sys
from _typeshed import AnyPath, BytesPath, StrPath
from typing import Any, AnyStr, Callable, List, Optional, Sequence, Text, Tuple, TypeVar, overload
_T = TypeVar("_T")
@@ -38,21 +37,21 @@ else:
# NOTE: Empty lists results in '' (str) regardless of contained type.
# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes
# So, fall back to Any
def commonprefix(m: Sequence[AnyPath]) -> Any: ...
def exists(path: AnyPath) -> bool: ...
def lexists(path: AnyPath) -> bool: ...
def commonprefix(m: Sequence[Text]) -> Any: ...
def exists(path: Text) -> bool: ...
def lexists(path: Text) -> bool: ...
# These return float if os.stat_float_times() == True,
# but int is a subclass of float.
def getatime(filename: AnyPath) -> float: ...
def getmtime(filename: AnyPath) -> float: ...
def getctime(filename: AnyPath) -> float: ...
def getsize(filename: AnyPath) -> int: ...
def isabs(s: AnyPath) -> bool: ...
def isfile(path: AnyPath) -> bool: ...
def isdir(s: AnyPath) -> bool: ...
def islink(path: AnyPath) -> bool: ...
def ismount(path: AnyPath) -> bool: ...
def getatime(filename: Text) -> float: ...
def getmtime(filename: Text) -> float: ...
def getctime(filename: Text) -> float: ...
def getsize(filename: Text) -> int: ...
def isabs(s: Text) -> bool: ...
def isfile(path: Text) -> bool: ...
def isdir(s: Text) -> bool: ...
def islink(path: Text) -> bool: ...
def ismount(path: Text) -> bool: ...
# Make sure signatures are disjunct, and allow combinations of bytes and unicode.
# (Since Python 2 allows that, too)
@@ -61,18 +60,18 @@ def ismount(path: AnyPath) -> bool: ...
@overload
def join(__p1: bytes, *p: bytes) -> bytes: ...
@overload
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: AnyPath) -> Text: ...
def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: Text) -> Text: ...
@overload
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: AnyPath) -> Text: ...
def join(__p1: bytes, __p2: bytes, __p3: Text, *p: Text) -> Text: ...
@overload
def join(__p1: bytes, __p2: Text, *p: AnyPath) -> Text: ...
def join(__p1: bytes, __p2: Text, *p: Text) -> Text: ...
@overload
def join(__p1: Text, *p: AnyPath) -> Text: ...
def join(__p1: Text, *p: Text) -> Text: ...
@overload
def relpath(path: BytesPath, start: Optional[BytesPath] = ...) -> bytes: ...
def relpath(path: str, start: Optional[str] = ...) -> str: ...
@overload
def relpath(path: StrPath, start: Optional[StrPath] = ...) -> Text: ...
def samefile(f1: AnyPath, f2: AnyPath) -> bool: ...
def relpath(path: Text, start: Optional[Text] = ...) -> Text: ...
def samefile(f1: Text, f2: Text) -> bool: ...
def sameopenfile(fp1: int, fp2: int) -> bool: ...
def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: ...
def split(p: AnyStr) -> Tuple[AnyStr, AnyStr]: ...