Add feature to compare core.python.namespaces.PythonPackageNamespace instances or core.python.namespaces.PythonModuleNamespace instances

This commit is contained in:
2026-04-08 13:33:12 +02:00
parent 286ba68c34
commit 66f06960a9
4 changed files with 39 additions and 4 deletions
+1
View File
@@ -12,3 +12,4 @@
- Add type checking for the `name` attribute at the constructor of `core.python.namespaces.PythonNamespace` - 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 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` - 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
+12 -1
View File
@@ -37,7 +37,6 @@ class PythonNamespace:
self.name = name self.name = name
class PythonPackageNamespace(PythonNamespace): class PythonPackageNamespace(PythonNamespace):
''' '''
A subclass of ``PythonNamespace`` for python package namespaces. A subclass of ``PythonNamespace`` for python package namespaces.
@@ -109,6 +108,15 @@ class PythonPackageNamespace(PythonNamespace):
return self._sub_namespaces_names(self) 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): class PythonModuleNamespace(PythonNamespace):
''' '''
A subclass of ``PythonNamespace`` for python module namespaces. A subclass of ``PythonNamespace`` for python module namespaces.
@@ -119,3 +127,6 @@ class PythonModuleNamespace(PythonNamespace):
def __init__(self, name: str) -> None: def __init__(self, name: str) -> None:
super().__init__(name) super().__init__(name)
def __eq__(self, value: PythonModuleNamespace) -> bool:
return self.name == value.name
@@ -10,3 +10,16 @@ class StrSubclass(str): ...
]) ])
def test_PythonModuleNamespace_name_attribute(namespace, expected_name): def test_PythonModuleNamespace_name_attribute(namespace, expected_name):
assert namespace.name == 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
@@ -66,3 +66,13 @@ def test_PythonPackageNamespace_namespace_exceptions(namespace, sub_namespaces,
def test_PythonPackageNamespace_sub_namespace_names(namespace, expected): def test_PythonPackageNamespace_sub_namespace_names(namespace, expected):
print('NAMESPACE NAMESPACE NAMESPACE NAMESPACE NAMESPACE:', namespace) print('NAMESPACE NAMESPACE NAMESPACE NAMESPACE NAMESPACE:', namespace)
assert namespace.sub_namespace_names() == expected 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