Context-aware contract validation for Python imports.
Enforce runtime, environment, and code structure before execution.
ImportSpy lets your Python modules declare structured import contracts (via .yml
files) to define:
- What environment they expect (OS, Python version, interpreter)
- What structure they must follow (classes, methods, variables)
- Who is allowed to import them
If the contract is not met, ImportSpy blocks the import β ensuring safe and predictable runtime behavior.
- β Validate imports dynamically at runtime or via CLI
- β Block incompatible usage of internal or critical modules
- β Enforce module structure, arguments, annotations
- β Context-aware: Python version, OS, architecture, interpreter
- β Human-readable YAML contracts
- β Clear, CI-friendly violation messages
pip install importspy
Requires Python 3.10+
ImportSpy is powered by a layered introspection model (SpyModel
), which captures:
Runtime
: CPU architectureSystem
: OS and environmentPython
: interpreter and versionModule
: classes, functions, variables, annotations
Each layer is validated against the corresponding section of your .yml
contract.
filename: plugin.py
variables:
- name: mode
value: production
annotation: str
classes:
- name: Plugin
methods:
- name: run
arguments:
- name: self
- name: data
annotation: dict
return_annotation: None
from importspy import Spy
caller = Spy().importspy(filepath="spymodel.yml")
caller.Plugin().run()
importspy -s spymodel.yml -l DEBUG path/to/module.py
- You define an import contract in
.yml
- At runtime or via CLI, ImportSpy inspects:
- Who is importing the module
- What the system/environment looks like
- What the module structure provides
- If validation fails β the import is blocked
- If valid β the module runs safely
- Pydantic 2.x β schema validation
- Typer β CLI
- ruamel.yaml β YAML support
inspect
+sys
β runtime introspection- Poetry β dependency management
- Sphinx + ReadTheDocs β documentation
Full documentation is available at:
π https://atellaluca.github.io/ImportSpy/
Here are some useful entry points:
-
π§ How ImportSpy works
A high-level overview of the validation lifecycle, contract structure, and runtime behavior. -
βοΈ SpyModel architecture
Deep dive into the declarative model that describes runtime, environment, and module contracts. -
π§ͺ Violation system
Learn how ImportSpy reports context-aware, structured errors on invalid imports. -
π CLI usage
Run contract validation in CI/CD pipelines or pre-deploy checks with the CLI interface. -
π Embedded usage
Use ImportSpy directly inside a module to protect it from being imported in unsupported contexts. -
π Writing import contracts
Guide to authoring.yml
contracts: syntax, best practices, and validation patterns.
- Plugin-based frameworks (e.g., CMS, CLI, IDE)
- CI/CD pipelines with strict integration
- Security-regulated environments (IoT, medical, fintech)
- Package maintainers enforcing internal boundaries
Pythonβs flexibility comes at a cost:
- Silent runtime mismatches
- Missing methods or classes
- Platform-dependent failures
- No enforcement over module consumers
ImportSpy brings governance
to how, when, and where modules are imported.
- β Star on GitHub
- π File issues or feature requests
- π€ Contribute
- π Sponsor on GitHub