spy16 / pyschemes

PySchemes is a library for validating data structures in python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

General question.

sathley opened this issue · comments

Hi,
I came across pyschemes recently on HN. My question is where would someone use pyschemes? If it is supposed to be used in testing, how is it better than using asserts. Also is there a possibility of using this for type-checking function arguments?

Testing is certainly not one of the use cases I had in mind while writing this. I had to validate configuration and JSON API payloads for a project I was working on. Function type checking is a good usecase. You could have simple decorator to add type checking for function arguments.

@check_args(int, int)
def sum(x, y):
   return x + y
# in python 3+
@check_args()
def sum(x: int, y: int):
   return x + y

I have the same question: Why pyschemes?

The API looks very close to schema. What advantage does pyscheme provide over existing libraries e.g. schema, marshmallow, etc.?

Yes indeed. The API looks very close to schema because this library was actually inspired by it. I was looking for a simple validation library.. I found schema, it was almost perfect for my use case.. But one thing i found weird was the fact that the following validates without error:

# this is right too, but not for my usecase.. i would want this schema to describe a tuple
# containing two elements.. an int at index 0 and a float at index 1.

Schema((int, float)).validate((5, 7, 8, 10))

And errors seemed to be little cryptic (in my usecase, i wanted to display the error message from the validator directly to the user without doing some message conversion in between.)

But above reasons are pointed out for the sake of giving reasons. Mostly, i just wrote it because i wanted to try it out.. 😄

Thanks for the explanation. It may be worth adding your rationale to the README and giving credit to schema . Always good to give credit where it's due. =)

Agreed !