Package: pyicontract-lint
pyicontract-lintpyicontract-lint lints contracts in Python code defined withicontract library.The following checks are performed:DescriptionIdentifierFile should be read and decoded correctly.unreadableA preconditions expects a subset of function’s arguments.pre-invalid-argA snapshot expects at most an argument element of the function’s arguments.snapshot-invalid-argIf a snapshot is defined on a function, a postcondition must be defined as well.snapshot-wo-postAcapturefunction must be defined in the contract.snapshot-wo-captureA postcondition expects a subset of function’s arguments.post-invalid-argIf a function returns None, a postcondition should not expectresultas argument.post-result-noneIf a postcondition expectsresultargument, the function should not expect it.post-result-conflictIf a postcondition expectsOLDargument, the function should not expect it.post-old-conflictAn invariant should only expectselfargument.inv-invalid-argAconditionmust be defined in the contract.no-conditionFile must be valid Python code.invalid-syntaxUsagePyicontract-lint parses the code and tries to infer the imported modules and functions usingastroid library. Hence you need to make sure that imported modules are on yourPYTHONPATHbefore 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.pyYou can also invoke it on directories. Pyicontract-lint will recursively search for*.pyfiles (including the
subdirectories) and verify the files:pyicontract-lint\/path/to/some/directoryBy default, pyicontract-lint outputs the errors in a verbose, human-readable format. If you prefer JSON, supply it--formatargument:pyicontract-lint\--formatjson\/path/to/some/directoryIf one or more checks fail, the return code will be non-zero. You can specify--dont_panicargument if you want
to have a zero return code even though one or more checks failed:pyicontract-lint\--dont_panic\/path/to/some/directoryTo disable any pyicontract-lint checks on a file, add#pyicontract-lint:disabledon a separate line to the file.
This is useful when you recursively lint files in a directory and want to exclude certain files.Moduleicontract_lintThe API is provided in theicontract_lintmodule if you want to use pycontract-lint programmatically.The main points of entry inicontract_linemodule are:check_file(...): lint a single file,check_recursively(...): lint a directory andcheck_paths(...): lint files and directories.The output is produced by functionsoutput_verbose(...)andoutput_json(...).Here is an example code that lints a list of given paths and produces a verbose output:importpathlibimportsysimporticontract_linterrors=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 onreadthedocs.InstallationInstall pyicontract-lint with pip:pip3installpyicontract-lintDevelopmentCheck out the repository.In the repository root, create the virtual environment:python3-mvenvvenv3Activate the virtual environment:sourcevenv3/bin/activateInstall the development dependencies:pip3install-e.[dev]We use tox for testing and packaging the distribution. Run:toxWe 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.pyThe pre-commit script can also automatically format the code:./precommit.py--overwriteVersioningWe followSemantic Versioning. The version X.Y.Z indicates:X is the major version (backward-incompatible),Y is the minor version (backward-compatible), andZ is the patch version (backward-compatible bug fix).