jd / tenacity

Retrying library for Python
http://tenacity.readthedocs.io
Apache License 2.0
6.82k stars 283 forks source link

How to union two `retry_if`? #422

Closed jamesbraza closed 1 year ago

jamesbraza commented 1 year ago

The docs talk about the pipe | operator to apply 2+ conditions with "or" (union) logic: https://tenacity.readthedocs.io/en/latest/index.html#stopping

Is there some way to do & for "and" (intersection) logic?

jamesbraza commented 1 year ago

Actually, I believe & works with tenacity==8.2.3:

import requests
from tenacity import (
    retry,
    retry_if_exception_message,
    retry_if_exception_type,
    stop_after_attempt,
)

@retry(
    retry=(
        retry_if_exception_type(requests.exceptions.HTTPError)
        & retry_if_exception_message("Gateway Time-out")
    ),
    stop=stop_after_attempt(3),
)
def run_with_retry() -> None:
    print("Attempt")
    # raise ValueError("Apples")
    # raise requests.exceptions.HTTPError("Apples")
    raise requests.exceptions.HTTPError("Gateway Time-out")

run_with_retry()

Play around with that