Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent hash #417

Merged
merged 2 commits into from
Jul 31, 2022
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
18 changes: 5 additions & 13 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ def __init__(
self._constraint: BaseConstraint | VersionConstraint
self._parser: Callable[[str], BaseConstraint | VersionConstraint]
self._name = ALIASES.get(name, name)
self._constraint_string = str(constraint)
constraint_string = str(constraint)

# Extract operator and value
m = self._CONSTRAINT_RE.match(self._constraint_string)
m = self._CONSTRAINT_RE.match(constraint_string)
if m is None:
raise ValueError(f"Invalid marker '{self._constraint_string}'")
raise ValueError(f"Invalid marker '{constraint_string}'")

self._operator = m.group(1)
if self._operator is None:
Expand Down Expand Up @@ -227,11 +227,10 @@ def __init__(

self._constraint = self._parser(glue.join(versions))
else:
self._constraint = self._parser(self._constraint_string)
self._constraint = self._parser(constraint_string)
else:
# if we have a in/not in operator we split the constraint
# into a union/multi-constraint of single constraint
constraint_string = self._constraint_string
if self._operator in {"in", "not in"}:
op, glue = ("==", " || ") if self._operator == "in" else ("!=", ", ")
values = re.split("[ ,]+", self._value)
Expand All @@ -243,13 +242,6 @@ def __init__(
def name(self) -> str:
return self._name

@property
def constraint_string(self) -> str:
if self._operator in {"in", "not in"}:
return f"{self._operator} {self._value}"

return self._constraint_string

@property
def constraint(self) -> BaseConstraint | VersionConstraint:
return self._constraint
Expand Down Expand Up @@ -362,7 +354,7 @@ def __eq__(self, other: object) -> bool:
return self._name == other.name and self._constraint == other.constraint

def __hash__(self) -> int:
return hash((self._name, self._constraint_string))
return hash((self._name, self._constraint))

def __str__(self) -> str:
return f'{self._name} {self._operator} "{self._value}"'
Expand Down
21 changes: 8 additions & 13 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@ def test_single_marker() -> None:

assert isinstance(m, SingleMarker)
assert m.name == "sys_platform"
assert m.constraint_string == "==darwin"
assert str(m.constraint) == "darwin"

m = parse_marker('python_version in "2.7, 3.0, 3.1"')

assert isinstance(m, SingleMarker)
assert m.name == "python_version"
assert m.constraint_string == "in 2.7, 3.0, 3.1"
assert str(m.constraint) == ">=2.7.0,<2.8.0 || >=3.0.0,<3.2.0"

m = parse_marker('"2.7" in python_version')

assert isinstance(m, SingleMarker)
assert m.name == "python_version"
assert m.constraint_string == "in 2.7"
assert str(m.constraint) == ">=2.7.0,<2.8.0"

m = parse_marker('python_version not in "2.7, 3.0, 3.1"')

assert isinstance(m, SingleMarker)
assert m.name == "python_version"
assert m.constraint_string == "not in 2.7, 3.0, 3.1"
assert str(m.constraint) == "<2.7.0 || >=2.8.0,<3.0.0 || >=3.2.0"

m = parse_marker(
Expand All @@ -54,10 +51,6 @@ def test_single_marker() -> None:

assert isinstance(m, SingleMarker)
assert m.name == "platform_machine"
assert (
m.constraint_string
== "in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32"
)
assert (
str(m.constraint)
== "x86_64 || X86_64 || aarch64 || AARCH64 || ppc64le || PPC64LE || amd64 ||"
Expand All @@ -71,18 +64,20 @@ def test_single_marker() -> None:

assert isinstance(m, SingleMarker)
assert m.name == "platform_machine"
assert (
m.constraint_string
== "not in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32"
" WIN32"
)
assert (
str(m.constraint)
== "!=x86_64, !=X86_64, !=aarch64, !=AARCH64, !=ppc64le, !=PPC64LE, !=amd64,"
" !=AMD64, !=win32, !=WIN32"
)


def test_single_marker_normalisation() -> None:
m1 = SingleMarker("python_version", ">=3.6")
m2 = SingleMarker("python_version", ">= 3.6")
assert m1 == m2
assert hash(m1) == hash(m2)


def test_single_marker_intersect() -> None:
m = parse_marker('sys_platform == "darwin"')

Expand Down