From 66f06960a95b2dd8e2d89ed875d4386120d8ab19 Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Wed, 8 Apr 2026 13:33:12 +0200 Subject: [PATCH] Add feature to compare core.python.namespaces.PythonPackageNamespace instances or core.python.namespaces.PythonModuleNamespace instances --- docs/CHANGELOG.md | 3 ++- src/jcloud_docsgen/core/python/namespaces.py | 15 +++++++++++++-- .../namespaces/test_PythonModuleNamespace.py | 13 +++++++++++++ .../namespaces/test_PythonPackageNamespace.py | 12 +++++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5ee0752..b0da146 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,4 +11,5 @@ - Add python namespaces classes - Add type checking for the `name` attribute at the constructor of `core.python.namespaces.PythonNamespace` - Add check whether sub-namespaces are not empty in `core.python.namespaces.PythonPackageNamespace.namespace` -- Add feature to get the names of all sub-namespaces of a `core.python.namespaces.PythonPackageNamespace` \ No newline at end of file +- Add feature to get the names of all sub-namespaces of a `core.python.namespaces.PythonPackageNamespace` +- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances \ No newline at end of file diff --git a/src/jcloud_docsgen/core/python/namespaces.py b/src/jcloud_docsgen/core/python/namespaces.py index 908a8f6..a4896d8 100644 --- a/src/jcloud_docsgen/core/python/namespaces.py +++ b/src/jcloud_docsgen/core/python/namespaces.py @@ -37,7 +37,6 @@ class PythonNamespace: self.name = name - class PythonPackageNamespace(PythonNamespace): ''' A subclass of ``PythonNamespace`` for python package namespaces. @@ -109,6 +108,15 @@ class PythonPackageNamespace(PythonNamespace): return self._sub_namespaces_names(self) + def __eq__(self, value: PythonPackageNamespace) -> bool: + # For performance reasons, is not called self.sub_namespace_names + # if the name attribute does not match, which can be quickly + # verified + if self.name != value.name: + return False + + return value.sub_namespace_names() == self.sub_namespace_names() + class PythonModuleNamespace(PythonNamespace): ''' A subclass of ``PythonNamespace`` for python module namespaces. @@ -118,4 +126,7 @@ class PythonModuleNamespace(PythonNamespace): ''' def __init__(self, name: str) -> None: - super().__init__(name) \ No newline at end of file + super().__init__(name) + + def __eq__(self, value: PythonModuleNamespace) -> bool: + return self.name == value.name \ No newline at end of file diff --git a/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py b/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py index ce815a4..f22bdd9 100644 --- a/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py +++ b/tests/unit/core/python/namespaces/test_PythonModuleNamespace.py @@ -10,3 +10,16 @@ class StrSubclass(str): ... ]) def test_PythonModuleNamespace_name_attribute(namespace, expected_name): assert namespace.name == expected_name + +@pytest.mark.parametrize('namespace1,namespace2,expected', [ + (PythonModuleNamespace('a'), PythonModuleNamespace('a'), True), + (PythonModuleNamespace('b'), PythonModuleNamespace('b'), True), + (PythonModuleNamespace('1'), PythonModuleNamespace('1'), True), + (PythonModuleNamespace('42'), PythonModuleNamespace('42'), True), + (PythonModuleNamespace('a'), PythonModuleNamespace('b'), False), + (PythonModuleNamespace('0'), PythonModuleNamespace('-0'), False), + (PythonModuleNamespace('1'), PythonModuleNamespace('a'), False), +]) +def test_PythonModuleNamespace___eq__(namespace1, namespace2, expected): + assert (namespace1 == namespace2) == expected + assert (namespace2 == namespace1) == expected \ No newline at end of file diff --git a/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py b/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py index 3a4758a..d9917c9 100644 --- a/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py +++ b/tests/unit/core/python/namespaces/test_PythonPackageNamespace.py @@ -65,4 +65,14 @@ def test_PythonPackageNamespace_namespace_exceptions(namespace, sub_namespaces, ]) def test_PythonPackageNamespace_sub_namespace_names(namespace, expected): print('NAMESPACE NAMESPACE NAMESPACE NAMESPACE NAMESPACE:', namespace) - assert namespace.sub_namespace_names() == expected \ No newline at end of file + assert namespace.sub_namespace_names() == expected + +@pytest.mark.parametrize('namespace1,namespace2,expected', [ + (PythonPackageNamespace('a', {'b': PythonModuleNamespace('b')}), PythonPackageNamespace('a', {'b': PythonModuleNamespace('b')}), True), + (PythonPackageNamespace('a', {'b': PythonModuleNamespace('b'), 'c': PythonPackageNamespace('c', {'d': PythonModuleNamespace('d')})}), PythonPackageNamespace('a', {'b': PythonModuleNamespace('b'), 'c': PythonPackageNamespace('c', {'d': PythonModuleNamespace('d')})}), True), + (PythonPackageNamespace('a', {}), PythonPackageNamespace('a', {}), True), + (PythonPackageNamespace('a', {}), PythonPackageNamespace('b', {}), False), +]) +def test_PythonPackageNamespace___eq__(namespace1, namespace2, expected): + assert (namespace1 == namespace2) == expected + assert (namespace2 == namespace1) == expected \ No newline at end of file