From a0e7ef50775fc35a288228a7328ee5901a7450ab Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 12 Nov 2024 16:17:07 -0500 Subject: [PATCH] fix: switch pkgutil for importlib-resources (#887) --- .pre-commit-config.yaml | 1 + nox/tox_to_nox.py | 13 ++++++++++--- pyproject.toml | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0bf73e7d..2bf0e87f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,6 +43,7 @@ repos: - orjson # Faster mypy - packaging - importlib_metadata + - importlib_resources - tomli - uv diff --git a/nox/tox_to_nox.py b/nox/tox_to_nox.py index ec81b706..34eeaf9e 100644 --- a/nox/tox_to_nox.py +++ b/nox/tox_to_nox.py @@ -18,8 +18,8 @@ import argparse import os -import pkgutil import re +import sys from configparser import ConfigParser from importlib import metadata from pathlib import Path @@ -29,18 +29,25 @@ import jinja2 import tox.config +if sys.version_info < (3, 9): + from importlib_resources import files +else: + from importlib.resources import files + TOX_VERSION = metadata.version("tox") TOX4 = int(TOX_VERSION.split(".")[0]) >= 4 +DATA = files("nox") + if TOX4: _TEMPLATE = jinja2.Template( - pkgutil.get_data(__name__, "tox4_to_nox.jinja2").decode("utf-8"), # type: ignore[union-attr] + DATA.joinpath("tox4_to_nox.jinja2").read_text(encoding="utf-8"), extensions=["jinja2.ext.do"], ) else: _TEMPLATE = jinja2.Template( - pkgutil.get_data(__name__, "tox_to_nox.jinja2").decode("utf-8"), # type: ignore[union-attr] + DATA.joinpath("tox_to_nox.jinja2").read_text(encoding="utf-8"), extensions=["jinja2.ext.do"], ) diff --git a/pyproject.toml b/pyproject.toml index 80f7ddda..4cf259c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ dependencies = [ "virtualenv>=20.14.1", ] optional-dependencies.tox_to_nox = [ + "importlib-resources; python_version<'3.9'", "jinja2", "tox", ]