Skip to content

Commit

Permalink
Don't tolerate invalid constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and neersighted committed Sep 2, 2022
1 parent 734ef94 commit 323e277
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
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

0 comments on commit 323e277

Please sign in to comment.