Public API to check a declared attribute type and to check if it is mandatory/optional #159
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello everyone,
First of all thanks a lot for this great library ! I am sad I found it only a couple days ago, otherwise I would probably not have spent time writing classtools-autocode. Indeed its main idea was to remove some of the boilerplate and link with PyContracts for validation.
Anyway, today I make this pull request because in another project of mine currently in development mode, parsyfiles, I perform some type introspection to understand what is required to parse an object. I use PEP484 type hints for that, but I wanted to support attrs too so I implemented something that does the job, and now I am proposing it to you.
The proposal is made of two functions : 'guess_type_from_validators', and 'is_mandatory'. Feel free to change the naming, I am not particularly good at finding great names.
For 'is_mandatory': it is simply checking if the validator is an instance of 'optional'.
For 'guess_type_from_validators': type information is extracted from the 'instance_of' validator. In order to help users rely on this validator instead of doing the same type checking in their custom validators, I introduce the 'chain' validator. Note that this might now be useless, since I saw in the source that you allow lists of validators.
In the future if issue 97 is resolved, the 'guess_type_from_validators' method should be updated to also use the new python 3.6 type hints as an additional source of type hint.
I added some unit tests for all the code, and updated the doc in api.rst.
Let me know what you think.