Skip to main content

Specify contracts for FastAPI endpoints.

Project description

Test on push to main Test coverage PyPI - version PyPI - Python Version

FastAPI-icontract is a FastAPI extension for design-by-contract which leverages icontract to allow you to specify and enforce code contracts in your FastAPI endpoints.

Depending on how you set it up, FastAPI-icontract will:

  • automatically enforce the contracts during testing or in production,

  • automatically add the contracts to your OpenAPI specification, and

  • render the Swagger UI with a specialized contracts plugin for nicer visualization.

Benefits of Adding Contracts to Your API

Enforcing code contracts in your FastAPI development opens up new venues for approaches to more systematic design at the API level:

  • Contracts are an important part of the specification.

    Unlike human language, contracts written in code are unambiguous.

  • Contracts are automatically verifiable.

    Your clients can rest assured that you actually run them. FastAPI-icontract will specify precisely which contracts are run in production and which were only verified during testing.

  • Contracts provide deeper testing.

    If you have a mesh of microservices that you need to test in conjunction, turn on all the contracts and test against your client’s data instead of your own limited unit test data.

  • Contracts specified in code allow for automatic client-side verification.

    Thus you can signal formally to the client up-front what you expect (using pre-conditions), while the client can verify what to expect back from you (using post-conditions).

  • Contracts are not just for input validation.

    Though you can use contracts for input validation as well, FastAPI already allows you to specify how you want your input verified. Contracts, on the other hand, really shine when you want to specify relations between the endpoints.

  • Contracts allow for automatic test generation.

    Tools for property-based testing such as Schemathesis can automatically generate test data and verify that your API works as expected. Post-conditions are an easy way to define your properties to be tested.

    There is an ongoing discussion with the authors of the Schemathesis how to integrate it with tools which generate data based on contracts such as icontract-hypothesis.

  • Contracts open up a wider ecosystem for analysis.

    When you decorate the endpoints with contracts, you can immediately use analysis tools such as CrossHair to analyze your code and find bugs.

    (Though this only makes sense for really stateless, purely functional endpoints.)

Teaser

The full documentation is available at: fastapi-icontract.readthedocs.io.

The following example is meant to invite you to further explore the extension.

Versioning

We follow Semantic Versioning. The version X.Y.Z indicates:

  • X is the major version (backward-incompatible),

  • Y is the minor version (backward-compatible), and

  • Z is the patch version (backward-compatible bug fix).

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

fastapi-icontract-0.0.1.tar.gz (13.6 kB view details)

Uploaded Source

File details

Details for the file fastapi-icontract-0.0.1.tar.gz.

File metadata

  • Download URL: fastapi-icontract-0.0.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

File hashes

Hashes for fastapi-icontract-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0834e1ca969386fabd7a8754aa4c30bd17a53f2efbfa45c3e2858b16b63a878e
MD5 c1533d9d54db1987cdae4faa5fe58a6a
BLAKE2b-256 9128685d7be8b1c7b8a3f1a50190d7fc43802fd981b985aac88a65a811c96350

See more details on using hashes here.

Supported by

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