Skip to main content

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

Project description

License PyPI Version Supported Python Versions Build Status Documentation Status ImportSpy โ€“ Runtime Contract Validation for Python

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, SpyModel Architecture

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:

How ImportSpy Embedded Mode Works
from importspy import Spy
importer = Spy().importspy(filepath="spymodel.yml")
importer.Plugin().run()

CLI Mode โ€“ validate externally in CI/CD:

How ImportSpy CLI Mode Works
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

  1. Define your contract in .yml or Python.

  2. ImportSpy loads your module and introspects its importer.

  3. Runtime environment + structure are matched against the contract.

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

๐ŸŒŸ Contribute & Support

๐Ÿ”ฅ Let your modules enforce their own rules. Start importing with structure.

๐Ÿ“œ License

MIT ยฉ 2024 โ€“ Luca Atella

ImportSpy Logo

ImportSpy is an open-source project maintained with โค๏ธ by Luca Atella.

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.3.3.tar.gz (30.0 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.3.3-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: importspy-0.3.3.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.9 Linux/6.13.12-100.fc40.x86_64

File hashes

Hashes for importspy-0.3.3.tar.gz
Algorithm Hash digest
SHA256 a510a342722f4c7dc21f7800d87a2fbe65ba6c214a66f1b7b8d9cbde4bb091b3
MD5 618c7f123f47195727c0c963dc8bd76e
BLAKE2b-256 a9d0aaf88c5f768a39b9e07f73364d28c15b767153355c267dfb97e8184ac232

See more details on using hashes here.

File details

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

File metadata

  • Download URL: importspy-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.9 Linux/6.13.12-100.fc40.x86_64

File hashes

Hashes for importspy-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bd371a3eb83098426971a9c11a62b5a3eebf455d81f2fd65826e263531d9401b
MD5 30132ba7a0a5af920613e89aefc517d9
BLAKE2b-256 cafc0c7663d07f98672691669b8a80d96c4eee688feb8a0061402de748e1007c

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