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

Support single tuple versioned block fixer #517

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Support single-tuple Django version in the versioned block fixer.

Thanks to Thibaut Decombe in `PR #517 <https://github.com/adamchainz/django-upgrade/pull/517>`__.

1.22.2 (2024-12-02)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion src/django_upgrade/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def is_passing_comparison(
and isinstance(test.ops[0], (ast.Gt, ast.GtE, ast.Lt, ast.LtE))
and len(test.comparators) == 1
and isinstance((comparator := test.comparators[0]), ast.Tuple)
and len(comparator.elts) == 2
and 0 < len(comparator.elts) <= 2
and all(isinstance(e, ast.Constant) for e in comparator.elts)
and all(isinstance(cast(ast.Constant, e).value, int) for e in comparator.elts)
):
Expand Down
44 changes: 44 additions & 0 deletions tests/fixers/test_versioned_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
check_transformed = partial(tools.check_transformed, settings=settings)


def test_empty_tuple():
check_noop(
"""\
import django

if django.VERSION > ():
foo()
""",
)


def test_future_version_gt():
check_noop(
"""\
Expand Down Expand Up @@ -263,3 +274,36 @@ def test_removed_block_internal_comment():

""",
)


def test_old_version_lt_single_tuple():
check_transformed(
"""\
import django

if django.VERSION < (4,):
foo()
bar()
""",
"""\
import django

bar()
""",
)


def test_current_version_gte_single_tuple():
check_transformed(
"""\
import django

if django.VERSION >= (4,):
foo()
""",
"""\
import django

foo()
""",
)