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

Re-allow expressions in UniqueConstraint #1220

Merged
merged 1 commit into from
Oct 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions django-stubs/db/models/constraints.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Any, Optional, Sequence, Tuple, Type, TypeVar, Union
from typing import Any, Optional, Sequence, Tuple, Type, TypeVar, Union, overload

from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models.base import Model
Expand Down Expand Up @@ -32,14 +32,23 @@ class UniqueConstraint(BaseConstraint):
fields: Tuple[str, ...]
condition: Optional[Q]
deferrable: Optional[Deferrable]

@overload
def __init__(
self,
*expressions: Union[str, Combinable],
fields: None = ...,
name: str,
condition: Optional[Q] = ...,
deferrable: Optional[Deferrable] = ...,
include: Optional[Sequence[str]] = ...,
opclasses: Sequence[Any] = ...,
) -> None: ...
@overload
def __init__(
self,
*,
# For 4.0:
# *expressions: Union[str, Combinable],
# fields: Optional[Sequence[str]] = ...,
# name: str = ...,
fields: Optional[Sequence[str]],
fields: Sequence[str],
name: str,
condition: Optional[Q] = ...,
deferrable: Optional[Deferrable] = ...,
Expand Down
37 changes: 37 additions & 0 deletions tests/typecheck/db/models/test_constraints.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
- case: unique_constraint_expressions
main: |
from django.db.models import Q, UniqueConstraint
from django.db.models.functions import Lower

UniqueConstraint(
Lower('name').desc(),
'category',
name='unique_lower_name_category',
)

- case: unique_constraint_fields
main: |
from django.db.models import Q, UniqueConstraint
from django.db.models.functions import Lower

UniqueConstraint(
fields=['name'],
name='unqiue_name',
)

- case: unique_constraint_expressions_fields
main: |
from django.db.models import Q, UniqueConstraint
from django.db.models.functions import Lower

UniqueConstraint(
Lower('name'),
fields=['name'],
name='unique_mess',
)
regex: true
out: |
main:4: error: No overload variant of "UniqueConstraint" matches argument types "Lower", "List\[str\]", "str"
main:4: note: Possible overload variants:
main:4: note: .*
Copy link
Member

Choose a reason for hiding this comment

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

Nice usage of regex! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice plugin, writing all the variants would have been unnecessarily verbose!

main:4: note: .*