Skip to content
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

.choices dropped for choices not subclassing the *Choices class #417

Open
niccolomineo opened this issue Jan 11, 2024 · 9 comments
Open

Comments

@niccolomineo
Copy link

Python Version

3.12.1

Django Version

5.0.1

Package Version

1.15.0

Description

I have a number of choice classes in a parent-child relationship. Such a relationship is the reason why, in those classes, I am not inheriting from any Django *Choices class.

I am using these choice classes in choices field params and django-upgrade appears to be dropping the .choices attribute since it is supporting Django 5.0. Passing that attribute is vital for such choice classes to work, though.

Shouldn't django-upgrade detect whether the passed .choices belongs to a class inheriting from any Django *Choices class, and only in that case drop the .choices attribute?

@niccolomineo niccolomineo changed the title .choices dropped choices not subclassing the *Choices class .choices dropped for choices not subclassing the *Choices class Jan 11, 2024
@adamchainz
Copy link
Owner

Shouldn't django-upgrade detect whether the passed .choices belongs to a class inheriting from any Django *Choices class, and only in that case drop the .choices attribute?

In general, django-upgrade can't do that as the AST is insufficient to determine the class. It could apply in niche cases.

We discussed this possibility in #369, and I guess it was probably rare enough not to occur. Well, here we are with a report that it happened. I think we should probably revert the feature. It's not that big of a fix anyway.

What do you think @UnknownPlatypus ?

@UnknownPlatypus
Copy link
Contributor

The easy solution is probably to revert the feature I agree.

But In the meantime, they will be some people with codebase they have control over knowing this fixer is safe for them.
Maybe we could reconsider the idea of having some fixers disabled by default and you'd have to opt-in for them if you know they are safe for you use case ? (I briefly gave some ideas in this comment)

Another option would be to track TextChoices/IntegerChoices definitions in the file and only apply fix in these case but it adds some complexity and makes the fixer less predictable for people.

Let me know what you prefer, I'll be happy to help anyways.

@washeck
Copy link

washeck commented Feb 8, 2024

I just want to add that I was also hit by this. Our codebase is full of classes like this one

class ReportStatus:
    IN_PROGRESS = "IN_PROGRESS"
    COMPLETED = "COMPLETED"
    choices = ((IN_PROGRESS, _("In progress")), (COMPLETED, _("Completed")))

which we used before TextChoices existed and removal of the .choices attribute broke our code.

@adamchainz
Copy link
Owner

adamchainz commented Feb 11, 2024

I’ve reverted the fixer for now in #425, released in 1.16.0. But @UnknownPlatypus if you want to limit it to definitions within the same module, I think that would be a good compromise. It’s likely to fix many cases but not all.

@niccolomineo
Copy link
Author

niccolomineo commented Feb 11, 2024

I’ve reverted the fixer for now in #425, released in 1.16.0. But @UnknownPlatypus if you want to limit it to definitions within the same module, I think that would be a good compromise. It’s likely to fix many cases but not all.

Thanks! As a long term fix, what about checking the inheriting class to tell whether .choices is needed?

@adamchainz
Copy link
Owner

Yes, as long as the choices class exists within the same module.

