From e5abd08f931c086ec5e14f37884a1e122a1795ff Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 16 May 2021 21:38:00 +0200 Subject: [PATCH] Replace PathLike unions with aliases from _typeshed (#5467) Standardize on 'from os import PathLike' --- stdlib/imp.pyi | 4 ++-- stdlib/importlib/metadata.pyi | 15 ++++++++------- stdlib/importlib/util.pyi | 6 +++--- stdlib/mimetypes.pyi | 6 +++--- stdlib/os/__init__.pyi | 9 +++++---- stdlib/sqlite3/dbapi2.pyi | 4 ++-- stdlib/tokenize.pyi | 4 ++-- stdlib/urllib/request.pyi | 4 ++-- stdlib/zoneinfo/__init__.pyi | 6 +++--- 9 files changed, 30 insertions(+), 28 deletions(-) diff --git a/stdlib/imp.pyi b/stdlib/imp.pyi index adcf6e097..c6049c38e 100644 --- a/stdlib/imp.pyi +++ b/stdlib/imp.pyi @@ -1,6 +1,6 @@ -import os import types from _typeshed import StrPath +from os import PathLike from typing import IO, Any, List, Optional, Protocol, Tuple, TypeVar, Union from _imp import ( @@ -57,7 +57,7 @@ def load_module(name: str, file: Optional[_FileLike], filename: str, details: Tu # IO[Any] is a TextIOWrapper if name is a .py file, and a FileIO otherwise. def find_module( - name: str, path: Union[None, List[str], List[os.PathLike[str]], List[StrPath]] = ... + name: str, path: Union[None, List[str], List[PathLike[str]], List[StrPath]] = ... ) -> Tuple[IO[Any], str, Tuple[str, str, int]]: ... def reload(module: types.ModuleType) -> types.ModuleType: ... def init_builtin(name: str) -> Optional[types.ModuleType]: ... diff --git a/stdlib/importlib/metadata.pyi b/stdlib/importlib/metadata.pyi index ae39c89b5..47c2825b7 100644 --- a/stdlib/importlib/metadata.pyi +++ b/stdlib/importlib/metadata.pyi @@ -1,11 +1,12 @@ import abc -import os import pathlib import sys +from _typeshed import StrPath from email.message import Message from importlib.abc import MetaPathFinder +from os import PathLike from pathlib import Path -from typing import Any, Dict, Iterable, List, NamedTuple, Optional, Tuple, Union, overload +from typing import Any, Dict, Iterable, List, NamedTuple, Optional, Tuple, overload if sys.version_info >= (3, 8): class PackageNotFoundError(ModuleNotFoundError): ... @@ -20,7 +21,7 @@ if sys.version_info >= (3, 8): class PackagePath(pathlib.PurePosixPath): def read_text(self, encoding: str = ...) -> str: ... def read_binary(self) -> bytes: ... - def locate(self) -> os.PathLike[str]: ... + def locate(self) -> PathLike[str]: ... # The following attributes are not defined on PackagePath, but are dynamically added by Distribution.files: hash: Optional[FileHash] size: Optional[int] @@ -33,7 +34,7 @@ if sys.version_info >= (3, 8): @abc.abstractmethod def read_text(self, filename: str) -> Optional[str]: ... @abc.abstractmethod - def locate_file(self, path: Union[os.PathLike[str], str]) -> os.PathLike[str]: ... + def locate_file(self, path: StrPath) -> PathLike[str]: ... @classmethod def from_name(cls, name: str) -> Distribution: ... @overload @@ -45,7 +46,7 @@ if sys.version_info >= (3, 8): cls, *, context: None = ..., name: Optional[str] = ..., path: List[str] = ..., **kwargs: Any ) -> Iterable[Distribution]: ... @staticmethod - def at(path: Union[str, os.PathLike[str]]) -> PathDistribution: ... + def at(path: StrPath) -> PathDistribution: ... @property def metadata(self) -> Message: ... @property @@ -69,8 +70,8 @@ if sys.version_info >= (3, 8): def find_distributions(cls, context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ... class PathDistribution(Distribution): def __init__(self, path: Path) -> None: ... - def read_text(self, filename: Union[str, os.PathLike[str]]) -> str: ... - def locate_file(self, path: Union[str, os.PathLike[str]]) -> os.PathLike[str]: ... + def read_text(self, filename: StrPath) -> str: ... + def locate_file(self, path: StrPath) -> PathLike[str]: ... def distribution(distribution_name: str) -> Distribution: ... @overload def distributions(*, context: DistributionFinder.Context) -> Iterable[Distribution]: ... diff --git a/stdlib/importlib/util.pyi b/stdlib/importlib/util.pyi index e5d4ec399..d1c92c579 100644 --- a/stdlib/importlib/util.pyi +++ b/stdlib/importlib/util.pyi @@ -1,8 +1,8 @@ import importlib.abc import importlib.machinery -import os import types -from typing import Any, Callable, List, Optional, Union +from _typeshed import AnyPath +from typing import Any, Callable, List, Optional def module_for_loader(fxn: Callable[..., types.ModuleType]) -> Callable[..., types.ModuleType]: ... def set_loader(fxn: Callable[..., types.ModuleType]) -> Callable[..., types.ModuleType]: ... @@ -20,7 +20,7 @@ def spec_from_loader( ) -> Optional[importlib.machinery.ModuleSpec]: ... def spec_from_file_location( name: str, - location: Optional[Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]] = ..., + location: Optional[AnyPath] = ..., *, loader: Optional[importlib.abc.Loader] = ..., submodule_search_locations: Optional[List[str]] = ..., diff --git a/stdlib/mimetypes.pyi b/stdlib/mimetypes.pyi index 1f874022b..d1b440647 100644 --- a/stdlib/mimetypes.pyi +++ b/stdlib/mimetypes.pyi @@ -1,9 +1,9 @@ import sys -from typing import IO, Dict, List, Optional, Sequence, Tuple, Union +from _typeshed import StrPath +from typing import IO, Dict, List, Optional, Sequence, Tuple if sys.version_info >= (3, 8): - from os import PathLike - def guess_type(url: Union[str, PathLike[str]], strict: bool = ...) -> Tuple[Optional[str], Optional[str]]: ... + def guess_type(url: StrPath, strict: bool = ...) -> Tuple[Optional[str], Optional[str]]: ... else: def guess_type(url: str, strict: bool = ...) -> Tuple[Optional[str], Optional[str]]: ... diff --git a/stdlib/os/__init__.pyi b/stdlib/os/__init__.pyi index 4500a748d..c79d478c6 100644 --- a/stdlib/os/__init__.pyi +++ b/stdlib/os/__init__.pyi @@ -7,6 +7,7 @@ from _typeshed import ( OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode, + StrPath, ) from builtins import OSError from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper as _TextIOWrapper @@ -367,8 +368,8 @@ if sys.platform != "win32": f_namemax: int # ----- os function stubs ----- -def fsencode(filename: Union[str, bytes, PathLike[Any]]) -> bytes: ... -def fsdecode(filename: Union[str, bytes, PathLike[Any]]) -> str: ... +def fsencode(filename: AnyPath) -> bytes: ... +def fsdecode(filename: AnyPath) -> str: ... @overload def fspath(path: str) -> str: ... @overload @@ -687,7 +688,7 @@ if sys.platform != "win32": if sys.version_info >= (3, 7): @overload def fwalk( - top: Union[str, PathLike[str]] = ..., + top: StrPath = ..., topdown: bool = ..., onerror: Optional[_OnError] = ..., *, @@ -705,7 +706,7 @@ if sys.platform != "win32": ) -> Iterator[Tuple[bytes, List[bytes], List[bytes], int]]: ... else: def fwalk( - top: Union[str, PathLike[str]] = ..., + top: StrPath = ..., topdown: bool = ..., onerror: Optional[_OnError] = ..., *, diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index 3681f99f4..159e55fc9 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -1,5 +1,5 @@ -import os import sys +from _typeshed import AnyPath from datetime import date, datetime, time from typing import Any, Callable, Generator, Iterable, Iterator, List, Optional, Protocol, Tuple, Type, TypeVar, Union @@ -66,7 +66,7 @@ def complete_statement(sql: str) -> bool: ... if sys.version_info >= (3, 7): def connect( - database: Union[bytes, str, os.PathLike[str]], + database: AnyPath, timeout: float = ..., detect_types: int = ..., isolation_level: Optional[str] = ..., diff --git a/stdlib/tokenize.pyi b/stdlib/tokenize.pyi index 23babea28..1df951bc5 100644 --- a/stdlib/tokenize.pyi +++ b/stdlib/tokenize.pyi @@ -1,6 +1,6 @@ import sys +from _typeshed import AnyPath from builtins import open as _builtin_open -from os import PathLike from token import * # noqa: F403 from typing import ( Any, @@ -62,7 +62,7 @@ def untokenize(iterable: Iterable[_Token]) -> Any: ... def detect_encoding(readline: Callable[[], bytes]) -> Tuple[str, Sequence[bytes]]: ... def tokenize(readline: Callable[[], bytes]) -> Generator[TokenInfo, None, None]: ... def generate_tokens(readline: Callable[[], str]) -> Generator[TokenInfo, None, None]: ... # undocumented -def open(filename: Union[str, bytes, int, PathLike[Any]]) -> TextIO: ... +def open(filename: Union[AnyPath, int]) -> TextIO: ... def group(*choices: str) -> str: ... # undocumented def any(*choices: str) -> str: ... # undocumented def maybe(*choices: str) -> str: ... # undocumented diff --git a/stdlib/urllib/request.pyi b/stdlib/urllib/request.pyi index 3f09496a6..96fd3c1dd 100644 --- a/stdlib/urllib/request.pyi +++ b/stdlib/urllib/request.pyi @@ -1,6 +1,6 @@ -import os import ssl import sys +from _typeshed import AnyPath from email.message import Message from http.client import HTTPMessage, HTTPResponse, _HTTPConnectionProtocol from http.cookiejar import CookieJar @@ -271,7 +271,7 @@ class HTTPErrorProcessor(BaseHandler): def urlretrieve( url: str, - filename: Optional[Union[str, os.PathLike[Any]]] = ..., + filename: Optional[AnyPath] = ..., reporthook: Optional[Callable[[int, int, int], None]] = ..., data: Optional[bytes] = ..., ) -> Tuple[str, HTTPMessage]: ... diff --git a/stdlib/zoneinfo/__init__.pyi b/stdlib/zoneinfo/__init__.pyi index 46cd6b871..35bd142b8 100644 --- a/stdlib/zoneinfo/__init__.pyi +++ b/stdlib/zoneinfo/__init__.pyi @@ -1,7 +1,7 @@ -import os import typing +from _typeshed import StrPath from datetime import tzinfo -from typing import Any, AnyStr, Iterable, Optional, Protocol, Sequence, Set, Type, Union +from typing import Any, Iterable, Optional, Protocol, Sequence, Set, Type _T = typing.TypeVar("_T", bound="ZoneInfo") @@ -23,7 +23,7 @@ class ZoneInfo(tzinfo): # Note: Both here and in clear_cache, the types allow the use of `str` where # a sequence of strings is required. This should be remedied if a solution # to this typing bug is found: https://github.com/python/typing/issues/256 -def reset_tzpath(to: Optional[Sequence[Union[os.PathLike[AnyStr], str]]] = ...) -> None: ... +def reset_tzpath(to: Optional[Sequence[StrPath]] = ...) -> None: ... def available_timezones() -> Set[str]: ... TZPATH: Sequence[str]