Add string representation for python namespace objects

This commit is contained in:
2026-04-08 14:13:12 +02:00
parent 66f06960a9
commit 54e3a30ef0
5 changed files with 37 additions and 4 deletions
+1
View File
@@ -13,3 +13,4 @@
- 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 - Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances
- Add string representation for python namespace objects
@@ -37,6 +37,9 @@ class PythonNamespace:
self.name = name self.name = name
def __repr__(self) -> str:
return f'{type(self).__name__}({self.name!r})'
class PythonPackageNamespace(PythonNamespace): class PythonPackageNamespace(PythonNamespace):
''' '''
A subclass of ``PythonNamespace`` for python package namespaces. A subclass of ``PythonNamespace`` for python package namespaces.
@@ -117,6 +120,9 @@ class PythonPackageNamespace(PythonNamespace):
return value.sub_namespace_names() == self.sub_namespace_names() return value.sub_namespace_names() == self.sub_namespace_names()
def __repr__(self) -> str:
return type(self).__name__ + repr((self.name, self.sub_namespaces))
class PythonModuleNamespace(PythonNamespace): class PythonModuleNamespace(PythonNamespace):
''' '''
A subclass of ``PythonNamespace`` for python module namespaces. A subclass of ``PythonNamespace`` for python module namespaces.
@@ -23,3 +23,11 @@ def test_PythonModuleNamespace_name_attribute(namespace, expected_name):
def test_PythonModuleNamespace___eq__(namespace1, namespace2, expected): def test_PythonModuleNamespace___eq__(namespace1, namespace2, expected):
assert (namespace1 == namespace2) == expected assert (namespace1 == namespace2) == expected
assert (namespace2 == namespace1) == expected assert (namespace2 == namespace1) == expected
@pytest.mark.parametrize('namespace,expected', [
(PythonModuleNamespace('a'), 'PythonModuleNamespace(\'a\')'),
(PythonModuleNamespace('1'), 'PythonModuleNamespace(\'1\')'),
(PythonModuleNamespace(StrSubclass('a')), 'PythonModuleNamespace(\'a\')'),
])
def test_PythonModuleNamespace_string_representation(namespace, expected):
assert repr(namespace) == expected
@@ -10,3 +10,11 @@ class StrSubclass(str): ...
]) ])
def test_PythonNamespace_name_attribute(namespace, expected_name): def test_PythonNamespace_name_attribute(namespace, expected_name):
assert namespace.name == expected_name assert namespace.name == expected_name
@pytest.mark.parametrize('namespace,expected', [
(PythonNamespace('a'), 'PythonNamespace(\'a\')'),
(PythonNamespace('1'), 'PythonNamespace(\'1\')'),
(PythonNamespace(StrSubclass('a')), 'PythonNamespace(\'a\')'),
])
def test_PythonNamespace_string_representation(namespace, expected):
assert repr(namespace) == expected
@@ -76,3 +76,13 @@ def test_PythonPackageNamespace_sub_namespace_names(namespace, expected):
def test_PythonPackageNamespace___eq__(namespace1, namespace2, expected): def test_PythonPackageNamespace___eq__(namespace1, namespace2, expected):
assert (namespace1 == namespace2) == expected assert (namespace1 == namespace2) == expected
assert (namespace2 == namespace1) == expected assert (namespace2 == namespace1) == expected
@pytest.mark.parametrize('namespace,expected', [
(PythonPackageNamespace('a', {}), 'PythonPackageNamespace(\'a\', {})'),
(PythonPackageNamespace('1', {}), 'PythonPackageNamespace(\'1\', {})'),
(PythonPackageNamespace(StrSubclass('a'), {}), 'PythonPackageNamespace(\'a\', {})'),
(namespace_a, 'PythonPackageNamespace(\'a\', {\'b\': PythonModuleNamespace(\'b\')})'),
(namespace_d, 'PythonPackageNamespace(\'d\', {\'e\': PythonPackageNamespace(\'e\', {\'f\': PythonPackageNamespace(\'f\', {\'g\': PythonPackageNamespace(\'g\', {\'h\': PythonPackageNamespace(\'h\', {\'i\': PythonPackageNamespace(\'i\', {\'j\': PythonPackageNamespace(\'j\', {\'k\': PythonModuleNamespace(\'k\')})})})})})})})')
])
def test_PythonPackageNamespace_string_representation(namespace, expected):
assert repr(namespace) == expected