Skip to content

Improve stubs for flatten and flatten_fieldsets #2572

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

Merged
merged 2 commits into from
Mar 25, 2025

Conversation

ngnpope
Copy link
Contributor

@ngnpope ngnpope commented Mar 18, 2025

I have made things!

These functions are implemented in a generic way that doesn't generally care about the content. The existing return type of Callable | str makes these awkward to use.

I've changed the use of Sequence in the _FieldGroups alias to use _ListOrTuple. This is another case where ModelAdmin.fields and the "fields" item in fieldsets are presumed by Django to be a list or tuple, e.g. there are some system checks that ensure this.

See https://github.com/django/django/blob/afbb8c709d40e77b3f71c152d363c5ad95ceec2d/django/contrib/admin/utils.py#L102-L120

Related issues

N/A

@ngnpope ngnpope force-pushed the improve-flatten-functions branch from 350a803 to 046aa7c Compare March 18, 2025 16:57
@ngnpope ngnpope marked this pull request as ready for review March 18, 2025 17:11
@ngnpope

This comment was marked as outdated.

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.

Thanks!

@ngnpope ngnpope force-pushed the improve-flatten-functions branch 4 times, most recently from 4c89ad4 to 2438ddb Compare March 25, 2025 16:41
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 pyright is not happy with something :(

@ngnpope ngnpope force-pushed the improve-flatten-functions branch 2 times, most recently from d927064 to bca1c4d Compare March 25, 2025 16:59
@ngnpope
Copy link
Contributor Author

ngnpope commented Mar 25, 2025

Looks like pyright is not happy with something :(

Yeah... Looking into that. It's hard to get the different type checkers to agree on pretty much anything at all it seems 😞

I've fixed a bunch of the pyright issues, ignored a few around the model fields that can't be handled until we support making fields generic as per #2214, and a handful still to check.

The good news is that, by adding some testing, I ended up simplifying the implementation.

ngnpope added 2 commits March 25, 2025 17:12
This renames `_DisplayT` to `_ListDisplayT` and creates a new
`_DisplayT` alias with the contents of the `_ListOrTuple`.

This will be used in the next commit.
These functions are implemented in a generic way that doesn't
generally care about the content. The existing return type of
`Callable | str` makes these awkward to use.

I've changed the use of `Sequence` in the `_FieldGroups` alias to use
`_ListOrTuple`. This is another case where `ModelAdmin.fields` and the
`"fields"` item in fieldsets are presumed by Django to be a `list` or
`tuple`, e.g. there are some system checks that ensure this.

See https://github.com/django/django/blob/afbb8c709d40e77b3f71c152d363c5ad95ceec2d/django/contrib/admin/utils.py#L102-L120
@ngnpope ngnpope force-pushed the improve-flatten-functions branch from bca1c4d to d1a968e Compare March 25, 2025 17:21
@ngnpope
Copy link
Contributor Author

ngnpope commented Mar 25, 2025

Success @sobolevn! Somehow it's turned out rather clean after my silly false starts! 😅

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.

Awesome! Thanks!

@sobolevn sobolevn merged commit 4debb57 into typeddjango:master Mar 25, 2025
40 checks passed
@ngnpope ngnpope deleted the improve-flatten-functions branch March 25, 2025 19:05
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.

2 participants