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

Add Django 1.10+ request.user attribute fixer #423

Merged
merged 18 commits into from
Feb 11, 2024

Conversation

Alurith
Copy link
Contributor

@Alurith Alurith commented Feb 6, 2024

Fixes #419

Copy link
Owner

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

Thanks for starting on this one! There’s still an unused import, and I’ve left one comment on an issue I spotted.

Please also add a changelog note crediting yourself and documentation in the style of the other fixers.

Comment on lines 66 to 67
y = find(tokens, i, name=OP, src=")")
del tokens[j + 1 : y + 1]
Copy link
Owner

Choose a reason for hiding this comment

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

Find the opening parenthesis and delete from the opening to closing only, rather than j+1. There may be space, even newlines and comments, before the parenthesis!

Add these tests to see what I mean:

request . user . is_authenticated ( )
if (
    request
    .user
    .is_authenticated  # bla
    ()
):
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, I also found a whitespace in the fixer's filename that I removed.

I am working on a fix for the additional cases you suggested but I encountered an issue.
I've changed the rewrite function like this:

def rewrite_user_is_auth(tokens: list[Token], i: int) -> None:
    j = find(tokens, i, name=NAME, src="is_authenticated")
    y = find(tokens, j, name=OP, src="(")
    z = find(tokens, y, name=OP, src=")")
    del tokens[z]
    del tokens[y]

but I can't make it work with this case

def test_spaces_and_comments():
    check_transformed(
        """\
        if (
            request
            .user
            .is_authenticated  # bla
            ()
        ):
            ...
        """,
        """\
        if (
            request
            .user
            .is_authenticated  # bla
            
        ):
            ...
        """,
        settings,
    )

This is the output of pytest

>       assert fixed == dedented_after
E       AssertionError: assert 'if (\n    request\n    .user\n    .is_authenticated  # bla\n    \n):\n    ...\n' == 'if (\n    request\n    .user\n    .is_authenticated  # bla\n    ()\n):\n    ...\n'
E
E           if (
E               request
E               .user
E               .is_authenticated  # bla
E         -     ()
E         ?     --
E         +     
E           ):
E               ...

tests\fixers\tools.py:25: AssertionError

It seems the only different between the two string are some whitespaces after the comment, so I tought it was because of my formatter but even disabling it or thinkering with the whitespaces didn't solve the issue.

I am sure I am missing something here, can you point me in the right direction?

Copy link
Owner

Choose a reason for hiding this comment

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

The problem is gonna be because either Black or textwrap.dedent() trims trailing whitespace in the file, whilst the fixer leaves it. We’re fine leaving the whitespace, django-upgrade leaves formatting to Black or whatever.

This version of the test should work:

def test_spaces_and_comments():
    check_transformed(
        """\
        if (
            request
            .user
            .is_authenticated  # bla
            () # bla
        ):
            ...
        """,
        """\
        if (
            request
            .user
            .is_authenticated  # bla
             # bla
        ):
            ...
        """,
        settings,
    )

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, doing two del operations will also break things. After deleting a token, the laters ones shift leftwards in position, decreasing their indexes. Best to delete a range instead - leaving a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was deleting first the closing parenthesis w/ del tokens[z] and the the opening one w/ del tokens[y] to not shift positions. Doing as you suggest will remove the space between the parentheses in this case.

def test_spaces_between():
    check_transformed(
        "request . user . is_authenticated ( )",
        "request . user . is_authenticated  ",
        settings,
    )

Is that fine?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, that is more consistent IMO. It will remove a comment in this obscure case:

request.user.is_anonymous(  # something
)

But I think that’s fine.

Copy link
Owner

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

I read the release notes section and noted that is_anonymous was also changed. Let’s make the fixer cover both (suggestions below), rename the fixer file to request_user_attributes.py, and the tests to test_request_user_attributes.py.

I will action these changes now to speed things along.

README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
src/django_upgrade/fixers/request_user_is_authenticated.py Outdated Show resolved Hide resolved
src/django_upgrade/fixers/request_user_is_authenticated.py Outdated Show resolved Hide resolved
src/django_upgrade/fixers/request_user_is_authenticated.py Outdated Show resolved Hide resolved
Comment on lines 66 to 67
y = find(tokens, i, name=OP, src=")")
del tokens[j + 1 : y + 1]
Copy link
Owner

Choose a reason for hiding this comment

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

Ah, doing two del operations will also break things. After deleting a token, the laters ones shift leftwards in position, decreasing their indexes. Best to delete a range instead - leaving a suggestion.

src/django_upgrade/fixers/request_user_is_authenticated.py Outdated Show resolved Hide resolved
@adamchainz adamchainz changed the title Add fixer to convert is_authenticated() to is_authenticated on django 1.10+ Add Django 1.10+ request.user attribute fixer Feb 11, 2024
@adamchainz adamchainz enabled auto-merge (squash) February 11, 2024 22:00
@adamchainz adamchainz merged commit 1bb76f6 into adamchainz:main Feb 11, 2024
7 checks passed
@adamchainz
Copy link
Owner

Thank you for your contribution! Released in 1.16.0.

@Alurith Alurith deleted the issue_419 branch February 12, 2024 08:12
j01101111sh pushed a commit to norns-dev/norns that referenced this pull request 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 pull request 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>
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

Successfully merging this pull request may close these issues.

Convert is_authenticated() to is_authenticated
2 participants