Skip to content

Commit

Permalink
packages: improve type hints for PackageSpecification/Package/Depenen…
Browse files Browse the repository at this point in the history
…cy.name (NormalizedName instead of str) (#442)
  • Loading branch information
radoering authored Aug 14, 2022
1 parent 971f824 commit a5d8cad
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@


if TYPE_CHECKING:
from packaging.utils import NormalizedName

from poetry.core.packages.package import Package


class Metadata:
metadata_version = "2.1"
# version 1.0
name: str | None = None
name: NormalizedName | None = None
version: str
platforms: tuple[str, ...] = ()
supported_platforms: tuple[str, ...] = ()
Expand Down
4 changes: 3 additions & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


if TYPE_CHECKING:
from packaging.utils import NormalizedName

from poetry.core.packages.constraints import BaseConstraint
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
Expand Down Expand Up @@ -96,7 +98,7 @@ def __init__(
self.source_name: str | None = None

@property
def name(self) -> str:
def name(self) -> NormalizedName:
return self._name

@property
Expand Down
4 changes: 3 additions & 1 deletion src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@


if TYPE_CHECKING:
from packaging.utils import NormalizedName

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.semver.version import Version
Expand Down Expand Up @@ -121,7 +123,7 @@ def __init__(
self._yanked = yanked

@property
def name(self) -> str:
def name(self) -> NormalizedName:
return self._name

@property
Expand Down
4 changes: 3 additions & 1 deletion src/poetry/core/packages/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


if TYPE_CHECKING:
from packaging.utils import NormalizedName

T = TypeVar("T", bound="PackageSpecification")


Expand Down Expand Up @@ -38,7 +40,7 @@ def __init__(
self._features = frozenset(features)

@property
def name(self) -> str:
def name(self) -> NormalizedName:
return self._name

@property
Expand Down
10 changes: 2 additions & 8 deletions src/poetry/core/pyproject/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from pathlib import Path
from typing import TYPE_CHECKING

from packaging.utils import canonicalize_name


if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency
Expand Down Expand Up @@ -44,13 +42,9 @@ def dependencies(self) -> list[Dependency]:
# https://docs.python.org/3/library/pathlib.html#methods
with suppress(OSError):
if path.is_file():
dependency = FileDependency(
name=canonicalize_name(path.name), path=path
)
dependency = FileDependency(name=path.name, path=path)
elif path.is_dir():
dependency = DirectoryDependency(
name=canonicalize_name(path.name), path=path
)
dependency = DirectoryDependency(name=path.name, path=path)

if dependency is None:
# skip since we could not determine requirement
Expand Down
7 changes: 6 additions & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import cast

Expand All @@ -12,6 +13,10 @@
from poetry.core.toml import TOMLFile


if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency


fixtures_dir = Path(__file__).parent / "fixtures"


Expand All @@ -37,7 +42,7 @@ def test_create_poetry() -> None:
assert package.python_versions == "~2.7 || ^3.6"
assert str(package.python_constraint) == ">=2.7,<2.8 || >=3.6,<4.0"

dependencies = {}
dependencies: dict[str, Dependency] = {}
for dep in package.requires:
dependencies[dep.name] = dep

Expand Down

0 comments on commit a5d8cad

Please sign in to comment.