Added support to create Generic models#987
Open
rschyboll wants to merge 8 commits intotortoise:developfrom
Open
Added support to create Generic models#987rschyboll wants to merge 8 commits intotortoise:developfrom
rschyboll wants to merge 8 commits intotortoise:developfrom
Conversation
Removed __getitem__ from ModelMeta and replaced it with __class_getitem__ in Model class, which checks if passes key is a class. If it is, it returns a _GenericAlias instance. If not, it makes the same action as before.
long2ice
approved these changes
Nov 24, 2021
Changed the __class_getitem__ method from returing directly a _GenericAliast method to it calling the __class_getitem__ from the Generic class. Also added a check, if the model inherits from Generic. It's a safety measure, so that the user has to inherit from Generic to create generic models.
long2ice
approved these changes
Nov 25, 2021
long2ice
previously approved these changes
Nov 26, 2021
Simply added #type: ignore comments, due to type checkers not working with __class_getitem__ properly yet. The old __getitem__ method was also disabled from type checking.
…ise-orm into generic-models
This file contains hidden or 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
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.
Description
Removed getitem from ModelMeta and replaced it with _class_getitem in Model class, which is described in PEP 560.
Also added to the function one condition, which checks if the key passed in is a tuple with classes or a single class, if it is, then it returns a _GenericAlias, which allows python to create that model as a Generic class instance.
If the user passes a key, which is not a class, it behaves the same way as before.
Motivation and Context
I was trying to make generic Models, which would allow me to create some generic methods, that would allow me to use them in all of my models. One example of such method would be:
It is a helper method, which returns a view class, that is later returned to the user, thanks to the Generic ability of that model, i need only to reimplement the abstract to_view method, and all the other helper methods have already the required type.
How Has This Been Tested?
Obviously i tested it only in my own code, so i couldn't cover all scenarios, but i also ran all tests from "make test", and "test_sqlite" without any errors.
In my understanding, it shouldn't affect any other areas, because the method before was only to get one model instance by using the models class as an array, accesing it by it's primary key. Hopefully i'm right, if not, then sorry in advance :(
Checklist: