module_load

This function allows you to control what can happen at module load stage.

Usage:

  1. Call deal.activate() before importing anything.
  2. Call deal.module_load() in any place at module level in all modules that should be tested. Pass inside all contracts that should be controlled. By design, only contracts from deal without arguments are supported.

Example

__init__.py:

import deal

deal.activate()

from .other import something

other.py:

import deal
import something_else

deal.module_load(deal.silent, deal.safe, deal.offline)

something = 1
print(1)  # contract violation! deal.SilentContractError will be raised

How it works

  1. Calling deal.activate registers import finder and loader. From now, all imported files will be checked by deal.
  2. The loader reads imported file, generates AST for it, and looks for deal.module_load calling.
  3. If loader found deal.module_load in the module, it extracts contracts from it.
  4. If all contracts are valid (imported from deal and have no arguments), loader loads the module with contracts activated.

Motivation

This contract is inspired by article Python at Scale: Strict Modules. A module loading should be fast, pure, and safe. This function allows to enforce it.