-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update importlib resources for 3.13 (#12298)
- Loading branch information
Showing
8 changed files
with
153 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import importlib.resources | ||
import pathlib | ||
import sys | ||
|
||
|
||
class _CustomPathLike: | ||
def __fspath__(self) -> str: | ||
return "" | ||
|
||
|
||
if sys.version_info >= (3, 13): | ||
|
||
def f(pth: pathlib.Path | str | _CustomPathLike) -> None: | ||
importlib.resources.open_binary("pkg", pth) | ||
# Encoding defaults to "utf-8" for one arg. | ||
importlib.resources.open_text("pkg", pth) | ||
# Otherwise, it must be specified. | ||
importlib.resources.open_text("pkg", pth, pth) # type: ignore | ||
importlib.resources.open_text("pkg", pth, pth, encoding="utf-8") | ||
|
||
# Encoding defaults to "utf-8" for one arg. | ||
importlib.resources.read_text("pkg", pth) | ||
# Otherwise, it must be specified. | ||
importlib.resources.read_text("pkg", pth, pth) # type: ignore | ||
importlib.resources.read_text("pkg", pth, pth, encoding="utf-8") | ||
|
||
importlib.resources.read_binary("pkg", pth) | ||
importlib.resources.path("pkg", pth) | ||
importlib.resources.is_resource("pkg", pth) | ||
importlib.resources.contents("pkg", pth) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
|
||
# Even though this file is 3.11+ only, Pyright will complain in stubtest for older versions. | ||
if sys.version_info >= (3, 11): | ||
import types | ||
from collections.abc import Callable | ||
from contextlib import AbstractContextManager | ||
from importlib.abc import ResourceReader, Traversable | ||
from pathlib import Path | ||
from typing import overload | ||
from typing_extensions import TypeAlias, deprecated | ||
|
||
Package: TypeAlias = str | types.ModuleType | ||
|
||
if sys.version_info >= (3, 12): | ||
Anchor: TypeAlias = Package | ||
|
||
def package_to_anchor( | ||
func: Callable[[Anchor | None], Traversable] | ||
) -> Callable[[Anchor | None, Anchor | None], Traversable]: ... | ||
@overload | ||
def files(anchor: Anchor | None = None) -> Traversable: ... | ||
@overload | ||
@deprecated("First parameter to files is renamed to 'anchor'") | ||
def files(package: Anchor | None = None) -> Traversable: ... | ||
|
||
else: | ||
def files(package: Package) -> Traversable: ... | ||
|
||
def get_resource_reader(package: types.ModuleType) -> ResourceReader | None: ... | ||
|
||
if sys.version_info >= (3, 12): | ||
def resolve(cand: Anchor | None) -> types.ModuleType: ... | ||
|
||
else: | ||
def resolve(cand: Package) -> types.ModuleType: ... | ||
|
||
if sys.version_info < (3, 12): | ||
def get_package(package: Package) -> types.ModuleType: ... | ||
|
||
def from_package(package: types.ModuleType) -> Traversable: ... | ||
def as_file(path: Traversable) -> AbstractContextManager[Path]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import sys | ||
|
||
# Even though this file is 3.13+ only, Pyright will complain in stubtest for older versions. | ||
if sys.version_info >= (3, 13): | ||
from _typeshed import StrPath | ||
from collections.abc import Iterator | ||
from contextlib import AbstractContextManager | ||
from importlib.resources._common import Anchor | ||
from io import TextIOWrapper | ||
from pathlib import Path | ||
from typing import BinaryIO, overload | ||
from typing_extensions import Unpack | ||
|
||
def open_binary(anchor: Anchor, *path_names: StrPath) -> BinaryIO: ... | ||
@overload | ||
def open_text( | ||
anchor: Anchor, *path_names: Unpack[tuple[StrPath]], encoding: str | None = "utf-8", errors: str | None = "strict" | ||
) -> TextIOWrapper: ... | ||
@overload | ||
def open_text(anchor: Anchor, *path_names: StrPath, encoding: str | None, errors: str | None = "strict") -> TextIOWrapper: ... | ||
def read_binary(anchor: Anchor, *path_names: StrPath) -> bytes: ... | ||
@overload | ||
def read_text( | ||
anchor: Anchor, *path_names: Unpack[tuple[StrPath]], encoding: str | None = "utf-8", errors: str | None = "strict" | ||
) -> str: ... | ||
@overload | ||
def read_text(anchor: Anchor, *path_names: StrPath, encoding: str | None, errors: str | None = "strict") -> str: ... | ||
def path(anchor: Anchor, *path_names: StrPath) -> AbstractContextManager[Path]: ... | ||
def is_resource(anchor: Anchor, *path_names: StrPath) -> bool: ... | ||
def contents(anchor: Anchor, *path_names: StrPath) -> Iterator[str]: ... |