Skip to content

Commit

Permalink
refactor: move poetry.core.semver to poetry.core.constraints.version
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Sep 23, 2022
1 parent 94b6754 commit 63533ae
Show file tree
Hide file tree
Showing 33 changed files with 155 additions and 107 deletions.
24 changes: 24 additions & 0 deletions src/poetry/core/constraints/version/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from poetry.core.constraints.version.empty_constraint import EmptyConstraint
from poetry.core.constraints.version.helpers import parse_constraint
from poetry.core.constraints.version.util import constraint_regions
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_range import VersionRange
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)
from poetry.core.constraints.version.version_union import VersionUnion


__all__ = [
"EmptyConstraint",
"Version",
"VersionConstraint",
"VersionRange",
"VersionRangeConstraint",
"VersionUnion",
"constraint_regions",
"parse_constraint",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from typing import TYPE_CHECKING

from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint


if TYPE_CHECKING:
from poetry.core.semver.version import Version
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)


class EmptyConstraint(VersionConstraint):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@


if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint


def parse_constraint(constraints: str) -> VersionConstraint:
if constraints == "*":
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

return VersionRange()

Expand Down Expand Up @@ -44,20 +44,20 @@ def parse_constraint(constraints: str) -> VersionConstraint:
if len(or_groups) == 1:
return or_groups[0]
else:
from poetry.core.semver.version_union import VersionUnion
from poetry.core.constraints.version.version_union import VersionUnion

return VersionUnion.of(*or_groups)


def parse_single_constraint(constraint: str) -> VersionConstraint:
from poetry.core.semver.patterns import BASIC_CONSTRAINT
from poetry.core.semver.patterns import CARET_CONSTRAINT
from poetry.core.semver.patterns import TILDE_CONSTRAINT
from poetry.core.semver.patterns import TILDE_PEP440_CONSTRAINT
from poetry.core.semver.patterns import X_CONSTRAINT
from poetry.core.semver.version import Version
from poetry.core.semver.version_range import VersionRange
from poetry.core.semver.version_union import VersionUnion
from poetry.core.constraints.version.patterns import BASIC_CONSTRAINT
from poetry.core.constraints.version.patterns import CARET_CONSTRAINT
from poetry.core.constraints.version.patterns import TILDE_CONSTRAINT
from poetry.core.constraints.version.patterns import TILDE_PEP440_CONSTRAINT
from poetry.core.constraints.version.patterns import X_CONSTRAINT
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_range import VersionRange
from poetry.core.constraints.version.version_union import VersionUnion

m = re.match(r"(?i)^v?[xX*](\.[xX*])*$", constraint)
if m:
Expand Down Expand Up @@ -142,6 +142,6 @@ def parse_single_constraint(constraint: str) -> VersionConstraint:
return VersionUnion(VersionRange(max=version), VersionRange(min=version))
return version

from poetry.core.semver.exceptions import ParseConstraintError
from poetry.core.constraints.version.exceptions import ParseConstraintError

raise ParseConstraintError(f"Could not parse version constraint: {constraint}")
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from typing import TYPE_CHECKING

from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange


if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint


def constraint_regions(constraints: list[VersionConstraint]) -> list[VersionRange]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

from typing import TYPE_CHECKING

from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.semver.version_union import VersionUnion
from poetry.core.constraints.version.empty_constraint import EmptyConstraint
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)
from poetry.core.constraints.version.version_union import VersionUnion
from poetry.core.version.pep440 import Release
from poetry.core.version.pep440.version import PEP440Version


if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint
from poetry.core.version.pep440 import LocalSegmentType
from poetry.core.version.pep440 import ReleaseTag

Expand Down Expand Up @@ -106,7 +108,7 @@ def intersect(self, other: VersionConstraint) -> Version | EmptyConstraint:
return EmptyConstraint()

def union(self, other: VersionConstraint) -> VersionConstraint:
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

if other.allows(self):
return other
Expand Down Expand Up @@ -146,7 +148,7 @@ def __repr__(self) -> str:
return f"<Version {str(self)}>"

def __eq__(self, other: object) -> bool:
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

if isinstance(other, VersionRange):
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@


if TYPE_CHECKING:
from poetry.core.semver.version import Version
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)


class VersionConstraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

from typing import TYPE_CHECKING

from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.semver.version_union import VersionUnion
from poetry.core.constraints.version.empty_constraint import EmptyConstraint
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)
from poetry.core.constraints.version.version_union import VersionUnion


if TYPE_CHECKING:
from poetry.core.semver.version import Version
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_constraint import VersionConstraint


class VersionRange(VersionRangeConstraint):
Expand Down Expand Up @@ -95,7 +97,7 @@ def allows(self, other: Version) -> bool:
return True

def allows_all(self, other: VersionConstraint) -> bool:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version

if other.is_empty():
return True
Expand All @@ -112,7 +114,7 @@ def allows_all(self, other: VersionConstraint) -> bool:
raise ValueError(f"Unknown VersionConstraint type {other}.")

def allows_any(self, other: VersionConstraint) -> bool:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version

