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 4.1 constraints violation_error_message #1263

Merged
merged 3 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions django-stubs/contrib/postgres/constraints.pyi
Expand Up @@ -19,4 +19,5 @@ class ExclusionConstraint(BaseConstraint):
deferrable: Deferrable | None = ...,
include: list[str] | tuple[str] | None = ...,
opclasses: list[str] | tuple[str] = ...,
violation_error_message: str | None = ...
) -> None: ...
7 changes: 5 additions & 2 deletions django-stubs/db/models/constraints.pyi
Expand Up @@ -14,7 +14,8 @@ class Deferrable(Enum):

class BaseConstraint:
name: str
def __init__(self, name: str) -> None: ...
violation_error_message: str | None = ...
adamchainz marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, name: str, violation_error_message: str | None = ...) -> None: ...
def constraint_sql(self, model: type[Model] | None, schema_editor: BaseDatabaseSchemaEditor | None) -> str: ...
def create_sql(self, model: type[Model] | None, schema_editor: BaseDatabaseSchemaEditor | None) -> str: ...
def remove_sql(self, model: type[Model] | None, schema_editor: BaseDatabaseSchemaEditor | None) -> str: ...
Expand All @@ -23,7 +24,7 @@ class BaseConstraint:

class CheckConstraint(BaseConstraint):
check: Q
def __init__(self, *, check: Q, name: str) -> None: ...
def __init__(self, *, check: Q, name: str, violation_error_message: str | None = ...) -> None: ...

class UniqueConstraint(BaseConstraint):
expressions: tuple[Combinable, ...]
Expand All @@ -41,6 +42,7 @@ class UniqueConstraint(BaseConstraint):
deferrable: Deferrable | None = ...,
include: Sequence[str] | None = ...,
opclasses: Sequence[Any] = ...,
violation_error_message: str | None = ...
) -> None: ...
@overload
def __init__(
Expand All @@ -52,4 +54,5 @@ class UniqueConstraint(BaseConstraint):
deferrable: Deferrable | None = ...,
include: Sequence[str] | None = ...,
opclasses: Sequence[Any] = ...,
violation_error_message: str | None = ...
) -> None: ...
10 changes: 10 additions & 0 deletions tests/typecheck/db/models/test_constraints.yml
Expand Up @@ -35,3 +35,13 @@
main:4: note: Possible overload variants:
main:4: note: .*
main:4: note: .*

- case: constraint_violation_error_message
Copy link
Member

Choose a reason for hiding this comment

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

We don't need tests for simple things. We only test plugins / complex overloads / etc

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree

main: |
from django.db.models import CheckConstraint, Q

CheckConstraint(
check=Q(name__startswith='a'),
name='name_starts_with_a',
violation_error_message='Name must start with a',
)
adamchainz marked this conversation as resolved.
Show resolved Hide resolved