From e0c9b54d6499cd208baa0c2ba158ef54b33aaf48 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Wed, 16 Nov 2022 11:33:35 +0100 Subject: [PATCH 1/3] Add Django 4.1 violation_error_message in Constraints --- django-stubs/contrib/postgres/constraints.pyi | 1 + django-stubs/db/models/constraints.pyi | 7 +++++-- tests/typecheck/db/models/test_constraints.yml | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/django-stubs/contrib/postgres/constraints.pyi b/django-stubs/contrib/postgres/constraints.pyi index dc93f0d11..c57042fd0 100644 --- a/django-stubs/contrib/postgres/constraints.pyi +++ b/django-stubs/contrib/postgres/constraints.pyi @@ -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: ... diff --git a/django-stubs/db/models/constraints.pyi b/django-stubs/db/models/constraints.pyi index ab7d94d3b..58828a5cd 100644 --- a/django-stubs/db/models/constraints.pyi +++ b/django-stubs/db/models/constraints.pyi @@ -14,7 +14,8 @@ class Deferrable(Enum): class BaseConstraint: name: str - def __init__(self, name: str) -> None: ... + violation_error_message: str | None = ... + 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: ... @@ -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, ...] @@ -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__( @@ -52,4 +54,5 @@ class UniqueConstraint(BaseConstraint): deferrable: Deferrable | None = ..., include: Sequence[str] | None = ..., opclasses: Sequence[Any] = ..., + violation_error_message: str | None = ... ) -> None: ... diff --git a/tests/typecheck/db/models/test_constraints.yml b/tests/typecheck/db/models/test_constraints.yml index 72bf38a31..3dc15d99c 100644 --- a/tests/typecheck/db/models/test_constraints.yml +++ b/tests/typecheck/db/models/test_constraints.yml @@ -35,3 +35,13 @@ main:4: note: Possible overload variants: main:4: note: .* main:4: note: .* + +- case: constraint_violation_error_message + 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', + ) From 19b48e57476777bc557f603ff3313ce26ac4657f Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 16 Nov 2022 12:06:52 +0000 Subject: [PATCH 2/3] remove test --- tests/typecheck/db/models/test_constraints.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/typecheck/db/models/test_constraints.yml b/tests/typecheck/db/models/test_constraints.yml index 3dc15d99c..72bf38a31 100644 --- a/tests/typecheck/db/models/test_constraints.yml +++ b/tests/typecheck/db/models/test_constraints.yml @@ -35,13 +35,3 @@ main:4: note: Possible overload variants: main:4: note: .* main:4: note: .* - -- case: constraint_violation_error_message - 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', - ) From 0f87990b7e48780fc0fcfa2f2c96ecd7ea0604cb Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 16 Nov 2022 12:07:32 +0000 Subject: [PATCH 3/3] always present Co-authored-by: Nikita Sobolev --- django-stubs/db/models/constraints.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-stubs/db/models/constraints.pyi b/django-stubs/db/models/constraints.pyi index 58828a5cd..f7b2d45b1 100644 --- a/django-stubs/db/models/constraints.pyi +++ b/django-stubs/db/models/constraints.pyi @@ -14,7 +14,7 @@ class Deferrable(Enum): class BaseConstraint: name: str - violation_error_message: str | None = ... + violation_error_message: str | None 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: ...