-
Notifications
You must be signed in to change notification settings - Fork 218
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
Add a Maybe validator #232
Add a Maybe validator #232
Conversation
It's quite a standard constructor in functional languages, I think it might be useful here as well since it's a common case for functions to return None in some conditions.. |
1 similar comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please resolve the comments.
@@ -544,6 +544,19 @@ def fn(arg): | |||
fn(1) | |||
|
|||
|
|||
def test_schema_decorator_no_args(): | |||
@validate(int, __return__=Maybe(int)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add test case for Invalid input as well?
@@ -459,6 +459,14 @@ def PathExists(v): | |||
raise PathInvalid("Not a Path") | |||
|
|||
|
|||
class Maybe(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add __repr__
and docstring.
Yes sure @tusharmakkar08 I will solve the issues, just wanted to know if you were happy to add this at all before I made the PR fully finished... |
Yeah, |
Is this any different from Any(T, None)? If not, I'm -1 on this as it adds only very minor convenience. |
AFAIK it's same as Any(T, None) just more verbose. @AndreaCrotti : Please confirm this. |
Well in terms of the result I guess it's the same thing, but:
We can write Maybe using Any if you prefer, but I generally think it would be a nice thing to add.. |
Any news about this @alecthomas ? I'm happy to implement it using Any internally if you prefer and add the missing tests/documentation.. |
Sure, why not. Pretty minor change but if it helps with clarity it is probably a good idea. |
ca06496
to
0f84edd
Compare
1 similar comment
@tusharmakkar08 @alecthomas I've done a few changes, can you let me know what you think now? strangely I wanted to add a test like this:
but for some reason it doesn't work, even if I get exactly that trying from ipython.. |
Also I've added a check to make sure that what you can only pass an actual type. I could actually use the @Validate decorator if you are happy about that. |
It's surprising that |
Very strangely if I try to do this
I get a very strange error
|
I can also leave it like this otherwise if you are happy with it, and add a bit of documentation in case it's all good to go |
Hey @AndreaCrotti I would ideally like to have a test case with the Thanks. |
2fa388a
to
fea2d4d
Compare
1 similar comment
I've done the repr test but it's a bit trivial now, because to support both py2 and py3 and fix this error
I had to just repr(int) inside the test itself |
@tusharmakkar08 @alecthomas what do you think better now? |
@tusharmakkar08 that's not related at all I believe.. |
|
||
s = Schema(Maybe(int)) | ||
assert s(1) == 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is your test for: assert s(None) == None ?
class Maybe(object): | ||
"""Validate that the object is of a given type or is None. | ||
|
||
:raises Invalid: if the value is not of the type declared or None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This contradicts what is written above. None type should not raise an exception.
282eac3
to
2290b04
Compare
Just addressed the comments from @divanovGH |
Hey @AndreaCrotti Please squash the commits. Thanks |
2290b04
to
434a25b
Compare
either of a certain type or None
434a25b
to
aa9c39d
Compare
Simple Maybe validator, which encapsulates the fact that something can be None or of the given type