Skip to main content

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

Project description

PyPI Version Supported Python Versions Build Status Documentation Status License GitHub Stars

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.

How ImportSpy Works

๐Ÿ” 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:

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:

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

๐Ÿ“œ License

MIT ยฉ 2024 โ€“ Luca Atella

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

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.2.tar.gz (29.8 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.2-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

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

Hashes for importspy-0.3.2.tar.gz
Algorithm Hash digest
SHA256 baa933eeab3b7e8fe86ad803beb070cff619f338d3a3b7a917eb180f6c0b8fcd
MD5 e64c2cfd0cf033e66ac88a4167260d68
BLAKE2b-256 f67066ce24c7b4b51c47a74288f8efc5fe28158a31666191e2374503a417fbef

See more details on using hashes here.

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

Hashes for importspy-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f83ba164af8004f4bffb9d643bb538a2dee921098d7a00d03ce982a2e7439021
MD5 b5d761db5058fb61e42b0e9cca9abe00
BLAKE2b-256 30973869096b81692fc1514a28d048524b1f57862e70196965709648e85576e1

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