Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve robustness of PyAutoscoper introducing validator #171

Open
jcfr opened this issue Jul 26, 2023 · 0 comments · May be fixed by #183
Open

Improve robustness of PyAutoscoper introducing validator #171

jcfr opened this issue Jul 26, 2023 · 0 comments · May be fixed by #183
Assignees
Labels
enhancement New feature or request

Comments

@jcfr
Copy link
Contributor

jcfr commented Jul 26, 2023

To systematically validate input and address issue like one originally described in the following issue, type of all parameter should be validated.

A possible approach would be to add validator like:

  • StrictInt
  • StrictFloat
  • StrictBool

Naming was adapted from this discussion1 related to pydantic2

from abc import ABC, abstractmethod


class Validator(ABC):

    def __set_name__(self, owner, name):
        self.private_name = '_' + name

    def __get__(self, obj, objtype=None):
        return getattr(obj, self.private_name)

    def __set__(self, obj, value):
        self.validate(value)
        setattr(obj, self.private_name, value)

    @abstractmethod
    def validate(self, value):
        pass


class Number(Validator):

    def __init__(self, minvalue=None, maxvalue=None, types=None):
        self.minvalue = minvalue
        self.maxvalue = maxvalue
        self.types = (int, float) if types is None else types

    def validate(self, value):
        if not isinstance(value, self.types):
            raise TypeError(f'Expected {value!r} to be {",",join(self.types)}')
        if self.minvalue is not None and value < self.minvalue:
            raise ValueError(
                f'Expected {value!r} to be at least {self.minvalue!r}'
            )
        if self.maxvalue is not None and value > self.maxvalue:
            raise ValueError(
                f'Expected {value!r} to be no more than {self.maxvalue!r}'
            )

class Float(Number):

    def __init__(self, minvalue=None, maxvalue=None)
        super.__init__(minvalue=minvalue, maxvalue=maxvalue, types=(float,)

class Integer(Number):

    def __init__(self, minvalue=None, maxvalue=None)
        super.__init__(minvalue=minvalue, maxvalue=maxvalue, types=(int,)

References:

Other informative resources:

Footnotes

  1. https://github.com/tiangolo/fastapi/discussions/8024

  2. https://github.com/pydantic/pydantic/issues/578

@NicerNewerCar NicerNewerCar added the enhancement New feature or request label Jul 27, 2023
@NicerNewerCar NicerNewerCar self-assigned this Jul 27, 2023
@NicerNewerCar NicerNewerCar linked a pull request Aug 2, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants