Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update importlib resources for 3.13 #12298

Merged
merged 68 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
43fd437
Update importlib resources for 3.13
max-muoto Jul 9, 2024
90ea2f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
e99fcca
Fix
max-muoto Jul 9, 2024
23e1544
Fix
max-muoto Jul 9, 2024
14d6a76
Overloads
max-muoto Jul 9, 2024
ff35a20
Tweak
max-muoto Jul 9, 2024
d1230c3
Fix
max-muoto Jul 9, 2024
ad24e2a
use unpack
max-muoto Jul 9, 2024
4d1dfca
Add tests
max-muoto Jul 9, 2024
90f1f34
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2024
bac332e
All nullable
max-muoto Jul 9, 2024
ae8bbe0
Tweak
max-muoto Jul 9, 2024
cc428aa
Tweak
max-muoto Jul 9, 2024
55e1c52
Fix
max-muoto Jul 9, 2024
f262d0a
Move to _common
max-muoto Jul 11, 2024
f91ed1e
Use TS
max-muoto Jul 11, 2024
5d9a9b4
Address comment
max-muoto Jul 11, 2024
f4cb586
Fix
max-muoto Jul 11, 2024
ad92305
tweak again
max-muoto Jul 11, 2024
c44c71b
reorder
max-muoto Jul 11, 2024
9c31008
Tweak
max-muoto Jul 11, 2024
bafcf51
Resolve issues
max-muoto Jul 13, 2024
f271829
Move to functional
max-muoto Jul 13, 2024
201ba16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2024
7d46617
Fix
max-muoto Jul 13, 2024
52dfac2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2024
0af767d
tweak
max-muoto Jul 13, 2024
af74ee9
Tweak
max-muoto Jul 13, 2024
d23df97
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2024
8e516d6
Tweak
max-muoto Jul 13, 2024
49828af
Update stdlib/importlib/resources/__init__.pyi
max-muoto Jul 16, 2024
d34c137
Update stdlib/importlib/resources/_functional.pyi
max-muoto Jul 16, 2024
c4dc73f
Add
max-muoto Jul 16, 2024
36a5efc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
a9dcfc6
Tweak
max-muoto Jul 16, 2024
34d62eb
Tweak
max-muoto Jul 16, 2024
e9e7737
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
4f999f8
Fix
max-muoto Jul 16, 2024
00ff92b
Tweaks
max-muoto Jul 16, 2024
1880aa1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
8837644
Pos isn't required
max-muoto Jul 16, 2024
56b8e78
Tweak
max-muoto Jul 16, 2024
1b35fd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
b2c58a1
Use collections.abc.Callable
max-muoto Aug 12, 2024
38ca047
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2024
e0f7095
Merge branch 'main' into importlib.resources
max-muoto Sep 14, 2024
455d5ad
Add ignore
max-muoto Sep 15, 2024
a764ad0
Add appropiate ignores
max-muoto Sep 15, 2024
1c74e63
Fix
max-muoto Sep 15, 2024
87c1e55
Fix
max-muoto Sep 15, 2024
6786f6c
Add ignore
max-muoto Sep 15, 2024
bced879
Account for older versions
max-muoto Sep 15, 2024
e59c349
Fix
max-muoto Sep 15, 2024
6b85a45
Fixes
max-muoto Sep 15, 2024
622360c
Tweak
max-muoto Sep 15, 2024
9de2ab6
Add block
max-muoto Sep 15, 2024
8190c3c
Nest _common
max-muoto Sep 15, 2024
3f3ef73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
61d4465
Fix
max-muoto Sep 15, 2024
9cfef10
Fixes
max-muoto Sep 15, 2024
46921b1
Fix
max-muoto Sep 15, 2024
db7f52c
Update stdlib/importlib/resources/__init__.pyi
max-muoto Sep 16, 2024
60e7c31
Update stdlib/importlib/resources/__init__.pyi
max-muoto Sep 16, 2024
51f9a42
Update stdlib/importlib/resources/__init__.pyi
max-muoto Sep 16, 2024
8d16119
Update stdlib/importlib/resources/__init__.pyi
max-muoto Sep 16, 2024
751673e
Update stdlib/@tests/test_cases/check_importlib_resources.py
max-muoto Sep 16, 2024
141a6e8
Merge branch 'main' into importlib.resources
max-muoto Sep 16, 2024
2587ba7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/@tests/stubtest_allowlists/py312.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ platform.uname_result.processor
unittest.TestCase.__init_subclass__
unittest.case.TestCase.__init_subclass__

# Deprecated argument is supported at runtime by renaming it through a decorator.
importlib.resources._common.files
importlib.resources.files
# Problematic protocol signature at runtime, see source code comments.
importlib.abc.Traversable.open
importlib.resources.abc.Traversable.open
Expand Down
13 changes: 3 additions & 10 deletions stdlib/@tests/stubtest_allowlists/py313.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

