FastAPI-icontract
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.)
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).
Metadata
Release files for fastapi-icontract 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi-icontract-0.0.4.tar.gz | 16.1 kB | Without provenance | Details for fastapi-icontract-0.0.4.tar.gz |
Release files / fastapi-icontract-0.0.4.tar.gz
| Download URL | fastapi-icontract-0.0.4.tar.gz |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7ea96598b7e6d53d0045b7e2f26c937434e8ff9e112f22753758394f016b0afe
|
|
BLAKE2b-256 checksum How to use checksums |
ebf76aab0a0ce9831a75db49c2b24a8aa9b210aa9b0d29208e775ed991d05c95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2
|
Security for fastapi-icontract 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.