-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Refactor shallow schema #1350
Merged
diego-plan9
merged 11 commits into
Qiskit:master
from
delapuente:feature/refactor-shallow-schema
Dec 5, 2018
Merged
Refactor shallow schema #1350
diego-plan9
merged 11 commits into
Qiskit:master
from
delapuente:feature/refactor-shallow-schema
Dec 5, 2018
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56e0dc9
to
5c62445
Compare
Overview ======== This PR introduces a kind of _mixin field_ `ModelValidation` with model type validation. All the Marshmallow fields are imported into `qiskit.validation.fields` and extended to include the mixin. The same happens with polymorphic fields and containers. Validation is possible thanks to a special copy of the model schema, the _shallow schema_, which replaces the `_deserialize` private method of its fields (at instance-level) to use the new `validate_model` from the `ModelValidator` mixin. Validating models consist into checking that the values are of the corresponding types. Specifically: 1. For regular fields, the type they deserialize to (deserialization type). 2. For fields containers (such as lists), the deserialization type of the contained field. 3. For schema containers (such as nested schemas), the type of the schema's model. 4. For polymorphic fields, one of the deserialization types of the choices. 5. For polymorphic schemas, one of the types of the possible schemas' model. While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider: ```python Book(title="The Terror", author=Person(name="Dan Simmons")) ``` `Person` instantiation is executed first. If successful, validating `Book` does not require `author`'s internal structure (i.e. `name`) to be validated again since otherwise, construction of `Person` would have failed. --- This refactor continues patching the method `_deserialize` of the schema fields but, instead of deciding how to patch it according to the type, it makes `_deserialize` to call a new custom method `_validate_model` with the desired behaviour. The different strategies from the if/elif/else block of the original implementation are now spread over the fields defined in qiskit.validation.fields This package also contains versions of marshmallow.fields that include their own `_validate_model` method.
This PR split the fields into general fields, containers and polymorphic. Each module has the definition of the different kind of fields. It also provides a base field for those able of model type validation and a general mechanism to perform this validation.
5c62445
to
6647574
Compare
6647574
to
bc0e6e5
Compare
atilag
approved these changes
Dec 5, 2018
jaygambetta
pushed a commit
to jaygambetta/qiskit-terra
that referenced
this pull request
Dec 5, 2018
* `shallow_schema` refactor to make its generation more maintainable. Overview ======== This PR introduces a kind of _mixin field_ `ModelValidation` with model type validation. All the Marshmallow fields are imported into `qiskit.validation.fields` and extended to include the mixin. The same happens with polymorphic fields and containers. Validation is possible thanks to a special copy of the model schema, the _shallow schema_, which replaces the `_deserialize` private method of its fields (at instance-level) to use the new `validate_model` from the `ModelValidator` mixin. Validating models consist into checking that the values are of the corresponding types. Specifically: 1. For regular fields, the type they deserialize to (deserialization type). 2. For fields containers (such as lists), the deserialization type of the contained field. 3. For schema containers (such as nested schemas), the type of the schema's model. 4. For polymorphic fields, one of the deserialization types of the choices. 5. For polymorphic schemas, one of the types of the possible schemas' model. While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider: ```python Book(title="The Terror", author=Person(name="Dan Simmons")) ``` `Person` instantiation is executed first. If successful, validating `Book` does not require `author`'s internal structure (i.e. `name`) to be validated again since otherwise, construction of `Person` would have failed. --- This refactor continues patching the method `_deserialize` of the schema fields but, instead of deciding how to patch it according to the type, it makes `_deserialize` to call a new custom method `_validate_model` with the desired behaviour. The different strategies from the if/elif/else block of the original implementation are now spread over the fields defined in qiskit.validation.fields This package also contains versions of marshmallow.fields that include their own `_validate_model` method. * Organize the fields into several modules and factor out validation. This PR split the fields into general fields, containers and polymorphic. Each module has the definition of the different kind of fields. It also provides a base field for those able of model type validation and a general mechanism to perform this validation. * Polishing docstrings and reviewing container and polymorphic validations. * Rename to make intentions clear and fixed fields hierarchy * Moving tests to its own folder * Split tests into different categories * Docstrings fixes and clarifications. * Style, convention and doc tweaks * Update CHANGELOG
jaygambetta
added a commit
that referenced
this pull request
Dec 5, 2018
* moving things * Removing * Moving around * Test * Changeling * Refactor shallow schema (#1350) * `shallow_schema` refactor to make its generation more maintainable. Overview ======== This PR introduces a kind of _mixin field_ `ModelValidation` with model type validation. All the Marshmallow fields are imported into `qiskit.validation.fields` and extended to include the mixin. The same happens with polymorphic fields and containers. Validation is possible thanks to a special copy of the model schema, the _shallow schema_, which replaces the `_deserialize` private method of its fields (at instance-level) to use the new `validate_model` from the `ModelValidator` mixin. Validating models consist into checking that the values are of the corresponding types. Specifically: 1. For regular fields, the type they deserialize to (deserialization type). 2. For fields containers (such as lists), the deserialization type of the contained field. 3. For schema containers (such as nested schemas), the type of the schema's model. 4. For polymorphic fields, one of the deserialization types of the choices. 5. For polymorphic schemas, one of the types of the possible schemas' model. While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider: ```python Book(title="The Terror", author=Person(name="Dan Simmons")) ``` `Person` instantiation is executed first. If successful, validating `Book` does not require `author`'s internal structure (i.e. `name`) to be validated again since otherwise, construction of `Person` would have failed. --- This refactor continues patching the method `_deserialize` of the schema fields but, instead of deciding how to patch it according to the type, it makes `_deserialize` to call a new custom method `_validate_model` with the desired behaviour. The different strategies from the if/elif/else block of the original implementation are now spread over the fields defined in qiskit.validation.fields This package also contains versions of marshmallow.fields that include their own `_validate_model` method. * Organize the fields into several modules and factor out validation. This PR split the fields into general fields, containers and polymorphic. Each module has the definition of the different kind of fields. It also provides a base field for those able of model type validation and a general mechanism to perform this validation. * Polishing docstrings and reviewing container and polymorphic validations. * Rename to make intentions clear and fixed fields hierarchy * Moving tests to its own folder * Split tests into different categories * Docstrings fixes and clarifications. * Style, convention and doc tweaks * Update CHANGELOG * Make to_dict and from_dict more discoverable, adjust converter tests (#1428) * Move to_dict and from_dict to BaseModel Move the `to_dict()` and `from_dict()` methods directly to the `BaseModel` instead of appending them at decoration time, for ease of usage and aiding discovery. * Fix converter tests * Result postprocess (#1404) * changelog * add result.postprocessing functions * format_statevector * restore ibmq _reorder_bits * update get_statevector, get_unitary, get_counts * remove the temporary hex conversion of tests * update remaining tests * Use integers in properties.gates.qubits (#1427) Use `integer` instead of `number` in the schema for BackendProperties (`BackendProperties.gates[x].qubits`). * Cleaning up * Linting
lia-approves
pushed a commit
to edasgupta/qiskit-terra
that referenced
this pull request
Jul 30, 2019
* `shallow_schema` refactor to make its generation more maintainable. Overview ======== This PR introduces a kind of _mixin field_ `ModelValidation` with model type validation. All the Marshmallow fields are imported into `qiskit.validation.fields` and extended to include the mixin. The same happens with polymorphic fields and containers. Validation is possible thanks to a special copy of the model schema, the _shallow schema_, which replaces the `_deserialize` private method of its fields (at instance-level) to use the new `validate_model` from the `ModelValidator` mixin. Validating models consist into checking that the values are of the corresponding types. Specifically: 1. For regular fields, the type they deserialize to (deserialization type). 2. For fields containers (such as lists), the deserialization type of the contained field. 3. For schema containers (such as nested schemas), the type of the schema's model. 4. For polymorphic fields, one of the deserialization types of the choices. 5. For polymorphic schemas, one of the types of the possible schemas' model. While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider: ```python Book(title="The Terror", author=Person(name="Dan Simmons")) ``` `Person` instantiation is executed first. If successful, validating `Book` does not require `author`'s internal structure (i.e. `name`) to be validated again since otherwise, construction of `Person` would have failed. --- This refactor continues patching the method `_deserialize` of the schema fields but, instead of deciding how to patch it according to the type, it makes `_deserialize` to call a new custom method `_validate_model` with the desired behaviour. The different strategies from the if/elif/else block of the original implementation are now spread over the fields defined in qiskit.validation.fields This package also contains versions of marshmallow.fields that include their own `_validate_model` method. * Organize the fields into several modules and factor out validation. This PR split the fields into general fields, containers and polymorphic. Each module has the definition of the different kind of fields. It also provides a base field for those able of model type validation and a general mechanism to perform this validation. * Polishing docstrings and reviewing container and polymorphic validations. * Rename to make intentions clear and fixed fields hierarchy * Moving tests to its own folder * Split tests into different categories * Docstrings fixes and clarifications. * Style, convention and doc tweaks * Update CHANGELOG
lia-approves
pushed a commit
to edasgupta/qiskit-terra
that referenced
this pull request
Jul 30, 2019
* moving things * Removing * Moving around * Test * Changeling * Refactor shallow schema (Qiskit#1350) * `shallow_schema` refactor to make its generation more maintainable. Overview ======== This PR introduces a kind of _mixin field_ `ModelValidation` with model type validation. All the Marshmallow fields are imported into `qiskit.validation.fields` and extended to include the mixin. The same happens with polymorphic fields and containers. Validation is possible thanks to a special copy of the model schema, the _shallow schema_, which replaces the `_deserialize` private method of its fields (at instance-level) to use the new `validate_model` from the `ModelValidator` mixin. Validating models consist into checking that the values are of the corresponding types. Specifically: 1. For regular fields, the type they deserialize to (deserialization type). 2. For fields containers (such as lists), the deserialization type of the contained field. 3. For schema containers (such as nested schemas), the type of the schema's model. 4. For polymorphic fields, one of the deserialization types of the choices. 5. For polymorphic schemas, one of the types of the possible schemas' model. While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider: ```python Book(title="The Terror", author=Person(name="Dan Simmons")) ``` `Person` instantiation is executed first. If successful, validating `Book` does not require `author`'s internal structure (i.e. `name`) to be validated again since otherwise, construction of `Person` would have failed. --- This refactor continues patching the method `_deserialize` of the schema fields but, instead of deciding how to patch it according to the type, it makes `_deserialize` to call a new custom method `_validate_model` with the desired behaviour. The different strategies from the if/elif/else block of the original implementation are now spread over the fields defined in qiskit.validation.fields This package also contains versions of marshmallow.fields that include their own `_validate_model` method. * Organize the fields into several modules and factor out validation. This PR split the fields into general fields, containers and polymorphic. Each module has the definition of the different kind of fields. It also provides a base field for those able of model type validation and a general mechanism to perform this validation. * Polishing docstrings and reviewing container and polymorphic validations. * Rename to make intentions clear and fixed fields hierarchy * Moving tests to its own folder * Split tests into different categories * Docstrings fixes and clarifications. * Style, convention and doc tweaks * Update CHANGELOG * Make to_dict and from_dict more discoverable, adjust converter tests (Qiskit#1428) * Move to_dict and from_dict to BaseModel Move the `to_dict()` and `from_dict()` methods directly to the `BaseModel` instead of appending them at decoration time, for ease of usage and aiding discovery. * Fix converter tests * Result postprocess (Qiskit#1404) * changelog * add result.postprocessing functions * format_statevector * restore ibmq _reorder_bits * update get_statevector, get_unitary, get_counts * remove the temporary hex conversion of tests * update remaining tests * Use integers in properties.gates.qubits (Qiskit#1427) Use `integer` instead of `number` in the schema for BackendProperties (`BackendProperties.gates[x].qubits`). * Cleaning up * Linting
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR introduces a kind of mixin field
ModelValidation
with model type validation. All the Marshmallow fields are imported intoqiskit.validation.fields
and extended to include the mixin. The same happens with polymorphic fields and containers.Validation is possible thanks to a special copy of the model schema, the shallow schema, which replaces the
_deserialize
private method of its fields (at instance-level) to use the newvalidate_model
from theModelValidator
mixin.Validating models consist into checking that the values are of the corresponding types. Namely:
While validating, nothing more than the type of the value is considered. Validation assumes that the values of the inner properties are already valid or they would have failed during their instantiation. For instance, consider:
Person
instantiation is executed first. If successful, validatingBook
does not requireauthor
internal structure (i.e.name
) to be validated again since otherwise, construction ofPerson
would have failed.