Skip to main content

A library for creating fluent, readable, and composable checks for your tests or application logic.

Project description

Fluent Checks

PyPI version License: MIT

A Python library for creating readable, composable, and chainable conditions for tests or application logic.

Installation

pip install fluent-checks

Quickstart

fluent-checks helps you turn complex, imperative conditional logic into a clean, fluent API.

Instead of this:

# Imperative style
start_time = time.time()
file_created = False
while time.time() - start_time < 5:
    if os.path.exists("my_file.txt"):
        file_created = True
        break
if not file_created:
    raise AssertionError("File not created in 5 seconds")

You can write this:

from fluent_checks import Check
import os

# Fluent style
file_exists = Check(lambda: os.path.exists("my_file.txt"))

# This line will block until the file exists or raise a TimeoutException after 5s
file_exists.with_timeout(5).wait_for()

Core Features

1. Combine Checks

Use standard logical operators to combine checks.

from fluent_checks import Check

is_ready = Check(lambda: service.status == "ready")
is_healthy = Check(lambda: service.health > 0.9)

# This will block until the service is both ready AND healthy
(is_ready & is_healthy).wait_for()

2. Add Modifiers

Chain methods to add conditions like timeouts, retries, or exception checks.

  • Time-Based:

    • .with_timeout(seconds): Fails if the check doesn't pass within a time limit.
    • .with_delay(seconds): Waits a fixed duration before checking.
    • .with_deadline(datetime): Fails if the check doesn't pass by a specific time.
  • Repetition:

    • .succeeds_within(attempts): Checks multiple times, succeeding if it passes at least once.
    • .is_consistent_for(attempts): Succeeds only if the check passes on every attempt.
  • Exception Handling:

    • .raises(ExceptionType): Succeeds if the wrapped function raises the specified exception.

Example with multiple modifiers:

import random

# A check for a flaky API that should eventually return True
flaky_api_call = Check(lambda: random.choice([True, False]))

# Succeeds if the API returns True within 3 tries, waiting 0.5s between each try.
check = flaky_api_call.with_delay(0.5).succeeds_within(3)

if check:
    print("API call succeeded!")

Contributing

Contributions are welcome! Please open an issue to discuss your ideas.

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fluent_checks-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fluent_checks-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file fluent_checks-0.1.1.tar.gz.

File metadata

  • Download URL: fluent_checks-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for fluent_checks-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e50098976d46d25568a06e644f6bf0b81286f4c71eec3e465fac6c1a9e79b7fe
MD5 f390c85669de91858ea05e59a4c2834f
BLAKE2b-256 a4f165b3cb17d6a86358477c2f093bc0a6916fbb79b3ab9da195777f6ebdc3b1

See more details on using hashes here.

File details

Details for the file fluent_checks-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fluent_checks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e73c9eb5c394a24549b11af0abea3030c01eb462cfd9f4b807bc9d53d26f42c9
MD5 3a5add39c0869f922fdbd324bf411f70
BLAKE2b-256 2614f21587e9aaa30204d0629b5de498b11d8edd992c84273611a78319b76c36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page