Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(python): bump python version and requirements #25

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: Setting up tox environment
uses: docker://kiwicom/tox:3.21.2
uses: docker://kiwicom/tox:3.25.0
env:
TOXENV: ${{ matrix.toxenv }}
XDG_CACHE_HOME: /tmp/cache
Expand All @@ -27,7 +27,7 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('requirements.txt') }}
strategy:
matrix:
toxenv: [py37, py38, py39]
toxenv: [py39, py310, py311]

lint:
name: pylint
Expand Down Expand Up @@ -66,17 +66,29 @@ jobs:
pre-commit:
name: Static checks
runs-on: ubuntu-latest
container: kiwicom/pre-commit:2.9.3
container: kiwicom/pre-commit:3.6.0


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

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

- name: Cache
uses: actions/cache@v2
uses: actions/cache@v4
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
- name: fix git
run: git config --system --add safe.directory '*'

- run: mkdir -p .pre-commit-cache
- run: pre-commit install --install-hooks
- run: pre-commit run -a
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
pyver: ["3.6", "3.7", "3.8", "3.9"]
pyver: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
default_language_version:
python: python3
python: python3.11
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 All @@ -24,7 +24,7 @@ repos:
- id: gitlint

- repo: https://github.com/adrienverge/yamllint
rev: v1.26.0
rev: v1.30.0
hooks:
- id: yamllint

Expand All @@ -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
55 changes: 25 additions & 30 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
#
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=./docs-requirements.txt ./docs-requirements.in
#
alabaster==0.7.12
alabaster==0.7.16
# via sphinx
babel==2.10.3
babel==2.16.0
# via sphinx
certifi==2022.6.15
certifi==2024.7.4
# via requests
charset-normalizer==2.0.12
charset-normalizer==3.3.2
# via requests
docutils==0.17.1
docutils==0.20.1
# via
# sphinx
# sphinx-rtd-theme
idna==3.3
idna==3.7
# via requests
imagesize==1.3.0
imagesize==1.4.1
# via sphinx
importlib-metadata==4.12.0
jinja2==3.1.4
# via sphinx
jinja2==3.1.2
# via sphinx
markupsafe==2.1.1
markupsafe==2.1.5
# via jinja2
packaging==21.3
packaging==24.1
# via sphinx
pygments==2.12.0
pygments==2.18.0
# via sphinx
pyparsing==3.0.9
# via packaging
pytz==2022.1
# via babel
requests==2.28.0
requests==2.32.3
# via sphinx
snowballstemmer==2.2.0
# via sphinx
sphinx==5.0.2
sphinx==7.4.7
# via
# -r ./docs-requirements.in
# sphinx-rtd-theme
sphinx-rtd-theme==1.0.0
# sphinxcontrib-jquery
sphinx-rtd-theme==2.0.0
# via -r ./docs-requirements.in
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-applehelp==2.0.0
# via sphinx
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-devhelp==2.0.0
# via sphinx
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-htmlhelp==2.1.0
# via sphinx
sphinxcontrib-jquery==4.1
# via sphinx-rtd-theme
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-qthelp==2.0.0
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
sphinxcontrib-serializinghtml==2.0.0
# via sphinx
urllib3==1.26.9
urllib3==2.2.2
# via requests
zipp==3.8.0
# via importlib-metadata
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.7
python_version = 3.11
ignore_missing_imports = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
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
45 changes: 18 additions & 27 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 @@ -149,17 +152,7 @@ def set_user_agent(self):
# type: () -> None
"""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()
)
string = 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.")
self.user_agent = string
Expand Down Expand Up @@ -273,7 +266,7 @@ def _process(
sleep_before_repeat=None, # type: Optional[float]
tags=None, # type: Optional[list]
raise_for_status=None, # type: Optional[bool]
**request_kwargs # type: Any
**request_kwargs, # type: Any
): # pylint: disable=too-many-statements
# type: (...) -> Optional[requests.Response]
r"""Run a request against a service depending on a request type.
Expand Down Expand Up @@ -366,9 +359,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 +439,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 @@ -491,7 +484,7 @@ def _log_with_params(
status_code=response.status_code,
attempt=attempt,
url=url,
**extra_params
**extra_params,
)

def sleep(self, seconds, request_category, tags):
Expand All @@ -518,12 +511,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 +528,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,17 +577,17 @@ 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,
**extra_params
**extra_params,
)

self.metric_increment(
metric="request",
request_category=request_category,
tags=tags + ["attempt:{}".format(attempt)],
tags=tags + [f"attempt:{attempt}"],
)

@staticmethod
Expand Down
10 changes: 4 additions & 6 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,11 +36,9 @@ 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())]
)
return " ".join([f"{key}={value}" for key, value in sorted(dictionary.items())])


def traced_sleep(trace_name, seconds, ddtrace, tags=None):
Expand Down
Loading
Loading