Skip to content

Replace TypeAlias _ClassLevelWidgetT with descriptor _WidgetTypeOrInstance #2615

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

michalpokusa
Copy link
Contributor

I have made things!

Related issues

None I could find.

This change allows for recognizing when .widget is used on Field instance and when on the type itself, allowing for different type annotations in each case.

image
image
image

Why even bother?
Example: Currently this correct code does not recognize is_required and attrs on field.widget, because it might be a type and not an instance.
image

After the change all types are correctly inferred, and syntax highlighting works as expected:
image

I hope you find it worth adding.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good idea! Thanks!

Please, fix the CI :)

@michalpokusa michalpokusa force-pushed the field-widget-rework branch 2 times, most recently from f0c7575 to 097bd62 Compare April 21, 2025 15:23
@michalpokusa michalpokusa changed the title Replace TypeAlias _ClassLevelWidgetT with descriptor WidgetTypeOrInstance Replace TypeAlias _ClassLevelWidgetT with descriptor _WidgetTypeOrInstance Apr 21, 2025
@michalpokusa
Copy link
Contributor Author

@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.

@UnknownPlatypus
Copy link
Contributor

Setuptools version 79 got installed, I suppose it probably breaks the multiple SETUPTOOLS_ENABLE_FEATURES in ci

@sobolevn
Copy link
Member

you can pin older setuptools for now in a separate PR :)

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add several type assertions for this? https://github.com/typeddjango/django-stubs/tree/master/tests/assert_type

@michalpokusa
Copy link
Contributor Author

michalpokusa commented Apr 22, 2025

@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 Widget, or I could make it generic, and type widget individually for each field type.

Example:
image
image
image
image

It provides more precise typing, but I am not sure whether it is not too restrictive.
On the other hand MultipleChoiceField could have a widget of type SelectMultiple or CheckboxSelectMultiple.
And how should it work with custom field types and custom widgets, wouldn't it break them?

What do you think about that?

@michalpokusa michalpokusa marked this pull request as draft April 22, 2025 22:05
@sobolevn
Copy link
Member

Let's first add the simplest typing possible. We can later update it to cover more cases.

@michalpokusa
Copy link
Contributor Author

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.
All the errors center around widgets being MediaDefiningClass instances. The interesting thing is that e.g. Field.hidden_widget is typed as type[Widget], while BaseFormSet.deletion_widget as MediaDefiningClass inside ".pyi" files.

Any further guidance would be highly appreciated.

@sobolevn
Copy link
Member

Based on the amount of failures I would say that this seems overly complex to type properly :(

However, I am open to ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants