From 50ac82294e6fc0590bf992636f6f76231466a0b7 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 5 Oct 2025 21:46:31 +0300 Subject: [PATCH 1/4] `importlib.resources.contents` is deprecated --- stdlib/importlib/resources/__init__.pyi | 3 ++- stdlib/importlib/resources/_functional.pyi | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/importlib/resources/__init__.pyi b/stdlib/importlib/resources/__init__.pyi index e672a619bd17..af783d04ddea 100644 --- a/stdlib/importlib/resources/__init__.pyi +++ b/stdlib/importlib/resources/__init__.pyi @@ -5,7 +5,7 @@ from contextlib import AbstractContextManager from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, Literal, TextIO -from typing_extensions import TypeAlias +from typing_extensions import TypeAlias, deprecated if sys.version_info >= (3, 11): from importlib.resources.abc import Traversable @@ -64,6 +64,7 @@ else: def read_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> str: ... def path(package: Package, resource: Resource) -> AbstractContextManager[Path, Literal[False]]: ... def is_resource(package: Package, name: str) -> bool: ... + @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") def contents(package: Package) -> Iterator[str]: ... if sys.version_info >= (3, 11): diff --git a/stdlib/importlib/resources/_functional.pyi b/stdlib/importlib/resources/_functional.pyi index 50f3405f9a00..c3ad260fc511 100644 --- a/stdlib/importlib/resources/_functional.pyi +++ b/stdlib/importlib/resources/_functional.pyi @@ -9,7 +9,7 @@ if sys.version_info >= (3, 13): from io import TextIOWrapper from pathlib import Path from typing import BinaryIO, Literal, overload - from typing_extensions import Unpack + from typing_extensions import Unpack, deprecated def open_binary(anchor: Anchor, *path_names: StrPath) -> BinaryIO: ... @overload @@ -27,4 +27,5 @@ if sys.version_info >= (3, 13): 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, Literal[False]]: ... def is_resource(anchor: Anchor, *path_names: StrPath) -> bool: ... + @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") def contents(anchor: Anchor, *path_names: StrPath) -> Iterator[str]: ... From b16b50e4bd8e036be8ea363c443329b8e40372fe Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 5 Oct 2025 22:02:59 +0300 Subject: [PATCH 2/4] Fix tests --- stdlib/@tests/test_cases/check_importlib_resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/@tests/test_cases/check_importlib_resources.py b/stdlib/@tests/test_cases/check_importlib_resources.py index 2638ca1c1ba9..862456a017e9 100644 --- a/stdlib/@tests/test_cases/check_importlib_resources.py +++ b/stdlib/@tests/test_cases/check_importlib_resources.py @@ -29,4 +29,4 @@ def f(pth: pathlib.Path | str | _CustomPathLike) -> None: importlib.resources.read_binary("pkg", pth) importlib.resources.path("pkg", pth) importlib.resources.is_resource("pkg", pth) - importlib.resources.contents("pkg", pth) + importlib.resources.contents("pkg", pth) # pyright: ignore[reportDeprecated] From b4214757ca3d456c6594e1f8d35039d829bb36e6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 6 Oct 2025 00:21:32 +0300 Subject: [PATCH 3/4] Fix tests --- stdlib/importlib/resources/__init__.pyi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/importlib/resources/__init__.pyi b/stdlib/importlib/resources/__init__.pyi index af783d04ddea..64178b6be544 100644 --- a/stdlib/importlib/resources/__init__.pyi +++ b/stdlib/importlib/resources/__init__.pyi @@ -64,8 +64,11 @@ else: def read_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> str: ... def path(package: Package, resource: Resource) -> AbstractContextManager[Path, Literal[False]]: ... def is_resource(package: Package, name: str) -> bool: ... - @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") - def contents(package: Package) -> Iterator[str]: ... + if sys.version_info >= (3, 11): + @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") + def contents(package: Package) -> Iterator[str]: ... + else: + def contents(package: Package) -> Iterator[str]: ... if sys.version_info >= (3, 11): from importlib.resources._common import as_file as as_file From 0b4410ac2eed2885ae662d0470672ebc5a890030 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 6 Oct 2025 23:32:51 +0300 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Semyon Moroz --- stdlib/importlib/resources/__init__.pyi | 2 +- stdlib/importlib/resources/_functional.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/importlib/resources/__init__.pyi b/stdlib/importlib/resources/__init__.pyi index 64178b6be544..28adc37da4a4 100644 --- a/stdlib/importlib/resources/__init__.pyi +++ b/stdlib/importlib/resources/__init__.pyi @@ -65,7 +65,7 @@ else: def path(package: Package, resource: Resource) -> AbstractContextManager[Path, Literal[False]]: ... def is_resource(package: Package, name: str) -> bool: ... if sys.version_info >= (3, 11): - @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") + @deprecated("Deprecated since Python 3.11. Use `files(anchor).iterdir()`.") def contents(package: Package) -> Iterator[str]: ... else: def contents(package: Package) -> Iterator[str]: ... diff --git a/stdlib/importlib/resources/_functional.pyi b/stdlib/importlib/resources/_functional.pyi index c3ad260fc511..71e01bcd3d5e 100644 --- a/stdlib/importlib/resources/_functional.pyi +++ b/stdlib/importlib/resources/_functional.pyi @@ -27,5 +27,5 @@ if sys.version_info >= (3, 13): 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, Literal[False]]: ... def is_resource(anchor: Anchor, *path_names: StrPath) -> bool: ... - @deprecated("Deprecated since Python 3.11, use `files(anchor).iterdir()`") + @deprecated("Deprecated since Python 3.11. Use `files(anchor).iterdir()`.") def contents(anchor: Anchor, *path_names: StrPath) -> Iterator[str]: ...