Skip to content

Commit

Permalink
constraints: deprecate constraint.version in favour of constraint.val…
Browse files Browse the repository at this point in the history
…ue for generic constraints
  • Loading branch information
radoering committed Nov 2, 2022
1 parent 432ba1a commit 89e96f5
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/poetry/core/constraints/generic/constraint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import operator
import warnings

from poetry.core.constraints.generic.any_constraint import AnyConstraint
from poetry.core.constraints.generic.base_constraint import BaseConstraint
Expand All @@ -15,17 +16,27 @@ class Constraint(BaseConstraint):

_trans_op_int = {OP_EQ: "==", OP_NE: "!="}

def __init__(self, version: str, operator: str = "==") -> None:
def __init__(self, value: str, operator: str = "==") -> None:
if operator == "=":
operator = "=="

self._version = version
self._value = value
self._operator = operator
self._op = self._trans_op_str[operator]

@property
def value(self) -> str:
return self._value

@property
def version(self) -> str:
return self._version
warnings.warn(
"The property 'version' is deprecated and will be removed. "
"Please use the property 'value' instead.",
DeprecationWarning,
stacklevel=2,
)
return self.value

@property
def operator(self) -> str:
Expand All @@ -41,7 +52,7 @@ def allows(self, other: BaseConstraint) -> bool:
is_other_non_equal_op = other.operator == "!="

if is_equal_op and is_other_equal_op:
return self._version == other.version
return self._value == other.value

if (
is_equal_op
Expand All @@ -51,7 +62,7 @@ def allows(self, other: BaseConstraint) -> bool:
or is_non_equal_op
and is_other_non_equal_op
):
return self._version != other.version
return self._value != other.value

return False

Expand All @@ -67,7 +78,7 @@ def allows_any(self, other: BaseConstraint) -> bool:
is_other_non_equal_op = other.operator == "!="

if is_non_equal_op and is_other_non_equal_op:
return self._version != other.version
return self._value != other.value

return other.allows(self)

Expand Down Expand Up @@ -127,11 +138,11 @@ def __eq__(self, other: object) -> bool:
if not isinstance(other, Constraint):
return NotImplemented

return (self.version, self.operator) == (other.version, other.operator)
return (self.value, self.operator) == (other.value, other.operator)

def __hash__(self) -> int:
return hash((self._operator, self._version))
return hash((self._operator, self._value))

def __str__(self) -> str:
op = self._operator if self._operator != "==" else ""
return f"{op}{self._version}"
return f"{op}{self._value}"

0 comments on commit 89e96f5

Please sign in to comment.