Skip to main content

ImportSpy ensures structural integrity, runtime compliance, and security for external modules, preventing inconsistencies and enforcing controlled execution.

Project description

ImportSpy

License PyPI Version Docs Python Versions Build Status

Context-aware contract validation for Python imports.
Enforce runtime, environment, and code structure before execution.

ImportSpy banner


🔍 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

SpyModel UML

ImportSpy is powered by a layered introspection model (SpyModel), which captures:

  • Runtime: CPU architecture
  • System: OS and environment
  • Python: interpreter and version
  • Module: 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()

Embedded mode


CLI Mode – external validation in CI

importspy -s spymodel.yml -l DEBUG path/to/module.py

CLI mode


🧠 How It Works

  1. You define an import contract in .yml
  2. At runtime or via CLI, ImportSpy inspects:
    • Who is importing the module
    • What the system/environment looks like
    • What the module structure provides
  3. If validation fails → the import is blocked
  4. If valid → the module runs safely

✅ Tech Stack


📘 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.


🚀 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
ImportSpy logo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

importspy-0.4.2.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

importspy-0.4.2-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

Details for the file importspy-0.4.2.tar.gz.

File metadata

  • Download URL: importspy-0.4.2.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for importspy-0.4.2.tar.gz
Algorithm Hash digest
SHA256 8ef84d7f0f8b0ea43719f5cddc2e47e00039644537564ac264c2200c5acbf961
MD5 7272e94ea132de5c61ec2f051e712e0b
BLAKE2b-256 240c775807a5044f8301891c3bdc64af38628299df69d9241a4f7997585d007d

See more details on using hashes here.

File details

Details for the file importspy-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: importspy-0.4.2-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.5 Darwin/24.5.0

File hashes

Hashes for importspy-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b74ea983ae0e3510be0711529151b8e705f57e5bdfe90b09fb41acb3454a18e
MD5 415be0b32dff383135bb506a479dead5
BLAKE2b-256 e78910c6b55bfc7201b20fe5329e6a793f07171b96dc9e509ba54a7c223ca579

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page