Skip to content
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
12 changes: 4 additions & 8 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,11 @@ def constraint(self) -> VersionConstraint:

@constraint.setter
def constraint(self, constraint: str | VersionConstraint) -> None:
from poetry.core.semver.version_constraint import VersionConstraint
if isinstance(constraint, str):
self._constraint = parse_constraint(constraint)
else:
self._constraint = constraint

try:
if not isinstance(constraint, VersionConstraint):
self._constraint = parse_constraint(constraint)
else:
self._constraint = constraint
except ValueError:
self._constraint = parse_constraint("*")
self._pretty_constraint = str(constraint)

def set_constraint(self, constraint: str | VersionConstraint) -> None:
Expand Down
7 changes: 7 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from poetry.core.packages.dependency import Dependency
from poetry.core.semver.exceptions import ParseConstraintError
from poetry.core.version.markers import parse_marker


Expand Down Expand Up @@ -238,6 +239,12 @@ def test_set_constraint_sets_pretty_constraint() -> None:
assert dependency.pretty_constraint == "^2.0"


def test_set_bogus_constraint_raises_exception() -> None:
dependency = Dependency("A", "^1.0")
with pytest.raises(ParseConstraintError):
dependency.constraint = "^=4.5" # type: ignore[assignment]


def test_with_constraint() -> None:
dependency = Dependency(
"foo",
Expand Down