ImportSpy ensures structural integrity, runtime compliance, and security for external modules, preventing inconsistencies and enforcing controlled execution.
Project description
ImportSpy
Context-aware contract validation for Python imports.
Enforce runtime, environment, and code structure before execution.
🔍 What is ImportSpy?
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.
✨ Key Features
- ✅ 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
📦 Installation
pip install importspy
Requires Python 3.10+
📐 Architecture
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.
📜 Example 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
🔧 Modes of Use
Embedded Mode – protect your own module
from importspy import Spy
caller = Spy().importspy(filepath="spymodel.yml")
caller.Plugin().run()
CLI Mode – external validation in CI
importspy -s spymodel.yml -l DEBUG path/to/module.py
🧠 How It Works
- 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
✅ Tech Stack
- Pydantic 2.x – schema validation
- Typer – CLI
- ruamel.yaml – YAML support
inspect+sys– runtime introspection- Poetry – dependency management
- Sphinx + ReadTheDocs – documentation
📘 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.ymlcontracts: syntax, best practices, and validation patterns.
🚀 Ideal Use Cases
- 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
💡 Why It Matters
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.
❤️ Contribute & Support
📜 License
MIT © 2024 – Luca Atella
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file importspy-0.4.5.tar.gz.
File metadata
- Download URL: importspy-0.4.5.tar.gz
- Upload date:
- Size: 26.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.7 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7357c1c0e30af394558475d6cabe28b4f31235b8898b43bebdb88929c0275f54
|
|
| MD5 |
f9302f5d0c997544c9d74e63e26d7291
|
|
| BLAKE2b-256 |
323d49f0e2e60ad5310043ec6b4f2bf676623d5a68dfe1d86bfe06cf9b0224b7
|
File details
Details for the file importspy-0.4.5-py3-none-any.whl.
File metadata
- Download URL: importspy-0.4.5-py3-none-any.whl
- Upload date:
- Size: 30.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.7 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ed3ea9e6341f9e8eafc9bf67d9d28c8b357a9160dbe46db83ba376d998b19d7
|
|
| MD5 |
f65a96f371ecc8bc29172a7e60b84fd7
|
|
| BLAKE2b-256 |
ea2e895f548e2e92820ccb255117e69899d28b18da45da3e81afebdd41d73a1c
|