From 3137990937ca8912c1cf7ced6a5450819ffa2e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 3 Jul 2022 15:22:26 +0200 Subject: [PATCH] Add explanatory comment and tests --- src/poetry/core/packages/dependency.py | 5 +++++ tests/packages/test_dependency.py | 28 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/poetry/core/packages/dependency.py b/src/poetry/core/packages/dependency.py index 8715c27d6..bbf3a1dee 100644 --- a/src/poetry/core/packages/dependency.py +++ b/src/poetry/core/packages/dependency.py @@ -574,6 +574,11 @@ def __eq__(self, other: object) -> bool: if not isinstance(other, Dependency): return NotImplemented + # "constraint" is implicitly given for direct origin dependencies and might not + # be set yet ("*"). Thus, it shouldn't be used to determine if two direct origin + # dependencies are equal. + # Calling is_direct_origin() for one dependency is sufficient because + # super().__eq__() returns False for different origins. return super().__eq__(other) and ( self._constraint == other.constraint or self.is_direct_origin() ) diff --git a/tests/packages/test_dependency.py b/tests/packages/test_dependency.py index 5089632e9..a64e641f9 100644 --- a/tests/packages/test_dependency.py +++ b/tests/packages/test_dependency.py @@ -361,6 +361,34 @@ def test_create_from_pep_508_url_with_activated_extras() -> None: assert dependency.extras == {"fred", "bar"} +@pytest.mark.parametrize( + "dependency1, dependency2, expected", + [ + (Dependency("a", "1.0"), Dependency("a", "1.0"), True), + (Dependency("a", "1.0"), Dependency("a", "1.0.1"), False), + (Dependency("a", "1.0"), Dependency("a1", "1.0"), False), + (Dependency("a", "1.0"), Dependency("a", "1.0", source_type="file"), False), + # constraint is implicitly given for direct origin dependencies, + # but might not be set + ( + Dependency("a", "1.0", source_type="file"), + Dependency("a", "*", source_type="file"), + True, + ), + # constraint is not implicit for non direct origin dependencies + (Dependency("a", "1.0"), Dependency("a", "*"), False), + ( + Dependency("a", "1.0", source_type="legacy"), + Dependency("a", "*", source_type="legacy"), + False, + ), + ], +) +def test_eq(dependency1: Dependency, dependency2: Dependency, expected: bool) -> None: + assert (dependency1 == dependency2) is expected + assert (dependency2 == dependency1) is expected + + @pytest.mark.parametrize( "attr_name, value", [