if other.is_empty():
return False
Expand All @@ -131,7 +133,7 @@ def allows_any(self, other: VersionConstraint) -> bool:
raise ValueError(f"Unknown VersionConstraint type {other}.")

def intersect(self, other: VersionConstraint) -> VersionConstraint:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version

if other.is_empty():
return other
Expand Down Expand Up @@ -187,7 +189,7 @@ def intersect(self, other: VersionConstraint) -> VersionConstraint:
)

def union(self, other: VersionConstraint) -> VersionConstraint:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version

if isinstance(other, Version):
if self.allows(other):
Expand Down Expand Up @@ -239,7 +241,7 @@ def union(self, other: VersionConstraint) -> VersionConstraint:
return VersionUnion.of(self, other)

def difference(self, other: VersionConstraint) -> VersionConstraint:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version

if other.is_empty():
return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from abc import abstractmethod
from typing import TYPE_CHECKING

from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint


if TYPE_CHECKING:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version


class VersionRangeConstraint(VersionConstraint):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from typing import TYPE_CHECKING

from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.constraints.version.empty_constraint import EmptyConstraint
from poetry.core.constraints.version.version_constraint import VersionConstraint
from poetry.core.constraints.version.version_range_constraint import (
VersionRangeConstraint,
)


if TYPE_CHECKING:
from poetry.core.semver.version import Version
from poetry.core.constraints.version.version import Version


class VersionUnion(VersionConstraint):
Expand All @@ -29,7 +31,7 @@ def ranges(self) -> list[VersionRangeConstraint]:

@classmethod
def of(cls, *ranges: VersionConstraint) -> VersionConstraint:
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

flattened: list[VersionRangeConstraint] = []
for constraint in ranges:
Expand Down Expand Up @@ -367,7 +369,7 @@ def _excludes_single_wildcard_range_check_is_valid_range(
return _padded_version_two in _check_versions

def excludes_single_wildcard_range(self) -> bool:
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

if len(self._ranges) != 2:
return False
Expand All @@ -389,8 +391,8 @@ def excludes_single_wildcard_range(self) -> bool:
return isinstance(VersionRange().difference(self), VersionRange)

def excludes_single_version(self) -> bool:
from poetry.core.semver.version import Version
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version import Version
from poetry.core.constraints.version.version_range import VersionRange

return isinstance(VersionRange().difference(self), Version)

Expand All @@ -409,7 +411,7 @@ def __hash__(self) -> int:
return h

def __str__(self) -> str:
from poetry.core.semver.version_range import VersionRange
from poetry.core.constraints.version.version_range import VersionRange

if self.excludes_single_version():
return f"!={VersionRange().difference(self)}"
Expand Down
6 changes: 4 additions & 2 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,16 @@ def create_dependency(
from poetry.core.constraints.generic import (
parse_constraint as parse_generic_constraint,
)
from poetry.core.constraints.version import (
parse_constraint as parse_version_constraint,
)
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.semver.helpers import parse_constraint
from poetry.core.version.markers import AnyMarker
from poetry.core.version.markers import parse_marker

Expand Down Expand Up @@ -345,7 +347,7 @@ def create_dependency(
marker = marker.intersect(
parse_marker(
create_nested_marker(
"python_version", parse_constraint(python_versions)
"python_version", parse_version_constraint(python_versions)
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
from packaging.tags import sys_tags

from poetry.core import __version__
from poetry.core.constraints.version import parse_constraint
from poetry.core.masonry.builders.builder import Builder
from poetry.core.masonry.builders.sdist import SdistBuilder
from poetry.core.masonry.utils.helpers import escape_name
from poetry.core.masonry.utils.helpers import normalize_file_permissions
from poetry.core.masonry.utils.package_include import PackageInclude
from poetry.core.semver.helpers import parse_constraint


if TYPE_CHECKING:
Expand Down
12 changes: 6 additions & 6 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
from typing import TypeVar

from poetry.core.constraints.generic import parse_constraint as parse_generic_constraint
from poetry.core.constraints.version import VersionRangeConstraint
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.specification import PackageSpecification
from poetry.core.packages.utils.utils import contains_group_without_marker
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.utils.utils import normalize_python_version_markers
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.version.markers import parse_marker


if TYPE_CHECKING:
from packaging.utils import NormalizedName

from poetry.core.constraints.version import VersionConstraint
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.version.markers import BaseMarker

T = TypeVar("T", bound="Dependency")
Expand Down Expand Up @@ -165,8 +165,8 @@ def marker(self) -> BaseMarker:

@marker.setter
def marker(self, marker: str | BaseMarker) -> None:
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.utils.utils import convert_markers
from poetry.core.semver.helpers import parse_constraint
from poetry.core.version.markers import BaseMarker
from poetry.core.version.markers import parse_marker

Expand Down Expand Up @@ -228,8 +228,8 @@ def in_extras(self) -> list[str]:

@property
def base_pep_508_name(self) -> str:
from poetry.core.semver.version import Version
from poetry.core.semver.version_union import VersionUnion
from poetry.core.constraints.version import Version
from poetry.core.constraints.version import VersionUnion

requirement = self.pretty_name

Expand Down
Loading

0 comments on commit 63533ae

Please sign in to comment.