Coder Social home page Coder Social logo

fcd-af / validating-base Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexfayers/validating-base

0.0 0.0 0.0 192 KB

Python Validating Base Class library. Enables runtime validation of argument types using typeguard.

License: MIT License

Shell 8.76% Python 91.24%

validating-base's Introduction

validating-base

A simple Validating Base Class library. Enables runtime validation of argument types using typeguard.

All you need to do is inherit from the ValidatingBaseClass class, type hint (more info) all the methods, and then specify the methods that you want to validate.

See the tests for some better examples of usage.

Installation

To install the project you only need to clone the repo and run pip install within the repo folder:

pip install .

Or you can install directly from GitHub:

pip install git+https://github.com/alexfayers/validating-base

Usage

You use validating-base by importing the base class and inheriting from it.

Below is an example, taken from the project's tests, that shows simple usage.

Anything that inherits from ValidatingBaseClass will need to implement an action method (because this is specified in the required_methods class variable).

This method will have it's argument types validated at runtime using simple type checking (because the method is specified in the validated_methods class variable) (this step is provided by typeguard).

If the first type checking step passes with no exceptions, then the validate_action method will be called. This method validates that the number_list argument is a list of integers.

Finally, the return type of the action method is validated using typeguard.

from validating_base import ValidatingBaseClass

class ActionExample(ValidatingBaseClass):
    """Shows an example usage of the `validating_base.ValidatingBaseClass` class."""

    required_methods: list[str] = ["action"]
    validated_methods: list[str] = ["action"]

    def validate_action(self, number_list: list[int]) -> None:
        """Validate that the data to be processed is in the correct format.

        Args:
            number_list (List[int]): The list of ints

        Raises:
            TypeError: Raised if the data is not the correct type
            ValueError: Raised if the types are correct, but there is an issue in the formatting
        """
        for number in number_list:
            if not isinstance(number, int):
                raise TypeError(f"{number} is not an integer")

Most of the time, an abstract base class will work. But, if you want the runtime validation then this library might be useful.

Documentation

Documentation for validating-base can be found within the docs folder.

validating-base's People

Contributors

alexfayers avatar fcd-af avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.