From a076a4867a838008a46b7605502b53e21e3c91c9 Mon Sep 17 00:00:00 2001 From: Jon Grace-Cox Date: Mon, 30 Dec 2024 06:43:37 -0500 Subject: [PATCH] chore: Update pre-commit hooks Update pre-commit hooks to latest version and apply fixes for mypy type checking and linting. --- .pre-commit-config.yaml | 8 +++---- anybadge/badge.py | 41 +++++++++++++++++++++------------- anybadge/server/cli.py | 15 ++++++++++--- anybadge/templates/__init__.py | 1 + build-requirements.txt | 4 +++- tasks/__init__.py | 1 + 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7bae051..d0ef4bc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -11,11 +11,11 @@ repos: - id: no-commit-to-branch args: [--branch, master] - repo: https://github.com/psf/black - rev: 22.6.0 + rev: 24.10.0 hooks: - id: black - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.971 + rev: v1.14.0 hooks: - id: mypy - additional_dependencies: [types-all] + additional_dependencies: [types-requests] diff --git a/anybadge/badge.py b/anybadge/badge.py index 3e992e1..a8b6b54 100644 --- a/anybadge/badge.py +++ b/anybadge/badge.py @@ -107,21 +107,21 @@ class Badge: self, label, value, - font_name: str = None, - font_size: int = None, - num_padding_chars: int = None, - num_label_padding_chars: float = None, - num_value_padding_chars: float = None, - template: Union[Path, str] = None, - style: str = None, - value_prefix: str = "", - value_suffix: str = "", + font_name: Optional[str] = None, + font_size: Optional[int] = None, + num_padding_chars: Optional[int] = None, + num_label_padding_chars: Optional[float] = None, + num_value_padding_chars: Optional[float] = None, + template: Optional[Union[Path, str]] = None, + style: Optional[str] = None, + value_prefix: Optional[str] = "", + value_suffix: Optional[str] = "", thresholds: Optional[Dict[float, str]] = None, - default_color: str = None, - use_max_when_value_exceeds: bool = True, - value_format: str = None, - text_color: str = None, - semver: bool = False, + default_color: Optional[str] = None, + use_max_when_value_exceeds: Optional[bool] = True, + value_format: Optional[str] = None, + text_color: Optional[str] = None, + semver: Optional[bool] = False, ): """Constructor for Badge class.""" # Set defaults if values were not passed @@ -162,7 +162,18 @@ class Badge: value_text = str(self.value_type(value)) self.value_prefix = value_prefix self.value_suffix = value_suffix - self.value_text = value_prefix + value_text + value_suffix + + # Combine prefix, value and suffix into a single value_text string + + if value_prefix: + self.value_text = value_prefix + else: + self.value_text = "" + + self.value_text += value_text + + if value_suffix: + self.value_text += value_suffix if font_name not in config.FONT_WIDTHS: raise ValueError( diff --git a/anybadge/server/cli.py b/anybadge/server/cli.py index 4d31fc4..a3ce03f 100644 --- a/anybadge/server/cli.py +++ b/anybadge/server/cli.py @@ -2,6 +2,7 @@ import argparse import logging from http.server import HTTPServer from os import environ +from typing import Optional, Tuple from anybadge.server.request_handler import AnyBadgeHTTPRequestHandler from anybadge.server import config @@ -9,7 +10,7 @@ from anybadge.server import config logger = logging.getLogger(__name__) -def run(listen_address: str = None, port: int = None): +def run(listen_address: Optional[str] = None, port: Optional[int] = None): """Run a persistent webserver.""" if not listen_address: listen_address = config.DEFAULT_SERVER_LISTEN_ADDRESS @@ -54,12 +55,20 @@ def parse_args() -> argparse.Namespace: return parser.parse_args() -def main(): +def main() -> None: """Run server.""" # Check for environment variables if "ANYBADGE_PORT" in environ: - config.DEFAULT_SERVER_PORT = environ["ANYBADGE_PORT"] + port = environ["ANYBADGE_PORT"] + try: + port_int = int(port) + except ValueError: + logger.error( + "ANYBADGE_PORT environment variable must be an integer. Got %s", port + ) + raise + config.DEFAULT_SERVER_PORT = port_int if "ANYBADGE_LISTEN_ADDRESS" in environ: config.DEFAULT_SERVER_LISTEN_ADDRESS = environ["ANYBADGE_LISTEN_ADDRESS"] diff --git a/anybadge/templates/__init__.py b/anybadge/templates/__init__.py index 38e45aa..f7c8a24 100644 --- a/anybadge/templates/__init__.py +++ b/anybadge/templates/__init__.py @@ -1,4 +1,5 @@ """Templates package.""" + import pkgutil from anybadge.exceptions import UnknownBadgeTemplate diff --git a/build-requirements.txt b/build-requirements.txt index aa26b53..2c5f7dc 100644 --- a/build-requirements.txt +++ b/build-requirements.txt @@ -4,4 +4,6 @@ pygments pytest pytest-cov requests -types-all +setuptools +types-requests +wheel diff --git a/tasks/__init__.py b/tasks/__init__.py index 4a610a0..2f036e5 100644 --- a/tasks/__init__.py +++ b/tasks/__init__.py @@ -1,4 +1,5 @@ """Invoke tasks for the project.""" + import glob import os import subprocess