From d5fafd84889b0dd2a841d711fa3b83d4ce56816a Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sun, 17 Jul 2022 12:42:59 +0100 Subject: [PATCH 1/2] use packaging.utils.canonicalize_name() --- src/poetry/core/masonry/metadata.py | 3 ++- src/poetry/core/packages/dependency_group.py | 2 +- src/poetry/core/packages/specification.py | 4 ++-- src/poetry/core/pyproject/tables.py | 2 +- src/poetry/core/utils/helpers.py | 11 +++-------- tests/utils/test_helpers.py | 6 ------ 6 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/poetry/core/masonry/metadata.py b/src/poetry/core/masonry/metadata.py index 29cdf5c7c..5a4300b7c 100644 --- a/src/poetry/core/masonry/metadata.py +++ b/src/poetry/core/masonry/metadata.py @@ -45,7 +45,8 @@ class Metadata: @classmethod def from_package(cls, package: Package) -> Metadata: - from poetry.core.utils.helpers import canonicalize_name + from packaging.utils import canonicalize_name + from poetry.core.utils.helpers import normalize_version from poetry.core.version.helpers import format_python_constraint diff --git a/src/poetry/core/packages/dependency_group.py b/src/poetry/core/packages/dependency_group.py index 5bfe2034e..9afa692e4 100644 --- a/src/poetry/core/packages/dependency_group.py +++ b/src/poetry/core/packages/dependency_group.py @@ -31,7 +31,7 @@ def add_dependency(self, dependency: Dependency) -> None: self._dependencies.append(dependency) def remove_dependency(self, name: str) -> None: - from poetry.core.utils.helpers import canonicalize_name + from packaging.utils import canonicalize_name name = canonicalize_name(name) diff --git a/src/poetry/core/packages/specification.py b/src/poetry/core/packages/specification.py index d49025565..5d1523fac 100644 --- a/src/poetry/core/packages/specification.py +++ b/src/poetry/core/packages/specification.py @@ -22,7 +22,7 @@ def __init__( source_subdirectory: str | None = None, features: Iterable[str] | None = None, ) -> None: - from poetry.core.utils.helpers import canonicalize_name + from packaging.utils import canonicalize_name self._pretty_name = name self._name = canonicalize_name(name) @@ -47,7 +47,7 @@ def pretty_name(self) -> str: @property def complete_name(self) -> str: - name = self._name + name: str = self._name if self._features: features = ",".join(sorted(self._features)) diff --git a/src/poetry/core/pyproject/tables.py b/src/poetry/core/pyproject/tables.py index e219c4c3a..25fe008c0 100644 --- a/src/poetry/core/pyproject/tables.py +++ b/src/poetry/core/pyproject/tables.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import TYPE_CHECKING -from poetry.core.utils.helpers import canonicalize_name +from packaging.utils import canonicalize_name if TYPE_CHECKING: diff --git a/src/poetry/core/utils/helpers.py b/src/poetry/core/utils/helpers.py index e538017bc..7b83c5c1f 100644 --- a/src/poetry/core/utils/helpers.py +++ b/src/poetry/core/utils/helpers.py @@ -14,22 +14,17 @@ from typing import Iterator from typing import no_type_check -from poetry.core.version.pep440 import PEP440Version - +from packaging.utils import canonicalize_name -_canonicalize_regex = re.compile(r"[-_.]+") +from poetry.core.version.pep440 import PEP440Version def combine_unicode(string: str) -> str: return unicodedata.normalize("NFC", string) -def canonicalize_name(name: str) -> str: - return _canonicalize_regex.sub("-", name).lower() - - def module_name(name: str) -> str: - return canonicalize_name(name).replace(".", "_").replace("-", "_") + return canonicalize_name(name).replace("-", "_") def normalize_version(version: str) -> str: diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index d9a9b6115..74c89a5b8 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -7,7 +7,6 @@ import pytest -from poetry.core.utils.helpers import canonicalize_name from poetry.core.utils.helpers import combine_unicode from poetry.core.utils.helpers import normalize_version from poetry.core.utils.helpers import parse_requires @@ -144,11 +143,6 @@ def test_parse_requires() -> None: assert result == expected -@pytest.mark.parametrize("raw", ["a-b-c", "a_b-c", "a_b_c", "a-b_c", "a.b-c"]) -def test_utils_helpers_canonical_names(raw: str) -> None: - assert canonicalize_name(raw) == "a-b-c" - - def test_utils_helpers_combine_unicode() -> None: combined_expected = "é" decomposed = "é" From 193a2f7eaddcefe0660cd86270e4e7ae24098679 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 17 Jul 2022 11:44:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/poetry/core/utils/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/poetry/core/utils/helpers.py b/src/poetry/core/utils/helpers.py index 7b83c5c1f..508cecb42 100644 --- a/src/poetry/core/utils/helpers.py +++ b/src/poetry/core/utils/helpers.py @@ -1,7 +1,6 @@ from __future__ import annotations import os -import re import shutil import stat import tempfile