Skip to main content

Lint contracts defined with icontract library.

Project description

pyicontract-lint

Build status Test coverage Documentation status PyPI - version PyPI - Python Version

pyicontract-lint lints contracts in Python code defined with icontract library.

The following checks are performed:

Description

Identifier

File should be read and decoded correctly.

unreadable

A preconditions expects a subset of function’s arguments.

pre-invalid-arg

A snapshot expects at most an argument element of the function’s arguments.

snapshot-invalid-arg

If a snapshot is defined on a function, a postcondition must be defined as well.

snapshot-wo-post

A capture function must be defined in the contract.

snapshot-wo-capture

A postcondition expects a subset of function’s arguments.

post-invalid-arg

If a function returns None, a postcondition should not expect result as argument.

post-result-none

If a postcondition expects result argument, the function should not expect it.

post-result-conflict

If a postcondition expects OLD argument, the function should not expect it.

post-old-conflict

An invariant should only expect self argument.

inv-invalid-arg

A condition must be defined in the contract.

no-condition

File must be valid Python code.

invalid-syntax

Usage

Pyicontract-lint parses the code and tries to infer the imported modules and functions using astroid library. Hence you need to make sure that imported modules are on your PYTHONPATH before you invoke pyicontract-lint.

Once you set up the environment, invoke pyicontract-lint with a list of positional arguments as paths:

pyicontract-lint \
    /path/to/some/directory/some-file.py \
    /path/to/some/directory/another-file.py

You can also invoke it on directories. Pyicontract-lint will recursively search for *.py files (including the subdirectories) and verify the files:

pyicontract-lint \
    /path/to/some/directory

By default, pyicontract-lint outputs the errors in a verbose, human-readable format. If you prefer JSON, supply it --format argument:

pyicontract-lint \
    --format json \
    /path/to/some/directory

If one or more checks fail, the return code will be non-zero. You can specify --dont_panic argument if you want to have a zero return code even though one or more checks failed:

pyicontract-lint \
    --dont_panic \
    /path/to/some/directory

To disable any pyicontract-lint checks on a file, add # pyicontract-lint: disabled on a separate line to the file. This is useful when you recursively lint files in a directory and want to exclude certain files.

Module icontract_lint

The API is provided in the icontract_lint module if you want to use pycontract-lint programmatically.

The main points of entry in icontract_line module are:

  • check_file(...): lint a single file,

  • check_recursively(...): lint a directory and

  • check_paths(...): lint files and directories.

The output is produced by functions output_verbose(...) and output_json(...).

Here is an example code that lints a list of given paths and produces a verbose output:

import pathlib
import sys

import icontract_lint

errors = icontract_lint.check_paths(paths=[
    pathlib.Path('/some/directory/file.py'),
    pathlib.Path('/yet/yet/another/directory'),
    pathlib.Path('/another/directory/another_file.py'),
    pathlib.Path('/yet/another/directory'),
])

output_verbose(errors=errors, stream=sys.stdout)

The full documentation of the module is available on readthedocs.

Installation

  • Install pyicontract-lint with pip:

pip3 install pyicontract-lint

Development

  • Check out the repository.

  • In the repository root, create the virtual environment:

python3 -m venv venv3
  • Activate the virtual environment:

source venv3/bin/activate
  • Install the development dependencies:

pip3 install -e .[dev]
  • We use tox for testing and packaging the distribution. Run:

tox
  • We also provide a set of pre-commit checks that lint and check code for formatting. Run them locally from an activated virtual environment with development dependencies:

./precommit.py
  • The pre-commit script can also automatically format the code:

./precommit.py  --overwrite

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

pyicontract-lint-2.1.1.tar.gz (11.9 kB view details)

Uploaded Source

File details

Details for the file pyicontract-lint-2.1.1.tar.gz.

File metadata

  • Download URL: pyicontract-lint-2.1.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for pyicontract-lint-2.1.1.tar.gz
Algorithm Hash digest
SHA256 755d5e0da86ac01a371518cb73a784c9c69607939f62b0b6c09d1e562570bea0
MD5 143fab2cc7c75ec8ca251a032d988da2
BLAKE2b-256 cce745d9c31bb5deaf04d5d470a16d7b17c92efe31e56b2cc28e00b2d36200b7

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