Skip to content

Commit

Permalink
fix: Reset error and response in the retry context (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmateusz committed Mar 26, 2024
1 parent f520b96 commit ac11130
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 277 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ jobs:

- name: Install dependencies
if: steps.cache-venv.outputs.cache-hit != 'true'
run: poetry run pip install pre-commit virtualenv
run: |
poetry install --all-extras --no-root --with=test --with=pydantic --with=dev
poetry run mypy --install-types --non-interactive src/**
- name: Run mypy
run: poetry run mypy src/**

- name: Run pre-commit
run: poetry run pre-commit run --all
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:

- name: Install dependencies
if: steps.cache-venv.outputs.cache-hit != 'true'
run: poetry install --no-interaction --all-extras --no-root --with=test --with=pydantic
run: poetry install --no-interaction --all-extras --no-root --with=test --with=pydantic --with=dev

- name: Install project
run: poetry install --no-interaction --all-extras
Expand Down
47 changes: 0 additions & 47 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,6 @@ repos:
- id: check-toml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
verbose: true
entry: mypy
args:
[
--warn-unused-configs,
--disallow-any-unimported,
#--disallow-any-expr,
#--disallow-any-decorated,
#--disallow-any-explicit,
--disallow-any-generics,
#--disallow-subclassing-any,
--disallow-untyped-calls,
--disallow-untyped-defs,
--disallow-incomplete-defs,
--check-untyped-defs,
--disallow-untyped-decorators,
--warn-redundant-casts,
--warn-unused-ignores,
--no-warn-no-return,
#--warn-return-any,
--warn-unreachable,
--local-partial-types,
--no-implicit-reexport,
--strict-equality,
#--strict,
--allow-empty-bodies,
--show-error-context,
--pretty,
--explicit-package-bases,
--soft-error-limit=10,
--cache-dir=./.mypy,
--config-file=pyproject.toml
]
language: python
additional_dependencies:
- cryptography
- pydantic
- pydantic-settings
- aiohttp
- pytest
- pytest_asyncio
- types-requests
- httpx
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
hooks:
Expand Down
510 changes: 286 additions & 224 deletions poetry.lock

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tool.poetry]
name = "meatie"
version = "0.1.1"
description = "Meatie is a Python metaprogramming library that eliminates the need for boilerplate code when integrating with REST APIs. The library generates code for calling a REST API based on method signatures annotated with type hints. Ribeye abstracts away mechanics related to HTTP communication, such as building URLs, encoding query parameters, parsing, and dumping Pydantic models. With some modest additional configuration effort, generated HTTP clients offer rate limiting, retries, and caching."
description = "Meatie is a Python typed REST client library that eliminates the need for boilerplate code when integrating with external APIs. The library generates code for calling a REST API based on method signatures annotated with type hints. Ribeye abstracts away mechanics related to HTTP communication, such as building URLs, encoding query parameters, parsing, and dumping Pydantic models. With some modest additional configuration effort, generated HTTP clients offer rate limiting, retries, and caching."
authors = ["pmateusz <[email protected]>"]
readme = "README.md"
keywords = ["http-client", "metaprogramming", "REST", "HTTP"]
keywords = ["http-client", "metaprogramming", "REST", "HTTP", "API", "requests", "httpx", "aiohttp", "pydantic", "type-hints"]
homepage = "https://github.com/pmateusz/meatie"
repository = "https://github.com/pmateusz/meatie"
license = "BSD-3-Clause"
Expand Down Expand Up @@ -36,10 +36,11 @@ pytest = ">=7.4.3"
pytest-asyncio = ">=0.23.2"
cryptography = ">=42.0.2"

[tool.poetry.group.pre-commit]
[tool.poetry.group.dev]
optional = true

[tool.poetry.group.pre-commit.dependencies]
[tool.poetry.group.dev.dependencies]
mypy = ">=1.9.0"
pre-commit = ">=3.6.0"
virtualenv = ">=20.25.0"

Expand All @@ -55,8 +56,29 @@ log_cli = true
pythonpath = ["src", "tests/shared"]
addopts = ["--import-mode=importlib"]


[tool.mypy]
mypy_path = ["$MYPY_CONFIG_FILE_DIR/src", "$MYPY_CONFIG_FILE_DIR/tests/shared"]
cache_dir = "./.mypy"
pretty = true
allow_empty_bodies = true
check_untyped_defs = true
disallow_any_generics = true
disallow_any_unimported = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
disallow_untyped_decorators = true
disallow_untyped_calls = true
explicit_package_bases = true
local_partial_types = true
no_warn_no_return = true
no_implicit_reexport = true
show_error_context = true
strict_equality = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true

[tool.coverage.run]
branch = true
Expand Down
2 changes: 2 additions & 0 deletions src/meatie/option/retry_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def __call__(self, operation_ctx: Context[T]) -> T:
last_result: Optional[T] = None
stopped = False
while not stopped:
retry_ctx.error = None
retry_ctx.response = None
if retry_ctx.attempt_number > 1:
wait_time = self.__wait(retry_ctx)
if wait_time > 0.0:
Expand Down
35 changes: 35 additions & 0 deletions tests/option/test_retry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 The Meatie Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
from unittest.mock import Mock

from meatie import Context, has_exception_type, never, zero
from meatie.option.retry_option import RetryOperator


def test_exception_is_reset() -> None:
# GIVEN
successful_response = Mock()
operator = RetryOperator(
on=has_exception_type(RuntimeError), wait=zero, stop=never, sleep_func=Mock()
)
ctx = Mock(spec=Context)
call_count = 0

def on_proceed() -> Mock:
nonlocal call_count

call_count += 1
if call_count == 1:
raise RuntimeError()

ctx.response = successful_response
return ctx.response

ctx.proceed = Mock(side_effect=on_proceed)

# WHEN
response = operator(ctx)

# THEN
assert ctx.proceed.call_count == 2
assert response is successful_response

0 comments on commit ac11130

Please sign in to comment.