Skip to content

Commit

Permalink
fix: eliminate the importlib.resources warnings (#1661)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Feb 3, 2023
1 parent a188032 commit 92f5f5f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
run: pdm run pytest -n auto --cov=pdm --cov-config=setup.cfg --cov-report=xml tests
- name: pack pdm
run: |
python -m pip install -U pdm-packer
pdm self add pdm-packer
pdm pack
python pdm.pyz --version
- name: Upload coverage to Codecov
Expand Down
1 change: 1 addition & 0 deletions news/1660.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminate the deprecation warnings from `importlib.resources`.
6 changes: 2 additions & 4 deletions src/pdm/__version__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import importlib.resources

from pdm.compat import importlib_metadata
from pdm.compat import importlib_metadata, resources_read_text


def read_version() -> str:
try:
return importlib_metadata.version(__package__ or "pdm")
except importlib_metadata.PackageNotFoundError:
return importlib.resources.read_text("pdm.models", "VERSION").strip()
return resources_read_text("pdm.models", "VERSION").strip()


__version__ = read_version()
6 changes: 2 additions & 4 deletions src/pdm/cli/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import argparse
import importlib.resources
import sys

from pdm.cli.commands.base import BaseCommand
from pdm.cli.options import Option
from pdm.compat import resources_read_text
from pdm.exceptions import PdmUsageError
from pdm.project import Project

Expand All @@ -31,8 +31,6 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
if shell not in self.SUPPORTED_SHELLS:
raise PdmUsageError(f"Unsupported shell: {shell}")
suffix = "ps1" if shell in {"powershell", "pwsh"} else shell
completion = importlib.resources.read_text(
"pdm.cli.completions", f"pdm.{suffix}"
)
completion = resources_read_text("pdm.cli.completions", f"pdm.{suffix}")
# Can't use rich print or otherwise the rich markups will be interpreted
print(completion.replace("%{python_executable}", sys.executable))
4 changes: 2 additions & 2 deletions src/pdm/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def get_dist_location(dist: Distribution) -> str:


def get_pep582_path(project: Project) -> str:
import importlib.resources
from pdm.compat import resources_open_binary

script_dir = Path(__file__).parent.parent / "pep582"
if script_dir.exists():
Expand All @@ -740,6 +740,6 @@ def get_pep582_path(project: Project) -> str:
if script_dir.joinpath("sitecustomize.py").exists():
return str(script_dir)
script_dir.mkdir(parents=True, exist_ok=True)
with importlib.resources.open_binary("pdm.pep582", "sitecustomize.py") as f:
with resources_open_binary("pdm.pep582", "sitecustomize.py") as f:
script_dir.joinpath("sitecustomize.py").write_bytes(f.read())
return str(script_dir)
31 changes: 31 additions & 0 deletions src/pdm/compat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
import importlib.resources
import sys
from pathlib import Path
from typing import BinaryIO, ContextManager

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


if (
sys.version_info >= (3, 9)
and not (sys.version_info[:2] == (3, 9) and sys.platform == "win32")
# a bug on windows+py39 that zipfile path is not normalized
):

def resources_open_binary(package: str, resource: str) -> BinaryIO:
return (importlib.resources.files(package) / resource).open("rb")

def resources_read_text(
package: str, resource: str, encoding: str = "utf-8", errors: str = "strict"
) -> str:
with (importlib.resources.files(package) / resource).open(
"r", encoding=encoding, errors=errors
) as f:
return f.read()

def resources_path(package: str, resource: str) -> ContextManager[Path]:
return importlib.resources.as_file(
importlib.resources.files(package) / resource
)

else:
resources_open_binary = importlib.resources.open_binary
resources_read_text = importlib.resources.read_text
resources_path = importlib.resources.path


if sys.version_info >= (3, 8):
from functools import cached_property
from typing import Literal, Protocol, TypedDict
Expand Down
5 changes: 3 additions & 2 deletions src/pdm/models/in_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

import contextlib
import functools
import importlib.resources
import json
import os
import subprocess
from typing import Any, Generator

from pdm.compat import resources_path


@contextlib.contextmanager
def _in_process_script(name: str) -> Generator[str, None, None]:
with importlib.resources.path(__name__, name) as script:
with resources_path(__name__, name) as script:
yield str(script)


Expand Down
4 changes: 2 additions & 2 deletions src/pdm/models/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


def _read_max_versions() -> dict[Version, int]:
from importlib.resources import open_binary
from pdm.compat import resources_open_binary

with open_binary("pdm.models", "python_max_versions.json") as fp:
with resources_open_binary("pdm.models", "python_max_versions.json") as fp:
return {Version(k): v for k, v in json.load(fp).items()}


Expand Down

0 comments on commit 92f5f5f

Please sign in to comment.