-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
django-stubs-ext: Add BaseModelMeta for typing Model inner Meta class #1375
Conversation
class Meta
inner class
Couldn't this be plugin territory also? Since it's Django's metaclass extension that constructs the Option instance. I suppose one would've hidden the inheritance/declaration behind a Perhaps that should be the suggestion/example, instead of suggesting runtime?
Also, a different thought as I'm writing. |
True, I suppose that would be better. But I'm afraid the implementation will end up being more complex than it sounds. I'm also not very familiar with plugin code.
How? Then users would need to also declare the contents of Meta twice, no?
Sorry, I don't understand. Where is mutation suggested? Yes, the values end up being accessible via the |
Sorry, it was a bit unclear regarding the mutation stuff. I think these are the relevant lines: It kind of seems that So the only gain we can have here, currently, is correct types on those |
Haha, yeah, that's a bit off. But what I mean is related to the protocol for Point is, that it's some protocol with optional attributes which should be attached as an attribute on Something like
Above is not correct, I'm just trying to display what I think is a limitation within the type system. |
Ah not really. AFAICT it just checks that there aren't any unrecognized attribute names. It doesn't check the types of attributes values. Some Meta types are checked in system checks, but not all of them.
Ahh yes, supporting that would be very cool. If it's not supported, I wonder if there's already a feature request in mypy. |
In the end, perhaps the approach here is as close as we'll get. Since Django will error during runtime on invalid attributes, providing a parent class with the types from And in practice, what happens, is like if you'd inherit a |
That's what I meant, but having incorrect types might make Django crash on some of the attributes. Which will happen before the type checking can tell there's an incorrect type. But that solely depends on what Django does with the attribute in question. |
I found some info on "partial protocols"/optional members in PEP 544 # Support optional protocol members. But it's suggested as a "future revisit" |
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.
I like the idea!
I think that we can later explore other options (including plugins, Meta: SomeProtocol
definitions in Model
type, etc).
But, I think that this is a farly simple solution that clearly solves a given problem!
And we clearly needs some docs for it. |
I looked into this, there are two pieces missing in mypy right now. I opened the latter feature request.
|
b16ec4a
to
1f23c88
Compare
1f23c88
to
6faa662
Compare
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.
👍
|
||
from django_stubs_ext import StrOrPromise | ||
|
||
class BaseModelMeta: |
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.
Any thoughts on this naming? BaseModelMeta? ModelMetaBase? TypedModelMeta?
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.
Also the module name. I chose django_stubs_ext.db.models
to match Django's django.db.models
.
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.
I like the way it is: db.models.BaseModelMeta
👍
Note: this feature was renamed from |
I've had some thoughts about how to expand
django-stubs-ext
.We can provide a base class for the Model inner
class Meta
. This inner class provides type hints for all available attributes, and thus mypy can check whether actual usages have valid types or not.Example usage: