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

Fix check_constraint_condition fixer for GIS models module #514

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
=========

* Fix ``check_constraint_condition`` fixer to work when ``django.contrib.gis.db.models`` is used to import ``CheckConstraint``.

`Issue #513 <https://github.com/adamchainz/django-upgrade/issues/513>`__.

1.22.1 (2024-10-11)
-------------------

Expand Down
11 changes: 9 additions & 2 deletions src/django_upgrade/fixers/check_constraint_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ def visit_Call(
(
isinstance(node.func, ast.Name)
and node.func.id == "CheckConstraint"
and "CheckConstraint" in state.from_imports["django.db.models"]
and (
"CheckConstraint" in state.from_imports["django.db.models"]
or "CheckConstraint"
in state.from_imports["django.contrib.gis.db.models"]
)
)
or (
isinstance(node.func, ast.Attribute)
and node.func.attr == "CheckConstraint"
and isinstance(node.func.value, ast.Name)
and node.func.value.id == "models"
and "models" in state.from_imports["django.db"]
and (
"models" in state.from_imports["django.db"]
or "models" in state.from_imports["django.contrib.gis.db"]
)
)
)
and (kwarg_names := {k.arg for k in node.keywords})
Expand Down
30 changes: 30 additions & 0 deletions tests/fixers/test_check_constraint_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ def test_success_name():
)


def test_success_name_gis():
check_transformed(
"""\
from django.contrib.gis.db.models import CheckConstraint

CheckConstraint(check=Q(id=1))
""",
"""\
from django.contrib.gis.db.models import CheckConstraint

CheckConstraint(condition=Q(id=1))
""",
)


def test_success_attr():
check_transformed(
"""\
Expand All @@ -101,6 +116,21 @@ def test_success_attr():
)


def test_success_attr_gis():
check_transformed(
"""\
from django.contrib.gis.db import models

models.CheckConstraint(check=models.Q(id=1))
""",
"""\
from django.contrib.gis.db import models

models.CheckConstraint(condition=models.Q(id=1))
""",
)


def test_success_other_args():
check_transformed(
"""\
Expand Down