Skip to content

Commit 5067515

Browse files
committed
refactor: resolve WPS210, WPS221, WPS229, WPS231 violations
1 parent f20b71e commit 5067515

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

cli/core/accounts/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ def get_active_account() -> Account:
161161

162162
try:
163163
account = find_active_account(accounts)
164-
console.print(f"Current active account: {account.id} ({account.name})")
165164
except NoActiveAccountFoundError as error:
166165
console.print(str(error))
167166
raise typer.Exit(code=3)
167+
else:
168+
console.print(f"Current active account: {account.id} ({account.name})")
168169

169170
return account
170171

cli/core/handlers/horizontal_tab_file_manager.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def write_error(self, error: str, resource_id: str | None = None) -> None:
8282
if row[self._id_field]["value"] == resource_id
8383
)
8484
try:
85-
coordinate = item_row[ERROR_COLUMN_NAME]["coordinate"]
86-
column_letter, row_number = self._get_row_and_column_from_coordinate(coordinate)
85+
column_letter, row_number = self._resolve_error_cell_position(item_row)
8786
except (KeyError, ValueError):
8887
column_letter = self.file_handler.get_sheet_next_column(self._sheet_name)
8988
coordinate = next(iter(item_row.values()))["coordinate"]
@@ -92,13 +91,15 @@ def write_error(self, error: str, resource_id: str | None = None) -> None:
9291

9392
self.file_handler.write([{self._sheet_name: {f"{column_letter}{row_number}": error}}])
9493

94+
def _get_currency_and_precision(self, record: DataModel) -> tuple[str | None, int | None]:
95+
return record.currency, record.precision # type: ignore[attr-defined]
96+
9597
def _get_style(self, record: DataModel, cell_value: Any) -> NamedStyle | None:
9698
if not isinstance(cell_value, float):
9799
return None
98100

99101
try:
100-
currency = record.currency # type: ignore[attr-defined]
101-
precision = record.precision # type: ignore[attr-defined]
102+
currency, precision = self._get_currency_and_precision(record)
102103
except AttributeError:
103104
return None
104105

@@ -107,6 +108,10 @@ def _get_style(self, record: DataModel, cell_value: Any) -> NamedStyle | None:
107108

108109
return get_number_format_style(currency, precision)
109110

111+
def _resolve_error_cell_position(self, item_row: dict[str, Any]) -> tuple[str, int]:
112+
coordinate = item_row[ERROR_COLUMN_NAME]["coordinate"]
113+
return self._get_row_and_column_from_coordinate(coordinate)
114+
110115
@abstractmethod
111116
def _read_data(self) -> Generator[dict[str, Any], None, None]:
112117
raise NotImplementedError

cli/core/mpt/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def join_url(self, url: str) -> str:
7676
The full URL as a string.
7777
7878
"""
79-
url = url[1:] if url and url[0] == "/" else url
79+
url = url.removeprefix("/")
8080
return urljoin(self.base_url, url, allow_fragments=True)
8181

8282

cli/core/price_lists/services/item_service.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def export(self) -> ServiceResult:
2525
limit = 100
2626
select = "audit,item.terms,priceList.precision,priceList.currency"
2727
while True:
28+
query_params = {"select": select, "offset": offset, "limit": limit}
2829
try:
29-
response = self.api.list({"select": select, "offset": offset, "limit": limit})
30+
response = self.api.list(query_params)
3031
except MPTAPIError as error:
3132
self.stats.add_error(TAB_PRICE_ITEMS)
3233
return ServiceResult(
@@ -62,14 +63,14 @@ def retrieve_from_mpt(self, resource_id: str) -> ServiceResult:
6263

6364
@override
6465
def update(self) -> ServiceResult:
65-
errors = []
66+
errors: list[str] = []
6667
for record in self.file_manager.read_data():
6768
if not record.to_update():
6869
self._set_skipped()
6970
continue
7071

72+
query_params = {"item.ExternalIds.vendor": record.vendor_id, "limit": 1}
7173
try:
72-
query_params = {"item.ExternalIds.vendor": record.vendor_id, "limit": 1}
7374
item_data = self.api.list(query_params=query_params)["data"][0]
7475
except MPTAPIError as error:
7576
errors.append(str(error))
@@ -80,8 +81,10 @@ def update(self) -> ServiceResult:
8081
record.type = "operations" if self.account.is_operations() else "vendor"
8182
try:
8283
self.api.update(item_data["id"], record.to_json())
83-
self._set_synced(record.id, record.coordinate)
8484
except MPTAPIError as error:
8585
errors.append(f"Item {record.id}: {error!s}")
8686
self._set_error(str(error), record.id)
87-
return ServiceResult(success=len(errors) == 0, errors=errors, model=None, stats=self.stats)
87+
continue
88+
89+
self._set_synced(record.id, record.coordinate)
90+
return ServiceResult(success=not errors, errors=errors, model=None, stats=self.stats)

cli/core/products/services/related_components_base_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def update(self) -> ServiceResult:
108108

109109
self._set_synced(data_model.id, data_model.coordinate)
110110

111-
return ServiceResult(success=len(errors) == 0, errors=errors, model=None, stats=self.stats)
111+
return ServiceResult(success=not errors, errors=errors, model=None, stats=self.stats)
112112

113113
def prepare_data_model_to_create(self, data_model: DataModel) -> DataModel:
114114
"""Hook method to customize the data model before creating it.

cli/plugins/audit_plugin/api.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ def get_audit_trail(client: Any, record_id: str) -> dict[str, Any]:
1111

1212
try:
1313
response = client.get(f"{endpoint}?{query_string}")
14-
return response.json()
14+
response_payload = response.json()
1515
except Exception as error:
1616
console.print(
1717
f"[red]Failed to retrieve audit trail for record {record_id}: {error!s}[/red]"
1818
)
1919
raise typer.Exit(1)
2020

21+
return response_payload
22+
2123

2224
def get_audit_records_by_object(
2325
client: Any, object_id: str, limit: int = 10
@@ -31,14 +33,16 @@ def get_audit_records_by_object(
3133

3234
try:
3335
response = client.get(f"{endpoint}?{query_string}")
34-
records = response.json().get("data", [])
35-
if not records:
36-
console.print(f"[red]No audit records found for object {object_id}[/red]")
37-
raise typer.Exit(1) # noqa: TRY301
36+
response_payload = response.json()
3837
except Exception as error:
3938
console.print(
4039
f"[red]Failed to retrieve audit records for object {object_id}: {error!s}[/red]"
4140
)
4241
raise typer.Exit(1)
4342

43+
records = response_payload.get("data", [])
44+
if not records:
45+
console.print(f"[red]No audit records found for object {object_id}[/red]")
46+
raise typer.Exit(1)
47+
4448
return records

0 commit comments

Comments
 (0)