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 packaging.utils.canonicalize_name() #418

Merged
merged 2 commits into from
Jul 18, 2022
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
3 changes: 2 additions & 1 deletion src/poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/packages/dependency_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions src/poetry/core/packages/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/pyproject/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 3 additions & 9 deletions src/poetry/core/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
import re
import shutil
import stat
import tempfile
Expand All @@ -14,22 +13,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:
Expand Down
6 changes: 0 additions & 6 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "é"
Expand Down