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

Use importlib.metadata instead of reading pyproject.toml in init #250

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions pycln/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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
Expand All @@ -20,14 +19,11 @@
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding=UTF8) # pragma: nocover

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):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ classifiers = [
"Topic :: Utilities"]
license = "MIT"
readme = "README.md"
include = ["pyproject.toml"]
packages = [{ include = "pycln" }, { include = "vendor" }]

[tool.poetry.scripts]
Expand Down
13 changes: 4 additions & 9 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
import sys
import tokenize
from os import getenv
from pathlib import Path

import pytest
import requests
import tomlkit
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__}/"
Expand Down
Loading