ImportSpy ensures structural integrity, runtime compliance, and security for external modules, preventing inconsistencies and enforcing controlled execution.
Project description
ImportSpy
Contract-based import validation for Python modules. Runtime-safe. Structure-aware. Declarative.
ImportSpy allows your Python modules to define explicit import contracts: rules about where, how, and by whom they can be safely imported โ and blocks any import that doesnโt comply.
๐ Key Benefits
โ Prevent import from unsupported environments โ Enforce structural expectations (classes, attributes, arguments) โ Control who can use your module and how โ Reduce runtime surprises across CI, staging, and production โ Define everything in readable .yml contracts
๐ก Why ImportSpy?
Python is flexible, but uncontrolled imports can lead to:
๐ฅ Silent runtime failures
๐ Structural mismatches (wrong or missing methods/classes)
๐ Inconsistent behavior across platforms
๐ซ Unauthorized usage of internal code
ImportSpy offers you runtime import governance โ clearly defined, enforced in real-time.
๐ Architecture Highlight
ImportSpy uses a layered model (SpyModel) that mirrors your execution context and module structure:
Runtime โ defines architecture and system
System โ declares OS and environment variables
Python โ specifies interpreter, version, and modules
Module โ lists classes, functions, variables (each represented as objects, not dicts)
Each element is introspected and validated dynamically, at runtime or via CLI.
๐ Contract Example
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
๐ฆ Installation
pip install importspy
โ Requires Python 3.10+
๐ Usage Modes
Embedded Mode โ the module protects itself:
from importspy import Spy
importer = Spy().importspy(filepath="spymodel.yml")
importer.Plugin().run()
CLI Mode โ validate externally in CI/CD:
importspy -s spymodel.yml -l DEBUG path/to/module.py
๐ Features Overview
โ Runtime validation based on import contracts
โ YAML-based, declarative format
โ Fine-grained introspection of classes, functions, arguments
โ OS, architecture, interpreter matching
โ Full error messages, CI-friendly output
โ Supports embedded or external enforcement
โ Strong internal model (SpyModel) powered by pydantic
๐ Ideal Use Cases
๐ก๏ธ Security-sensitive systems (finance, IoT, medical)
๐งฉ Plugin-based architectures (CMS, CLI, extensions)
๐งช CI/CD pipelines with strict integration rules
๐งฑ Frameworks with third-party extension points
๐ฆ Package maintainers enforcing integration rules
๐ง How It Works
Define your contract in .yml or Python.
ImportSpy loads your module and introspects its importer.
Runtime environment + structure are matched against the contract.
If mismatch โ import blocked. If valid โ import continues safely.
๐ฏ Tech Stack
โ Pydantic 2.x โ contract validation engine
โ Typer โ CLI interface
โ ruamel.yaml โ YAML parsing
โ inspect + sys โ runtime context introspection
โ Poetry โ package + dependency management
โ Sphinx + ReadTheDocs โ full docs and architecture reference
๐ Documentation
๐ Full Docs โ https://importspy.readthedocs.io/
๐งฑ Model Overview โ https://importspy.readthedocs.io/en/latest/advanced/architecture_index.html
๐งช Use Cases โ https://importspy.readthedocs.io/en/latest/overview/use_cases_index.html
๐ Contribute & Support
โญ Star โ https://github.com/atellaluca/ImportSpy
๐ Contribute via issues or PRs
๐ Sponsor โ https://github.com/sponsors/atellaluca
๐ License
MIT ยฉ 2024 โ Luca Atella
๐ฅ Let your modules enforce their own rules. Start importing with structure.
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.3.2.tar.gz.
File metadata
- Download URL: importspy-0.3.2.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.9 Linux/6.13.10-100.fc40.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
baa933eeab3b7e8fe86ad803beb070cff619f338d3a3b7a917eb180f6c0b8fcd
|
|
| MD5 |
e64c2cfd0cf033e66ac88a4167260d68
|
|
| BLAKE2b-256 |
f67066ce24c7b4b51c47a74288f8efc5fe28158a31666191e2374503a417fbef
|
File details
Details for the file importspy-0.3.2-py3-none-any.whl.
File metadata
- Download URL: importspy-0.3.2-py3-none-any.whl
- Upload date:
- Size: 39.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.9 Linux/6.13.10-100.fc40.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f83ba164af8004f4bffb9d643bb538a2dee921098d7a00d03ce982a2e7439021
|
|
| MD5 |
b5d761db5058fb61e42b0e9cca9abe00
|
|
| BLAKE2b-256 |
30973869096b81692fc1514a28d048524b1f57862e70196965709648e85576e1
|