j01101111sh pushed a commit to norns-dev/norns that referenced this issue Feb 12, 2024
[//]: # (dependabot-start)
⚠️  **Dependabot is rebasing this PR** ⚠️ 

Rebasing might not happen immediately, so don't worry if this takes some
time.

Note: if you make any changes to this PR yourself, they will take
precedence over the rebase.

---

[//]: # (dependabot-end)

Bumps [django-upgrade](https://github.com/adamchainz/django-upgrade)
from 1.15.0 to 1.16.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/adamchainz/django-upgrade/blob/main/CHANGELOG.rst">django-upgrade's
changelog</a>.</em></p>
<blockquote>
<h2>1.16.0 (2024-02-11)</h2>
<ul>
<li>
<p>Remove the Django 5.0+ fixer that dropped <code>.choices</code> from
model field <code>choices</code> parameters.
It was too unreliable because it could break use for “DIY” enumeration
types.</p>
<p>Thanks to Niccolò Mineo and washeck for reporting in <code>Issue
[#417](adamchainz/django-upgrade#417)
&lt;https://github.com/adamchainz/django-upgrade/issues/417&gt;</code>__.</p>
</li>
<li>
<p>Add Django 1.10+ fixer to rewrite <code>request.user</code> functions
that changed to boolean attributes: <code>is_authenticated</code> and
<code>is_anonymous</code>.</p>
<p>Thanks to Alessandro Ferrini in <code>PR
[#423](adamchainz/django-upgrade#423)
&lt;https://github.com/adamchainz/django-upgrade/pull/423&gt;</code>__.</p>
</li>
<li>
<p>Add Django 2.0+ imports fixes for names moved from
<code>django.core.urlresolvers</code> to <code>django.urls</code>.</p>
<p>Thanks to Thibaut Decombe in <code>PR
[#404](adamchainz/django-upgrade#404)
&lt;https://github.com/adamchainz/django-upgrade/pull/404&gt;</code>__.</p>
</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/103ef9dbca4cd70b3534bda23331da00f3162e1f"><code>103ef9d</code></a>
Version 1.16.0</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/76db0c5b496f8e3594f9001727c759dbbcb84870"><code>76db0c5</code></a>
Remove model field .choices fixer (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/425">#425</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d14b93f16e621d1e6be0b8bee22d2dcd3ec81db1"><code>d14b93f</code></a>
Add Django 2.0+ django.urls import fixes (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/404">#404</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/1bb76f6f537335bd96fe8cdd746afcd735238c0b"><code>1bb76f6</code></a>
Add Django 1.10+ request.user attribute fixer (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/423">#423</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/230eca81af39db709b95d268594e0143cbe616d3"><code>230eca8</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/422">#422</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d37d450d75160127ad7b4faae76c391c97e90fa8"><code>d37d450</code></a>
Use isort to sort imports (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/424">#424</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/5018a7dbbe4464521953ece034b1ff6bb3268725"><code>5018a7d</code></a>
Upgrade requirements (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/421">#421</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d70bf0e6d40515035054a5744b5dac29e0267dd8"><code>d70bf0e</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/420">#420</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/fa52dcfa2108f298fb87c7adda697a9c8376255d"><code>fa52dcf</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/416">#416</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/49ada8019c93ce9bfe7aac642b871f7cc45c1621"><code>49ada80</code></a>
Upgrade requirements (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/414">#414</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/adamchainz/django-upgrade/compare/1.15.0...1.16.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=django-upgrade&package-manager=pip&previous-version=1.15.0&new-version=1.16.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
j01101111sh pushed a commit to norns-dev/norns that referenced this issue Feb 27, 2024
[//]: # (dependabot-start)
⚠️  **Dependabot is rebasing this PR** ⚠️ 

Rebasing might not happen immediately, so don't worry if this takes some
time.

Note: if you make any changes to this PR yourself, they will take
precedence over the rebase.

---

[//]: # (dependabot-end)

Bumps [django-upgrade](https://github.com/adamchainz/django-upgrade)
from 1.15.0 to 1.16.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/adamchainz/django-upgrade/blob/main/CHANGELOG.rst">django-upgrade's
changelog</a>.</em></p>
<blockquote>
<h2>1.16.0 (2024-02-11)</h2>
<ul>
<li>
<p>Remove the Django 5.0+ fixer that dropped <code>.choices</code> from
model field <code>choices</code> parameters.
It was too unreliable because it could break use for “DIY” enumeration
types.</p>
<p>Thanks to Niccolò Mineo and washeck for reporting in <code>Issue
[#417](adamchainz/django-upgrade#417)
&lt;https://github.com/adamchainz/django-upgrade/issues/417&gt;</code>__.</p>
</li>
<li>
<p>Add Django 1.10+ fixer to rewrite <code>request.user</code> functions
that changed to boolean attributes: <code>is_authenticated</code> and
<code>is_anonymous</code>.</p>
<p>Thanks to Alessandro Ferrini in <code>PR
[#423](adamchainz/django-upgrade#423)
&lt;https://github.com/adamchainz/django-upgrade/pull/423&gt;</code>__.</p>
</li>
<li>
<p>Add Django 2.0+ imports fixes for names moved from
<code>django.core.urlresolvers</code> to <code>django.urls</code>.</p>
<p>Thanks to Thibaut Decombe in <code>PR
[#404](adamchainz/django-upgrade#404)
&lt;https://github.com/adamchainz/django-upgrade/pull/404&gt;</code>__.</p>
</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/103ef9dbca4cd70b3534bda23331da00f3162e1f"><code>103ef9d</code></a>
Version 1.16.0</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/76db0c5b496f8e3594f9001727c759dbbcb84870"><code>76db0c5</code></a>
Remove model field .choices fixer (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/425">#425</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d14b93f16e621d1e6be0b8bee22d2dcd3ec81db1"><code>d14b93f</code></a>
Add Django 2.0+ django.urls import fixes (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/404">#404</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/1bb76f6f537335bd96fe8cdd746afcd735238c0b"><code>1bb76f6</code></a>
Add Django 1.10+ request.user attribute fixer (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/423">#423</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/230eca81af39db709b95d268594e0143cbe616d3"><code>230eca8</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/422">#422</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d37d450d75160127ad7b4faae76c391c97e90fa8"><code>d37d450</code></a>
Use isort to sort imports (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/424">#424</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/5018a7dbbe4464521953ece034b1ff6bb3268725"><code>5018a7d</code></a>
Upgrade requirements (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/421">#421</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/d70bf0e6d40515035054a5744b5dac29e0267dd8"><code>d70bf0e</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/420">#420</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/fa52dcfa2108f298fb87c7adda697a9c8376255d"><code>fa52dcf</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/416">#416</a>)</li>
<li><a
href="https://github.com/adamchainz/django-upgrade/commit/49ada8019c93ce9bfe7aac642b871f7cc45c1621"><code>49ada80</code></a>
Upgrade requirements (<a
href="https://redirect.github.com/adamchainz/django-upgrade/issues/414">#414</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/adamchainz/django-upgrade/compare/1.15.0...1.16.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=django-upgrade&package-manager=pip&previous-version=1.15.0&new-version=1.16.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@UnknownPlatypus
Copy link
Contributor

I’ve reverted the fixer for now in #425, released in 1.16.0. But @UnknownPlatypus if you want to limit it to definitions within the same module, I think that would be a good compromise. It’s likely to fix many cases but not all.

I'm not sure it's worth because it would create inconsistency across the codebase, with people wondering why sometimes there is a .choices and sometimes not.

To handle this safely all the time, it would require multi-file analysis and type inference which is way to much work for this fixer. Maybe something a tool like ruff might be able to support at some point.

Given the very little gain in convenience and readability, I don't think it's worth the hassle.
What do you think @adamchainz ?

@adamchainz
Copy link
Owner

I'm not sure it's worth because it would create inconsistency across the codebase, with people wondering why sometimes there is a .choices and sometimes not.

I'm not so concerned about this. There are lots of edge cases django-upgrade backs out of handling because not everything can be automated.

To handle this safely all the time, it would require multi-file analysis and type inference which is way to much work for this fixer. Maybe something a tool like ruff might be able to support at some point.

Yes, agree.

Given the very little gain in convenience and readability, I don't think it's worth the hassle.
What do you think @adamchainz ?

I'm happy leaving this open for now, in case someone wants to retry it with the extra guards. I think it would be nice and help advertise the ability to skip .choices.

@UnknownPlatypus
Copy link
Contributor

Alright, your probably right. I might take a stab at this this week then

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

No branches or pull requests

4 participants