# TODO: triage these new errors
_tkinter.create
importlib.resources.Anchor
importlib.resources.Resource
importlib.resources.__all__
importlib.resources.contents
importlib.resources.is_resource
importlib.resources.open_binary
importlib.resources.open_text
importlib.resources.path
importlib.resources.read_binary
importlib.resources.read_text
os.path.splitroot
tkinter.Misc.after_info
tkinter.Misc.busy
Expand Down Expand Up @@ -151,6 +141,9 @@ platform.uname_result.processor
unittest.TestCase.__init_subclass__
unittest.case.TestCase.__init_subclass__

# Deprecated argument is supported at runtime by renaming it through a decorator.
importlib.resources._common.files
importlib.resources.files
# Problematic protocol signature at runtime, see source code comments.
importlib.abc.Traversable.open
importlib.resources.abc.Traversable.open
Expand Down
32 changes: 32 additions & 0 deletions stdlib/@tests/test_cases/check_importlib_resources.py
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)
2 changes: 2 additions & 0 deletions stdlib/VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ importlib.metadata._meta: 3.10-
importlib.metadata.diagnose: 3.13-
importlib.readers: 3.10-
importlib.resources: 3.7-
importlib.resources._common: 3.11-
importlib.resources._functional: 3.13-
importlib.resources.abc: 3.11-
importlib.resources.readers: 3.11-
importlib.resources.simple: 3.11-
Expand Down
4 changes: 2 additions & 2 deletions stdlib/importlib/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ if sys.version_info >= (3, 9):
# which is not the case.
@overload
@abstractmethod
def open(self, mode: Literal["r"] = "r", /, *, encoding: str | None = None, errors: str | None = None) -> IO[str]: ...
def open(self, mode: Literal["r"] = "r", *, encoding: str | None = None, errors: str | None = None) -> IO[str]: ...
@overload
@abstractmethod
def open(self, mode: Literal["rb"], /) -> IO[bytes]: ...
def open(self, mode: Literal["rb"]) -> IO[bytes]: ...
@property
@abstractmethod
def name(self) -> str: ...
Expand Down
54 changes: 39 additions & 15 deletions stdlib/importlib/resources/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,61 @@ from types import ModuleType
from typing import Any, BinaryIO, TextIO
from typing_extensions import TypeAlias

if sys.version_info >= (3, 11):
from importlib.resources._common import Package as Package
else:
Package: TypeAlias = str | ModuleType

if sys.version_info >= (3, 9):
from importlib.abc import Traversable

__all__ = ["Package", "Resource", "contents", "is_resource", "open_binary", "open_text", "path", "read_binary", "read_text"]
__all__ = ["Package", "contents", "is_resource", "open_binary", "open_text", "path", "read_binary", "read_text"]

if sys.version_info >= (3, 9):
__all__ += ["as_file", "files"]

if sys.version_info >= (3, 10):
__all__ += ["ResourceReader"]

Package: TypeAlias = str | ModuleType
if sys.version_info < (3, 13):
__all__ += ["Resource"]

if sys.version_info >= (3, 11):
Resource: TypeAlias = str
else:
if sys.version_info < (3, 11):
Resource: TypeAlias = str | os.PathLike[Any]
elif sys.version_info < (3, 13):
Resource: TypeAlias = str

def open_binary(package: Package, resource: Resource) -> BinaryIO: ...
def open_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> TextIO: ...
def read_binary(package: Package, resource: Resource) -> bytes: ...
def read_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> str: ...
def path(package: Package, resource: Resource) -> AbstractContextManager[Path]: ...
def is_resource(package: Package, name: str) -> bool: ...
def contents(package: Package) -> Iterator[str]: ...
if sys.version_info >= (3, 13):
from importlib.resources._common import Anchor as Anchor

if sys.version_info >= (3, 9):
__all__ += ["Anchor"]

from importlib.resources._functional import (
contents as contents,
is_resource as is_resource,
open_binary as open_binary,
open_text as open_text,
path as path,
read_binary as read_binary,
read_text as read_text,
)

else:
def open_binary(package: Package, resource: Resource) -> BinaryIO: ...
def open_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> TextIO: ...
def read_binary(package: Package, resource: Resource) -> bytes: ...
def read_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> str: ...
def path(package: Package, resource: Resource) -> AbstractContextManager[Path]: ...
def is_resource(package: Package, name: str) -> bool: ...
def contents(package: Package) -> Iterator[str]: ...

if sys.version_info >= (3, 11):
from importlib.resources._common import as_file as as_file
elif sys.version_info >= (3, 9):
def as_file(path: Traversable) -> AbstractContextManager[Path]: ...

if sys.version_info >= (3, 12):
def files(anchor: Package | None = ...) -> Traversable: ...
if sys.version_info >= (3, 11):
from importlib.resources._common import files as files

elif sys.version_info >= (3, 9):
def files(package: Package) -> Traversable: ...
Expand Down
42 changes: 42 additions & 0 deletions stdlib/importlib/resources/_common.pyi
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]: ...
30 changes: 30 additions & 0 deletions stdlib/importlib/resources/_functional.pyi
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]: ...