Replace TypeAlias _ClassLevelWidgetT with descriptor _WidgetTypeOrInstance#2615
Replace TypeAlias _ClassLevelWidgetT with descriptor _WidgetTypeOrInstance#2615michalpokusa wants to merge 6 commits intotypeddjango:masterfrom
_ClassLevelWidgetT with descriptor _WidgetTypeOrInstance#2615Conversation
sobolevn
left a comment
There was a problem hiding this comment.
Looks like a good idea! Thanks!
Please, fix the CI :)
f0c7575 to
097bd62
Compare
_ClassLevelWidgetT with descriptor WidgetTypeOrInstance_ClassLevelWidgetT with descriptor _WidgetTypeOrInstance
…rInstance` for `Field.widget`
097bd62 to
4c4e795
Compare
|
@sobolevn I think I might need some help there. I checked why the CI is failing and it looks like errors are not even slightly related to what I have changed. I checkouted the current origin/HEAD, ran the test locally and also I got a lot of errors. Is there something obvious I am missing? I believe there might be a problem with the CI itself as I see that on other PR #2616 also fails in the same way. |
|
Setuptools version 79 got installed, I suppose it probably breaks the multiple |
|
you can pin older |
sobolevn
left a comment
There was a problem hiding this comment.
Can you please also add several type assertions for this? https://github.com/typeddjango/django-stubs/tree/master/tests/assert_type
|
@sobolevn I started adding type assertions, and I would like to get you point of view on one thing. I could leave this as typing everything as the most general It provides more precise typing, but I am not sure whether it is not too restrictive. What do you think about that? |
|
Let's first add the simplest typing possible. We can later update it to cover more cases. |
4426950 to
6c0e4a0
Compare
|
I have added assert type tests. Seems like this is a bit more complex than I initailly assumed, right now there is a problem with stubtest. Any further guidance would be highly appreciated. |
|
Based on the amount of failures I would say that this seems overly complex to type properly :( However, I am open to ideas. |
|
Would it be possible to supress these errors? It seems like they are a side affect of some problem with django/mypy itself. I tried to fix the errors in Any opinion on that would be appreciated. |
I think it might be actual error for the types when working with a field class, not an instance of a field. I do agree the error message is a bit weird tho but the Could you add a test with a custom Field using a custom widget ? Something like that: class MyField(Field):
widget = InputI think it will fail for the same reason as in #2650, I couldn't find a way to make it work with this approach for a similar need, maybe you'll find a way. Basically the approach with a descriptor makes it mostly work when working with instances of |
… and added widgets to allowlist.txt
c84dd6b to
fc18a3f
Compare
|
After a few hours of researching and testing multiple approaches, which mainly included trying to overcome problems with mypy, I finally came up with current version. So, right now:
def __init__(self, **kwargs):
if "widget" not in kwargs:
kwargs["widget"] = RangeWidget(self.base_field.widget)
Overall, all the tests pass, and I feel much more confident about this version rather than trying to make mypy happy, even when it is clearly wrong with its detection. @sobolevn I would really appreciate your opinion on this, I believe it fits with the rest of the django-stubs codebase and that the use of allowlist.txt is justified in this case. |









I have made things!
Related issues
None I could find.
This change allows for recognizing when
.widgetis used onFieldinstance and when on the type itself, allowing for different type annotations in each case.Why even bother?

Example: Currently this correct code does not recognize
is_requiredandattrsonfield.widget, because it might be a type and not an instance.After the change all types are correctly inferred, and syntax highlighting works as expected:

I hope you find it worth adding.