From b93779d36974b0107deda2f0340e0367ee06c21e Mon Sep 17 00:00:00 2001 From: Thibaut Decombe Date: Sun, 15 Dec 2024 17:31:42 +0100 Subject: [PATCH 1/2] Support one element tuple in the versioned block fixer --- src/django_upgrade/ast.py | 2 +- tests/fixers/test_versioned_branches.py | 44 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/django_upgrade/ast.py b/src/django_upgrade/ast.py index 1acc86a3..5ceea4ed 100644 --- a/src/django_upgrade/ast.py +++ b/src/django_upgrade/ast.py @@ -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) ): diff --git a/tests/fixers/test_versioned_branches.py b/tests/fixers/test_versioned_branches.py index 6106e814..afa6c393 100644 --- a/tests/fixers/test_versioned_branches.py +++ b/tests/fixers/test_versioned_branches.py @@ -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( """\ @@ -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() + """, + ) From 2ac3a8b5b5b6fce0e97b44a4393cbc6146737b0b Mon Sep 17 00:00:00 2001 From: Thibaut Decombe Date: Sun, 15 Dec 2024 17:31:53 +0100 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c4feca95..434397d9 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ Changelog ========= +* Support single-tuple Django version in the versioned block fixer. + + Thanks to Thibaut Decombe in `PR #517 `__. + 1.22.2 (2024-12-02) -------------------