Skip to main content

A metaclass to enforce strict type checking and metadata handling for classes.

Project description

StrictMeta

PyPI Version Python Versions License

A metaclass to enforce strict type checking and metadata handling for classes in Python.

StrictMeta is most beneficial in environments where consistency, predictability, and clear documentation of data types are crucial. It's particularly useful for maintaining API stability across different versions or releases, ensuring that future modifications respect the original design intentions. However, it should be used with caution in performance-critical applications or when flexibility in attribute manipulation is essential.

For projects where strict type enforcement is not critical but clear documentation and maintainability are priorities, a more gradual approach to adding type annotations might be more appropriate.

Table of Contents

Introduction

StrictMeta is a metaclass that enforces strict type checking and metadata handling for classes. It ensures that all attributes in a class have proper type annotations, collects comments and descriptions from the class and its base classes, creates slots based on these annotations, and updates metadata for each annotation by adding or updating Comment objects.

When to Use StrictMeta

  1. Ensuring Type Consistency: If you have a class hierarchy where all subclasses must adhere to specific types for certain attributes, StrictMeta ensures that these type constraints are enforced across the entire inheritance chain. This is particularly useful in large codebases or when collaborating with other developers who might not strictly follow your coding standards.

  2. Documentation and Metadata: For classes where clarity and documentation are crucial (e.g., APIs, configuration classes), StrictMeta can automatically collect comments and descriptions from attributes and provide a clear, documented interface to users of the class. This is particularly useful for maintaining API stability over time as attribute names and types might change but their purpose should remain consistent.

  3. Automated Testing: In automated testing environments, where scripts or frameworks expect specific data types, StrictMeta can help catch type mismatches early in the development cycle, reducing runtime errors that could otherwise lead to bugs or crashes during execution.

  4. Legacy Code Refactoring: For projects looking to refactor legacy code by gradually adding type annotations and metadata handling, StrictMeta provides a structured way to enforce these changes without disrupting existing functionality.

When to Avoid StrictMeta

  1. Performance Overhead: If your project heavily relies on dynamic attribute manipulation or if performance is critical (e.g., in high-frequency trading systems), the overhead of type checking and metadata handling might be unnecessary and could slow down execution. In such cases, a more lightweight approach without strict type enforcement might be preferable.

  2. Flexibility vs. Constraints: If you need maximum flexibility in your class definitions (e.g., allowing attributes to change types frequently), StrictMeta’s rigid enforcement of types and annotations might restrict the design freedom you require. In such scenarios, a more flexible approach without strict type checking would be more appropriate.

  3. Learning Curve: For new developers joining the project, especially those less familiar with Python's typing system or the specific conventions you've adopted for StrictMeta, there could be a learning curve in understanding how to properly annotate classes and what metadata means. This might slow down their productivity initially until they become comfortable with your coding standards.

Installation

To install StrictMeta, you can use pip:

pip install StrictMeta

Usage Examples

Let's assume we have a configuration class for a software application where certain attributes must be of specific types, and these types need to be clearly documented. We can use StrictMeta to ensure that all attributes are properly annotated and commented.

We create an instance of AppConfig and set values to its attributes. If a value is assigned that does not match the expected type, a TypeError is raised, indicating that the attribute was not set correctly according to the type annotation.

from StrictMeta import strict, get_comment

@strict
class AppConfig:
    """
    Configuration class for the software application.
    """
    api_key: str  # API key used for authentication
    max_connections: int = 256  # Maximum number of simultaneous connections allowed
    """ These are the"""
    debug_mode: bool  = False  # Whether debugging is enabled or not

# Example usage
config = AppConfig()
config.api_key = "your_api_key"
try:
    config.max_connections = "not_an_integer"  # This will raise a TypeError
except TypeError as e:
    print(e)  # Output: Cannot set attribute 'max_connections' to value of type <class 'str'>. Expected type is <class 'int'>.

config.max_connections = 64

# You may still retrieve the default connections
max_connections_comment = get_comment(AppConfig, 'max_connections')
print(f"default is {max_connections_comment.default}")  # Output: default is 256

# You may als retrieve the inline comment:
print(f"comment is {max_connections_comment.comment}")  # Output: comment is Maximum number of simultaneous connections allowed

Type Annotations

The attributes api_key, max_connections, and debug_mode are annotated with their respective types (str, int, and bool). These annotations will be enforced by StrictMeta.

Comments and Descriptions

Comments for each attribute are provided in the docstring of the class. These comments will be collected and associated with the type annotations.

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for more information on how to contribute to this project.

License

StrictMeta is available under the GNU Lesser General Public License version 3


Thank you for considering using StrictMeta! We hope it helps you enforce strict type checking and metadata handling in your Python projects.

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

strictmeta-0.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

strictmeta-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file strictmeta-0.1.0.tar.gz.

File metadata

  • Download URL: strictmeta-0.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for strictmeta-0.1.0.tar.gz
Algorithm Hash digest
SHA256 42b58d522586e234b440cc7c50e1e1e73109498128c2ac36051261877b1d114f
MD5 90901c9e55706d442bbf35a260ee3300
BLAKE2b-256 4321b6013c474cac9ef1892c2a66d1b1998b5335b8db35952328bc5b3c7a4e99

See more details on using hashes here.

File details

Details for the file strictmeta-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strictmeta-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for strictmeta-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7adc78388211a08afd75cee27cb599b6af0493a604daa171e6bea98a02c0276
MD5 579f3a36290fda591094a3d6c1ec58e9
BLAKE2b-256 a7f96f983c43251812026114e82b6d554fdefb7d7ee41ae08e5e2844b511fb37

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