From c066c9798922b02a24548717181d9451efb1695e Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Wed, 24 Jan 2024 12:35:34 +0100 Subject: [PATCH] Update pyproject setting, remove pytest.ini and fix ruff findings (#74) --- aioelectricitymaps/decorators.py | 2 +- aioelectricitymaps/electricitymaps.py | 29 ++++++++++++++--------- aioelectricitymaps/marshmallow.py | 4 ++-- aioelectricitymaps/models.py | 3 ++- pyproject.toml | 33 +++++++++++++++------------ pytest.ini | 2 -- tests/conftest.py | 2 +- tests/test_electricitymaps.py | 5 ++-- 8 files changed, 46 insertions(+), 34 deletions(-) delete mode 100644 pytest.ini diff --git a/aioelectricitymaps/decorators.py b/aioelectricitymaps/decorators.py index 7ab8e60..75e76b9 100644 --- a/aioelectricitymaps/decorators.py +++ b/aioelectricitymaps/decorators.py @@ -11,7 +11,7 @@ def retry_legacy( - func: Callable[_P, Coroutine[Any, Any, _R]] + func: Callable[_P, Coroutine[Any, Any, _R]], ) -> Callable[_P, Coroutine[Any, Any, _R]]: """Decorator to retry a function with the legacy API if SwitchedToLegacyAPI is raised.""" diff --git a/aioelectricitymaps/electricitymaps.py b/aioelectricitymaps/electricitymaps.py index 0b2e54e..4228fb8 100644 --- a/aioelectricitymaps/electricitymaps.py +++ b/aioelectricitymaps/electricitymaps.py @@ -1,9 +1,9 @@ """Async Python client for electricitymaps.com.""" from __future__ import annotations +from dataclasses import dataclass import json import logging -from dataclasses import dataclass from typing import Any from aiohttp import ClientSession @@ -32,7 +32,6 @@ class ElectricityMaps: async def _get(self, url: str, params: dict[str, Any] | None = None) -> Any: """Execute a GET request against the API.""" - if self.session is None: self.session = ClientSession() self._close_session = True @@ -44,16 +43,18 @@ async def _get(self, url: str, params: dict[str, Any] | None = None) -> Any: try: async with self.session.get( - url, headers=headers, params=params + url, + headers=headers, + params=params, ) as response: parsed = await response.json() except json.JSONDecodeError as exception: raise ElectricityMapsDecodeError( - f"JSON decoding failed: {exception}" + f"JSON decoding failed: {exception}", ) from exception except Exception as exc: raise ElectricityMapsError( - f"Unknown error occurred while fetching data: {exc}" + f"Unknown error occurred while fetching data: {exc}", ) from exc else: _LOGGER.debug( @@ -74,7 +75,7 @@ async def _get(self, url: str, params: dict[str, Any] | None = None) -> Any: # enable legacy mode and let the function recalled by the decorator if not self._is_legacy_token: _LOGGER.debug( - "Detected invalid token on new API, retrying on legacy API." + "Detected invalid token on new API, retrying on legacy API.", ) self._is_legacy_token = True raise SwitchedToLegacyAPI @@ -85,27 +86,33 @@ async def _get(self, url: str, params: dict[str, Any] | None = None) -> Any: @retry_legacy async def latest_carbon_intensity_by_coordinates( - self, lat: str, lon: str + self, + lat: str, + lon: str, ) -> CarbonIntensityResponse: """Get carbon intensity by coordinates.""" if self._is_legacy_token: result = await self._get( - ApiEndpoints.LEGACY_CARBON_INTENSITY, {"lat": lat, "lon": lon} + ApiEndpoints.LEGACY_CARBON_INTENSITY, + {"lat": lat, "lon": lon}, ) else: result = await self._get( - ApiEndpoints.CARBON_INTENSITY, {"lat": lat, "lon": lon} + ApiEndpoints.CARBON_INTENSITY, + {"lat": lat, "lon": lon}, ) return CarbonIntensityResponse.from_dict(result) @retry_legacy async def latest_carbon_intensity_by_country_code( - self, code: str + self, + code: str, ) -> CarbonIntensityResponse: """Get carbon intensity by country code.""" if self._is_legacy_token: result = await self._get( - ApiEndpoints.LEGACY_CARBON_INTENSITY, {"countryCode": code} + ApiEndpoints.LEGACY_CARBON_INTENSITY, + {"countryCode": code}, ) else: result = await self._get(ApiEndpoints.CARBON_INTENSITY, {"zone": code}) diff --git a/aioelectricitymaps/marshmallow.py b/aioelectricitymaps/marshmallow.py index ff769fa..e7617b1 100644 --- a/aioelectricitymaps/marshmallow.py +++ b/aioelectricitymaps/marshmallow.py @@ -16,6 +16,6 @@ class ZoneList(dict[str, Zone], DataClassJsonMixin): mm_field=fields.Dict( keys=fields.String(), values=fields.Nested(Zone.schema()), - ) - ) + ), + ), ) diff --git a/aioelectricitymaps/models.py b/aioelectricitymaps/models.py index 5078447..1b12799 100644 --- a/aioelectricitymaps/models.py +++ b/aioelectricitymaps/models.py @@ -35,5 +35,6 @@ class Zone(DataClassJsonMixin): zone_name: str = field(metadata=config(letter_case=LetterCase.CAMEL)) country_name: str | None = field( - metadata=config(letter_case=LetterCase.CAMEL), default=None + metadata=config(letter_case=LetterCase.CAMEL), + default=None, ) diff --git a/pyproject.toml b/pyproject.toml index e509571..46891f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,12 +31,17 @@ aresponses = "^3.0.0" syrupy = "4.6.0" yamllint = "^1.33.0" -[tool.coverage.run] -plugins = ["covdefaults"] -source = ["aioelectricitymaps"] +[tool.poetry.urls] +"Bug Tracker" = "https://github.com/jpbede/aioelectricitymaps/issues" +Changelog = "https://github.com/jpbede/aioelectricitymaps/releases" [tool.coverage.report] show_missing = true +fail_under = 50 + +[tool.coverage.run] +plugins = ["covdefaults"] +source = ["aioelectricitymaps"] [tool.mypy] # Specify the target platform details in config, so your developers are @@ -60,7 +65,6 @@ disallow_untyped_calls = true disallow_untyped_decorators = true disallow_untyped_defs = true no_implicit_optional = true -no_implicit_reexport = true strict_optional = true warn_incomplete_stub = true warn_no_return = true @@ -70,9 +74,6 @@ warn_unused_configs = true warn_unused_ignores = true [tool.pylint.MASTER] -#extension-pkg-whitelist = [ -# "orjson", -#] ignore = [ "tests", ] @@ -99,6 +100,10 @@ disable = [ "duplicate-code", "format", "unsubscriptable-object", + "too-many-instance-attributes", + "too-many-arguments", + "too-many-public-methods", + "wrong-import-order", ] [tool.pylint.SIMILARITIES] @@ -114,15 +119,15 @@ asyncio_mode = "auto" [tool.ruff] ignore = [ "ANN101", # Self... explanatory + "ANN102", # cls... just as useless "ANN401", # Opinioated warning on disallowing dynamically typed expressions "D203", # Conflicts with other rules "D213", # Conflicts with other rules "D417", # False positives in some occasions "PLR2004", # Just annoying, not really useful - - # Conflicts with the Ruff formatter - "COM812", - "ISC001", + "PLR0913", # Too many arguments + "TCH001", + "TCH003", ] select = ["ALL"] @@ -132,9 +137,9 @@ mark-parentheses = false [tool.ruff.isort] known-first-party = ["aioelectricitymaps"] - -[tool.ruff.lint.flake8-type-checking] -#runtime-evaluated-base-classes = ["mashumaro.mixins.orjson.DataClassORJSONMixin"] +force-sort-within-sections = true +split-on-trailing-comma = false +combine-as-imports = true [tool.ruff.mccabe] max-complexity = 25 diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 1ceab94..0000000 --- a/pytest.ini +++ /dev/null @@ -1,2 +0,0 @@ -[pytest] -addopts = -p no:warnings diff --git a/tests/conftest.py b/tests/conftest.py index 4fa1def..86a6ca4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ -import pytest from aresponses import ResponsesMockServer +import pytest from . import load_fixture diff --git a/tests/test_electricitymaps.py b/tests/test_electricitymaps.py index a1abb47..1c1ca8a 100644 --- a/tests/test_electricitymaps.py +++ b/tests/test_electricitymaps.py @@ -2,8 +2,8 @@ from unittest.mock import patch import aiohttp -import pytest from aresponses import ResponsesMockServer +import pytest from aioelectricitymaps import ElectricityMaps from aioelectricitymaps.exceptions import ( @@ -42,7 +42,8 @@ async def test_carbon_intensity_by_coordinates(mock_response, snapshot) -> None: em = ElectricityMaps(token="abc123", session=session) assert ( await em.latest_carbon_intensity_by_coordinates( - lat="53.1357012", lon="8.2024685" + lat="53.1357012", + lon="8.2024685", ) == snapshot )