Skip to content

Commit eea9be6

Browse files
authored
Add a _typeshed.pyi file and a PathLike alias (#4161)
1 parent ec3370b commit eea9be6

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

stdlib/2and3/_typeshed/__init__.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Utility types for typeshed
2+
3+
# This module contains various common types to be used by typeshed. The
4+
# module and its types do not exist at runtime. You can use this module
5+
# outside of typeshed, but no API stability guarantees are made. To use
6+
# it in implementation (.py) files, the following construct must be used:
7+
#
8+
# from typing import TYPE_CHECKING
9+
# if TYPE_CHECKING:
10+
# from _typeshed import ...
11+
#
12+
# If on Python versions < 3.10 and "from __future__ import annotations"
13+
# is not used, types from this module must be quoted.
14+
15+
import sys
16+
from typing import Text, Union
17+
18+
# StrPath and AnyPath can be used in places where a
19+
# path can be used instead of a string, starting with Python 3.6.
20+
if sys.version_info >= (3, 6):
21+
from os import PathLike
22+
StrPath = Union[str, PathLike[str]]
23+
BytesPath = Union[bytes, PathLike[bytes]]
24+
AnyPath = Union[str, bytes, PathLike[str], PathLike[bytes]]
25+
else:
26+
StrPath = Text
27+
BytesPath = bytes
28+
AnyPath = Union[Text, bytes]

tests/stubtest_whitelists/py3_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ _importlib_modulespec
55
_operator.methodcaller
66
_threading_local.local.__new__
77
_types
8+
_typeshed
89
_weakref.CallableProxyType.__getattr__
910
_weakref.ProxyType.__getattr__
1011
_weakref.ReferenceType.__call__
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import sys
22
from typing import Any, AnyStr, Callable, ContextManager, Generic, IO, Optional, Text, Type, Union
3-
4-
if sys.version_info >= (3, 6):
5-
from os import PathLike
6-
_Path = Union[str, bytes, PathLike[str], PathLike[bytes]]
7-
else:
8-
_Path = Union[Text, bytes]
3+
from _typeshed import AnyPath
94

105
def replace_atomic(src: AnyStr, dst: AnyStr) -> None: ...
116
def move_atomic(src: AnyStr, dst: AnyStr) -> None: ...
127
class AtomicWriter(object):
13-
def __init__(self, path: _Path, mode: Text = ..., overwrite: bool = ...) -> None: ...
8+
def __init__(self, path: AnyPath, mode: Text = ..., overwrite: bool = ...) -> None: ...
149
def open(self) -> ContextManager[IO[Any]]: ...
1510
def _open(self, get_fileobject: Callable[..., IO[AnyStr]]) -> ContextManager[IO[AnyStr]]: ...
16-
def get_fileobject(self, dir: Optional[_Path] = ..., **kwargs: Any) -> IO[Any]: ...
11+
def get_fileobject(self, dir: Optional[AnyPath] = ..., **kwargs: Any) -> IO[Any]: ...
1712
def sync(self, f: IO[Any]) -> None: ...
1813
def commit(self, f: IO[Any]) -> None: ...
1914
def rollback(self, f: IO[Any]) -> None: ...
2015
def atomic_write(
21-
path: _Path, writer_cls: Type[AtomicWriter] = ..., **cls_kwargs: object,
16+
path: AnyPath, writer_cls: Type[AtomicWriter] = ..., **cls_kwargs: object,
2217
) -> ContextManager[IO[Any]]: ...

third_party/2and3/jinja2/utils.pyi

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from typing import Any, Callable, IO, Iterable, Optional, Protocol, Text, TypeVar, Union
3+
from _typeshed import AnyPath
34

45
from markupsafe import Markup as Markup, escape as escape, soft_unicode as soft_unicode
56

@@ -13,12 +14,6 @@ if sys.version_info >= (3, 8):
1314
else:
1415
_True = bool
1516

16-
if sys.version_info >= (3, 6):
17-
from builtins import _PathLike
18-
_PathType = Union[bytes, Text, _PathLike]
19-
else:
20-
_PathType = Union[bytes, Text]
21-
2217
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any])
2318

2419
class _ContextFunction(Protocol[_CallableT]):
@@ -42,7 +37,7 @@ def select_autoescape(enabled_extensions: Iterable[str] = ..., disabled_extensio
4237
def consume(iterable: Iterable[object]) -> None: ...
4338
def clear_caches() -> None: ...
4439
def import_string(import_name: str, silent: bool = ...) -> Any: ...
45-
def open_if_exists(filename: _PathType, mode: str = ...) -> Optional[IO[Any]]: ...
40+
def open_if_exists(filename: AnyPath, mode: str = ...) -> Optional[IO[Any]]: ...
4641
def object_type_repr(obj: object) -> str: ...
4742
def pformat(obj: object, verbose: bool = ...) -> str: ...
4843
def urlize(text: Union[Markup, Text], trim_url_limit: Optional[int] = ..., rel: Optional[Union[Markup, Text]] = ..., target: Optional[Union[Markup, Text]] = ...) -> str: ...

third_party/2and3/toml.pyi

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
from typing import Any, IO, List, Mapping, MutableMapping, Optional, Protocol, Text, Type, Union
2+
from _typeshed import StrPath
23
import datetime
34
import sys
45

5-
if sys.version_info >= (3, 4):
6+
if sys.version_info >= (3, 6):
7+
_PathLike = StrPath
8+
elif sys.version_info >= (3, 4):
69
import pathlib
7-
if sys.version_info >= (3, 6):
8-
import os
9-
_PathLike = Union[Text, pathlib.PurePath, os.PathLike]
10-
else:
11-
_PathLike = Union[Text, pathlib.PurePath]
10+
_PathLike = Union[StrPath, pathlib.PurePath]
1211
else:
13-
_PathLike = Text
12+
_PathLike = StrPath
1413

1514
class _Writable(Protocol):
1615
def write(self, obj: str) -> Any: ...

0 commit comments

Comments
 (0)