From 5ab0645b0643a535c5ab6ce5ce2eb96319a74f1c Mon Sep 17 00:00:00 2001 From: Christoph Souris Date: Thu, 1 Aug 2024 16:21:57 +0200 Subject: [PATCH] Linting fixes. --- c8y_api/model/tenants.py | 36 +++++++++++++++++++++++------------- tests/model/test_audit.py | 1 + tests/test_util.py | 5 ++++- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/c8y_api/model/tenants.py b/c8y_api/model/tenants.py index d2bdcf5..1bdaf46 100644 --- a/c8y_api/model/tenants.py +++ b/c8y_api/model/tenants.py @@ -10,9 +10,9 @@ from typing import Generator, List from c8y_api._base_api import CumulocityRestApi +from c8y_api.model.applications import Application from c8y_api.model._base import SimpleObject, CumulocityResource from c8y_api.model._parser import SimpleObjectParser -from model import Application class Tenant(SimpleObject): @@ -75,8 +75,8 @@ def __init__(self, self.creation_time = None self.parent = None self.status = None - self._applications = None - self._owned_applications = None + self._applications = [] + self._owned_applications = [] domain = SimpleObject.UpdatableProperty('_u_domain') admin_name = SimpleObject.UpdatableProperty('_u_admin_name') @@ -102,9 +102,8 @@ def applications(self) -> Generator[Application]: Returns: A Generator for Application instances. """ - if self._applications: - for application_json in self._applications: - yield Application.from_json(application_json) + for application_json in self._applications: + yield Application.from_json(application_json) @property def all_applications(self) -> List[Application]: @@ -113,7 +112,7 @@ def all_applications(self) -> List[Application]: Returns: A list of Application instances. """ - return [x for x in self.applications] + return list(self.applications) @property def owned_applications(self) -> Generator[Application]: @@ -122,9 +121,8 @@ def owned_applications(self) -> Generator[Application]: Returns: A Generator for Application instances. """ - if self._owned_applications: - for application_json in self._owned_applications: - yield Application.from_json(application_json) + for application_json in self._owned_applications: + yield Application.from_json(application_json) @property def all_owned_applications(self) -> List[Application]: @@ -133,7 +131,7 @@ def all_owned_applications(self) -> List[Application]: Returns: A list of Application instances. """ - return [x for x in self.owned_applications] + return list(self.owned_applications) @classmethod def from_json(cls, json: dict) -> Tenant: @@ -152,6 +150,7 @@ def from_json(cls, json: dict) -> Tenant: obj = cls._from_json(json, Tenant()) # Extract (but don't parse) referenced application. Parsing is # done lazily in property implementations + # pylint: disable=protected-access if 'applications' in json: obj._applications = [x['application'] for x in json['applications']['references']] if 'ownedApplications' in json: @@ -210,8 +209,19 @@ def get_current(self) -> Tenant: tenant.c8y = self.c8y return tenant - def get(self, id: str) -> Tenant: - tenant = Tenant.from_json(self._get_object(id)) + def get(self, tenant_id: str) -> Tenant: + """Read a specific tenant from the database. + + Args: + tenant_id (str|int): database ID of a tenant + + Returns: + Tenant object + + Raises: + KeyError: if the ID cannot be resolved. + """ + tenant = Tenant.from_json(self._get_object(tenant_id)) tenant.c8y = self.c8y # inject c8y connection into instance return tenant diff --git a/tests/model/test_audit.py b/tests/model/test_audit.py index 45acf5e..110f812 100644 --- a/tests/model/test_audit.py +++ b/tests/model/test_audit.py @@ -3,6 +3,7 @@ # and/or its subsidiaries and/or its affiliates and/or their licensors. # Use, reproduction, transfer, publication or disclosure is prohibited except # as specifically provided for in your License Agreement with Software AG. + # pylint: disable=protected-access import json diff --git a/tests/test_util.py b/tests/test_util.py index 96be5a3..fc05e07 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -4,6 +4,8 @@ # Use, reproduction, transfer, publication or disclosure is prohibited except # as specifically provided for in your License Agreement with Software AG. +# pylint: disable=protected-access + from __future__ import annotations import os @@ -16,6 +18,7 @@ from c8y_api._jwt import JWT from model._util import _StringUtil + @pytest.mark.parametrize( 'name, expected', [ @@ -25,10 +28,10 @@ ('_leading_underscore', 'leadingUnderscore'), ]) def test_snake_to_pascal_case(name, expected): + """Verify that snake case conversion works as expected.""" assert _StringUtil.to_pascal_case(name) == expected - @patch.dict(os.environ, {'C8Y_SOME': 'some', 'C8Y_THING': 'thing', 'C8YNOT': 'not'}, clear=True) def test_c8y_keys(): """Verify that the C8Y_* keys can be filtered from environment."""