From 88c9ed2e17c18ecb80fed5b9d33c22b7bd18ef08 Mon Sep 17 00:00:00 2001 From: Or Harambam Date: Sun, 5 Jan 2025 23:41:26 +0200 Subject: [PATCH 1/6] Use importlib.metadata instead of reading pyproject.toml as it may not exist --- pycln/__init__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pycln/__init__.py b/pycln/__init__.py index 1fc405f..892a770 100644 --- a/pycln/__init__.py +++ b/pycln/__init__.py @@ -1,12 +1,12 @@ import io import os import sys -import tokenize +from importlib.metadata import metadata from pathlib import Path -import tomlkit import typer + #: Add vendor directory to module search path VENDOR_PATH = Path(__file__).parent.parent.joinpath("vendor") sys.path.append(str(VENDOR_PATH)) @@ -22,12 +22,10 @@ ISWIN = os.name == "nt" PYPROJECT_PATH = Path(__file__).parent.parent.joinpath("pyproject.toml") -with tokenize.open(PYPROJECT_PATH) as toml_f: - pycln = tomlkit.parse(toml_f.read())["tool"]["poetry"] - -__name__ = str(pycln["name"]) -__doc__ = str(pycln["description"]) -__version__ = pycln["version"] +pycln = metadata("pycln") +__name__ = str(pycln["Name"]) +__doc__ = str(pycln["Summary"]) +__version__ = str(pycln["Version"]) def version_callback(value: bool): From b9c924ecab8110970c591925b55e68bb4d23d234 Mon Sep 17 00:00:00 2001 From: Hadi Alqattan Date: Mon, 6 Jan 2025 21:44:31 +0300 Subject: [PATCH 2/6] add: change log --- docs/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9164f6d..a372eec 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Fixed + +- [Reading `pyproject.toml` causes missing `pyproject.toml` error by @or150](https://github.com/hadialqattan/pycln/pull/250) + ## Changed - [Drop Python3.7 by @hadialqattan](https://github.com/hadialqattan/pycln) From f9073c45121973dc933a88047fc7ed2190b4b5c8 Mon Sep 17 00:00:00 2001 From: Hadi Alqattan Date: Mon, 6 Jan 2025 21:47:10 +0300 Subject: [PATCH 3/6] fix: missing pre-commit run --- pycln/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pycln/__init__.py b/pycln/__init__.py index 892a770..3394266 100644 --- a/pycln/__init__.py +++ b/pycln/__init__.py @@ -6,7 +6,6 @@ import typer - #: Add vendor directory to module search path VENDOR_PATH = Path(__file__).parent.parent.joinpath("vendor") sys.path.append(str(VENDOR_PATH)) From d7cfd4af5120e5850a365fae20ec83319a4cd39e Mon Sep 17 00:00:00 2001 From: Hadi Alqattan Date: Mon, 6 Jan 2025 22:01:30 +0300 Subject: [PATCH 4/6] fix: stop shipping pyproject.toml --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fb96218..484b74d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,6 @@ classifiers = [ "Topic :: Utilities"] license = "MIT" readme = "README.md" -include = ["pyproject.toml"] packages = [{ include = "pycln" }, { include = "vendor" }] [tool.poetry.scripts] From a25941738ff8d96c557971c81766ac37ec06e5f5 Mon Sep 17 00:00:00 2001 From: Hadi Alqattan Date: Mon, 6 Jan 2025 22:02:20 +0300 Subject: [PATCH 5/6] fix(ci): also install pycln to read the metadata --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7798717..a4b14a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install poetry - python -m poetry install --no-root --with=dev + python -m poetry install --with=dev - name: Test with pytest by poetry run: | From 298c9b4203930c45e3be5390adfe340e01d0c6bb Mon Sep 17 00:00:00 2001 From: Hadi Alqattan Date: Mon, 6 Jan 2025 22:05:50 +0300 Subject: [PATCH 6/6] change: PYPROJECT_PATH is only needed in testing for now --- pycln/__init__.py | 1 - tests/test_metadata.py | 13 ++++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/pycln/__init__.py b/pycln/__init__.py index 3394266..4a7fd74 100644 --- a/pycln/__init__.py +++ b/pycln/__init__.py @@ -19,7 +19,6 @@ sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding=UTF8) # pragma: nocover ISWIN = os.name == "nt" -PYPROJECT_PATH = Path(__file__).parent.parent.joinpath("pyproject.toml") pycln = metadata("pycln") __name__ = str(pycln["Name"]) diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 61aa300..5cec795 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -4,6 +4,7 @@ import sys import tokenize from os import getenv +from pathlib import Path import pytest import requests @@ -11,20 +12,14 @@ from semver import VersionInfo from typer import Exit -from pycln import ( - PYPROJECT_PATH, - VENDOR_PATH, - __doc__, - __name__, - __version__, - version_callback, -) +from pycln import VENDOR_PATH, __doc__, __name__, __version__, version_callback from .utils import sysu # Constants. +PYPROJECT_PATH = Path(__file__).parent.parent.joinpath("pyproject.toml") with tokenize.open(PYPROJECT_PATH) as toml_f: - PYCLN_METADATA = tomlkit.parse(toml_f.read())["tool"]["poetry"] + PYCLN_METADATA = tomlkit.parse(toml_f.read())["tool"]["poetry"] # type: ignore PYCLN_PYPI_JSON_URL = f"https://pypi.org/pypi/{__name__}/json" PYCLN_PYPI_URL = f"https://pypi.org/project/{__name__}/"