Skip to content

Commit

Permalink
chore: upgrade static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas03 committed Aug 14, 2024
1 parent 9e1a4e2 commit c876ca8
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 41 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,13 @@ jobs:


steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Cache
uses: actions/cache@v2
with:
path: .pre-commit-cache
key: static-checks-${{ hashFiles('.pre-commit-config.yaml') }}
# git checkout is not creating a working git folder, it has dubious ownership if not configured
# The pre-commit hook will not work on this original git folder
# https://github.com/pre-commit/pre-commit/issues/2125
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_language_version:
exclude: "^.github.*"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
exclude: ^.*\.md$
Expand Down Expand Up @@ -35,7 +35,7 @@ repos:
language_version: system

- repo: https://github.com/pycqa/isort
rev: 5.7.0
rev: 5.13.2
hooks:
- id: isort
additional_dependencies: [".[pyproject]"]
Expand All @@ -46,7 +46,7 @@ repos:
- id: black

- repo: https://github.com/PyCQA/pylint
rev: pylint-2.6.0
rev: v3.2.6
hooks:
- id: pylint
exclude: ^(docs/).*$
Expand Down
3 changes: 0 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ disable =
no-member,
unused-argument,
broad-except,
relative-import,
wrong-import-position,
bare-except,
locally-disabled,
protected-access,
abstract-method,
no-self-use,
fixme,
too-few-public-methods,
bad-continuation,
useless-object-inheritance,
too-many-arguments,
too-many-locals,
Expand Down
1 change: 1 addition & 0 deletions request_session/_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module ensuring lib compatibilty."""

from __future__ import absolute_import

try:
Expand Down
1 change: 1 addition & 0 deletions request_session/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module contains exceptions from requests and request_session."""

from typing import Any # pylint: disable=unused-import

from requests.exceptions import ConnectionError # pylint: disable=redefined-builtin
Expand Down
1 change: 1 addition & 0 deletions request_session/protocols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Simple protocols to duck type dependency injections."""

from typing import Any, List, Optional


Expand Down
37 changes: 15 additions & 22 deletions request_session/request_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Main RequestSession module."""

import re
import time
from collections import namedtuple
Expand Down Expand Up @@ -113,7 +114,9 @@ def __init__(
self.logger = logger
self.log_prefix = log_prefix
self.allowed_log_levels = allowed_log_levels
self.retriable_client_errors = retriable_client_errors if retriable_client_errors else [408]
self.retriable_client_errors = (
retriable_client_errors if retriable_client_errors else [408]
)

self.prepare_new_session()

Expand Down Expand Up @@ -150,15 +153,7 @@ def set_user_agent(self):
"""Set proper user-agent string to header according to RFC22."""
pattern = r"^(?P<service_name>\S.+?)\/(?P<version>\S.+?) \((?P<organization>\S.+?) (?P<environment>\S.+?)\)(?: ?(?P<sys_info>.*))$"
string = (
"{service_name}/{version} ({organization} {environment}) {sys_info}".format(
service_name=self.user_agent_components.service_name, # type: ignore
version=self.user_agent_components.version, # type: ignore
organization=self.user_agent_components.organization, # type: ignore
environment=self.user_agent_components.environment, # type: ignore
sys_info=self.user_agent_components.sys_info # type: ignore
if self.user_agent_components.sys_info # type: ignore
else "",
).strip()
f"{self.user_agent_components.service_name}/{self.user_agent_components.version} ({self.user_agent_components.organization} {self.user_agent_components.environment}) {self.user_agent_components.sys_info if self.user_agent_components.sys_info else ''}".strip()
)
if not re.match(pattern, string):
raise InvalidUserAgentString("Provided User-Agent string is not valid.")
Expand Down Expand Up @@ -366,9 +361,11 @@ def _process(
attempt=run,
)

if self.is_server_error(error, status_code) or self.retry_on_client_errors(status_code):
if self.is_server_error(
error, status_code
) or self.retry_on_client_errors(status_code):
if is_econnreset_error:
self.log("info", "{}.session_replace".format(request_category))
self.log("info", f"{request_category}.session_replace")
self.remove_session()
self.prepare_new_session()

Expand Down Expand Up @@ -444,9 +441,7 @@ def _send_request(self, request_type, request_params, tags, run, request_categor
:param str request_category: Category for log and metric reporting.
:return requests.Response: HTTP Response Object.
"""
metric_name = "{request_category}.response_time".format(
request_category=request_category
)
metric_name = f"{request_category}.response_time"

if not self.statsd:
return self.session.request(method=request_type, **request_params)
Expand Down Expand Up @@ -518,12 +513,10 @@ def metric_increment(self, metric, request_category, tags, attempt=None):
"""
new_tags = list(tags) if tags else []
if attempt:
new_tags.append("attempt:{attempt}".format(attempt=attempt))
new_tags.append(f"attempt:{attempt}")

if self.statsd is not None:
metric_name = "{metric_base}.{metric_type}".format(
metric_base=request_category, metric_type=metric
)
metric_name = f"{request_category}.{metric}"
self.statsd.increment(metric_name, tags=new_tags)

def log(self, level, event, **kwargs):
Expand All @@ -537,7 +530,7 @@ def log(self, level, event, **kwargs):
"""
if not level in self.allowed_log_levels:
raise AttributeError("Provided log level is not allowed.")
event_name = "{prefix}.{event}".format(prefix=self.log_prefix, event=event)
event_name = f"{self.log_prefix}.{event}"
if self.logger is not None:
getattr(self.logger, level)(event_name, **kwargs)

Expand Down Expand Up @@ -586,7 +579,7 @@ def _exception_log_and_metrics(

self.log(
"exception",
"{}.failed".format(request_category),
f"{request_category}.failed",
error_type=error_type,
status_code=status_code,
attempt=attempt,
Expand All @@ -596,7 +589,7 @@ def _exception_log_and_metrics(
self.metric_increment(
metric="request",
request_category=request_category,
tags=tags + ["attempt:{}".format(attempt)],
tags=tags + [f"attempt:{attempt}"],
)

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions request_session/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Utilites used in RequestSession."""
import logging

import sys
import time
from typing import Any, Dict, Iterator, List, Optional, Text
from typing import Any, Dict, List, Optional

from .protocols import Ddtrace

Expand Down Expand Up @@ -36,10 +36,10 @@ def split_tags_and_update(dictionary, tags):


def dict_to_string(dictionary):
# type: (Dict[str, Any]) -> Text
# type: (Dict[str, Any]) -> str
"""Convert dictionary to key=value pairs separated by a space."""
return " ".join(
["{}={}".format(key, value) for key, value in sorted(dictionary.items())]
[f"{key}={value}" for key, value in sorted(dictionary.items())]
)


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
with open("README.md", encoding="utf-8") as f:
readme = f.read()

with open("requirements.in") as f:
with open("requirements.in", encoding="utf-8") as f:
install_requires = [line for line in f if line and line[0] not in "#-"]

with open("test-requirements.in") as f:
with open("test-requirements.in", encoding="utf-8") as f:
tests_require = [line for line in f if line and line[0] not in "#-"]

setup(
Expand Down
1 change: 1 addition & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Just a conftest."""

from typing import Any, Callable

import httpbin as Httpbin
Expand Down
1 change: 1 addition & 0 deletions test/test_request_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the main module."""

import itertools
import sys
from typing import Any, Callable, Dict, Iterator, List, Union
Expand Down
1 change: 1 addition & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the utilities."""

import sys
from typing import Dict, List

Expand Down

0 comments on commit c876ca8

Please sign in to comment.