diff --git a/ocp_resources/namespace.py b/ocp_resources/namespace.py index 59428fdf27..8380e4d886 100644 --- a/ocp_resources/namespace.py +++ b/ocp_resources/namespace.py @@ -38,3 +38,14 @@ def to_dict(self) -> None: _spec["finalizers"] = self.finalizers # End of generated code + + def __str__(self) -> str: + """Return human-readable string "name" rather than default __repr__. + + Calling str, format or print on a Namespace object will result in it being resolved + to its name attribute if found else the repr will be returned. + + Returns: + str: Human-readable string "name" or the output of __repr__. + """ + return name if (name := getattr(self, "name", None)) else self.__repr__() diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 9fe83241c8..486dc75403 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -1368,7 +1368,7 @@ def _get() -> Generator["NamespacedResource|ResourceInstance", None, None]: yield cls( client=dyn_client, name=resource_field.metadata.name, - namespace=resource_field.metadata.namespace, + namespace=str(resource_field.metadata.namespace), ) except TypeError: if raw: @@ -1377,7 +1377,7 @@ def _get() -> Generator["NamespacedResource|ResourceInstance", None, None]: yield cls( client=dyn_client, name=_resources.metadata.name, - namespace=_resources.metadata.namespace, + namespace=str(_resources.metadata.namespace), ) return Resource.retry_cluster_exceptions(func=_get, exceptions_dict=exceptions_dict)