Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

[REQ] Feedback request: BoolClass vs bool, NoneClass vs None, subclassing all validated schemas? #179

@spacether

Description

@spacether

Is your feature request related to a problem? Please describe.

Request for feedback from users of this tool

Describe the solution you'd like

  1. Would people prefer that boolean data stays bool?
  2. Would people prefer that None data stays None?
  3. When do you want classes generated?
    a. dict (json object) based classes
    b. list (json array) based classes. Note: writing these classes will allow for type hints when accessing values using openapi v3.1.0 prefixItems + acessing a value at a specific index
    c. what about composition(oneOf/anyOf/allOf/not) classes?
    d. simple data types with validation str/number/int?
  4. Is the fact that all payloads are instances of their validated schema useful to you?
  5. Would people prefer that class instances always identify their base type in the IDE? I added that here with will be included in the next major release:
my_model = MyModel(some_str='a')
# my_model is type MyModel[frozendict.frozendict] in the vscode IDE
# my_model.some_str is type SomeStr[str] in the vscode IDE
  1. Do you not like the deep nesting of classes in the Schema_ class? I fixed that in this PR.

Describe alternatives you've considered

I could restructure the tool to keep boolean values as bool, None values as None and only generate schema classes for
json object models. I could also make it so that payloads are instances of one class only.

  • json schema validations would still be run
  • but because the payloads do not subclass all vaidated schemas, validation would be run again if checking if a specific part of a payload is valid to a specific schema. Today validation would NOT need to be rerun because validation results are stored in the generated classes.

Inheriting from a single class would allow enums to be used.

Notes on overloading the __new__ constructor to allow more specific instance types

  • mypy throws an error if None is returned from __new__
  • vscode does not understand None returns from __new__ or __call__
  • so if one wanted None or bool returns, I would need to restructure the generated class and (json) Schema_ information
    • validated payloads could be instantiated using CompnentNameSchema_.validate(arg)
      • this can return None/bool without any issue + a ComponentName class that is a TypedDict or subclasses frozendict.
      • this allows developers access to the Schema_ info, which is stored in the CompnentNameSchema_ class
      • the IDE would know the return type by adding method overloads of the validate method
      • this same pattern could be used in other languages too, like Kotlin etc

The proposal would look like this:

class DictNoneOrBoolDict(typing_extensions.TypedDict):
    foo: int
    bar: str

Class DictNoneOrBoolchema_:
    # openapi schema data
    types = {bool, dict}

    # any needed python data
    python_types = typing.Union[bool, None, DictNoneOrBoolDict]

    @typing.overload
    @classmethod
    def valdate(cls, arg: bool) -> bool:
        ...
    @typing.overload
    @classmethod
    def validate(cls, arg: None) -> None:
        ...
    @typing.overload
    @classmethod
    def validate(cls, arg: DictOrBoolDict) -> DictNoneOrBoolDict:
        ...
    @classmethod
    def validate(cls, arg):
        # validation logic invoked here, probably still convert date/datetime/uuiid into str
        # Not sure about keeping int/float as Decimal
        # If they are kept separate, then int literals could be used for type hints, which would be nice
        return arg

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions