-
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 Schema.infer method #311
add Schema.infer method #311
Conversation
This introduces the class method Schema.infer, to infer a Schema from concrete data. This will be useful for converting existing known-good data (e.g. API responses) into enforceable schemas.
I have written code like this for my own projects and found it useful. I figured it might be nice to add it to the library itself. Forgive me if this functionality already exists. I couldn't find it! |
@dtao aside from the possible discussion of whether this logic should be part of this library, I wonder whether it should take multiple samples to infer the schema? Because it may be that a schema "should be" a list of multiple values (e.g., |
I'm open to changing things, but from @svisser's comment I think we might not be on the same page re: what this method does (or should do). This is certainly at least partially my fault for not being more thorough in the PR description (and perhaps in the docstring as well). The way the method works in the current implementation isn't that So: s = Schema.infer({
'int': 42,
'str': 'foo',
'bool': True
})
s({'int': 27, 'str': 'bar', 'bool': False}) # valid I don't personally think it's necessary (or even feasible) for this method to provide a comprehensive way of inferring every feature already supported by the |
I think it's fine to merge this, but if you could add something like the following to the docstring: "Note: only very basic inference is supported". |
@alecthomas I added the note that you suggested. I thought about being a bit more verbose and explaining more, but hopefully the example in the docstring with the note together will do most of the work to manage expectations. |
1 similar comment
Yeah, that's great, thanks :) |
This introduces the class method Schema.infer, to infer a Schema from
concrete data. This will be useful for converting existing known-good
data (e.g. API responses) into enforceable schemas.