From f61f8df5e2e8c67bf154614ff71b3821b02cbb6f Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 2 Sep 2022 21:25:29 +0100 Subject: [PATCH] Don't tolerate invalid constraints --- src/poetry/core/packages/dependency.py | 12 ++++-------- tests/packages/test_dependency.py | 7 +++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/poetry/core/packages/dependency.py b/src/poetry/core/packages/dependency.py index 4310d6953..d604b399f 100644 --- a/src/poetry/core/packages/dependency.py +++ b/src/poetry/core/packages/dependency.py @@ -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: diff --git a/tests/packages/test_dependency.py b/tests/packages/test_dependency.py index a9bbb496a..bdcb3aef6 100644 --- a/tests/packages/test_dependency.py +++ b/tests/packages/test_dependency.py @@ -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 @@ -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",