Show HN: Automatically Validate Python Packages

(pypi.org)

2 points | by EdwardK1 9 hours ago ago

4 comments

  • EdwardK1 6 hours ago

    Clear Examples;

    # Single File:

    from decimal import Decimal

    from validatedata import autovalidate

    # custom type checker for decimals

    def is_decimal(v): return isinstance(v, Decimal)

    # add at the bottom of a file you want to auto-validate autovalidate(

        module="my_project.my_module",
    
        type_checkers={Decimal: is_decimal},   # custom checkers
    
        raise_exceptions=True,                 # raise on failure
    
        enforce_hints=False,                   # require type hints on all functions
    
        # decorator=my_custom_decorator,       # optional: use your own decorator
    
    )

    # Entire Package:

    from validatedata import autovalidate_package

    report = autovalidate_package(

        package="my_project",
    
        include=[ "my_project.*" ],   # only modules under my_project
    
        exclude=[ "my_project.tests.*" ],  # skip tests
    
        # dry_run=True,
    
    )

    # Uncomment dry_run=True plus the print below to see what would be auto-validated.

    # print(report["decorated"])

  • EdwardK1 6 hours ago

    Benchmarks (3 million repetitions)

    | Test | validatedata (validator) | msgspec | pydantic | fastjsonschema |

    |---------------|---------------------|-------------|---------|----------|

    | Dict (valid) | 5.6091s | 2.6221s | 11.9899s | 14.6283s|

    | Dict (invalid) | 0.9845s | 3.5143s | 13.9074s | 9.3520s |

  • EdwardK1 9 hours ago

    from decimal import Decimal from validatedata import autovalidate

    # custom type checker for decimals def is_decimal(v): return isinstance(v, Decimal)

    # add at the bottom of a file you want to auto-validate autovalidate( module="my_project.my_module", type_checkers={Decimal: is_decimal}, # custom checkers raise_exceptions=True, # raise on failure enforce_hints=False, # require type hints on all functions # decorator=my_custom_decorator, # optional: use your own decorator )

    You can also use the autovalidate_package function to automatically validate the entire package. The decorator parameter enables you to swap the inbuilt decorator with one from another library or your own.If enforce_hints is set, exceptions will be raised once untyped functions are detected

    pip install validatedata validatedata 0.6.1

  • EdwardK1 9 hours ago

    [dead]