Skip to content

Commit

Permalink
Expand docstrings with details of their API
Browse files Browse the repository at this point in the history
These functions now go into details about the arguments they take, the
errors they may raise, and the data they return.
  • Loading branch information
meshy committed Feb 13, 2024
1 parent 80677a9 commit 5475554
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/django_integrity/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def immediate(names: Sequence[str], *, using: str) -> Iterator[None]:
create and close (or roll back) a savepoint, and set and unset the constraint state.
# See Note [Deferrable constraints]
Args:
names: The names of the constraints to change.
using: The name of the database connection to use.
Raises:
NotInTransaction: When we try to change constraints outside of a transaction.
"""
set_immediate(names, using=using)
try:
Expand All @@ -68,6 +75,12 @@ def set_all_immediate(*, using: str) -> None:
Set all constraints to IMMEDIATE for the remainder of the transaction.
# See Note [Deferrable constraints]
Args:
using: The name of the database connection to use.
Raises:
NotInTransaction: When we try to change constraints outside of a transaction.
"""
if django_db.transaction.get_autocommit(using):
raise NotInTransaction
Expand All @@ -81,6 +94,13 @@ def set_immediate(names: Sequence[str], *, using: str) -> None:
Set particular constraints to IMMEDIATE for the remainder of the transaction.
# See Note [Deferrable constraints]
Args:
names: The names of the constraints to set to IMMEDIATE.
using: The name of the database connection to use.
Raises:
NotInTransaction: When we try to change constraints outside of a transaction.
"""
if django_db.transaction.get_autocommit(using):
raise NotInTransaction
Expand All @@ -101,6 +121,13 @@ def set_deferred(names: Sequence[str], *, using: str) -> None:
Set particular constraints to DEFERRED for the remainder of the transaction.
# See Note [Deferrable constraints]
Args:
names: The names of the constraints to set to DEFERRED.
using: The name of the database connection to use.
Raises:
NotInTransaction: When we try to change constraints outside of a transaction.
"""
if django_db.transaction.get_autocommit(using):
raise NotInTransaction
Expand Down Expand Up @@ -140,6 +167,18 @@ def foreign_key_constraint_name(
This means that the constraint name is not deterministic based on the model and field alone.
Django surely ought to have a public method for this, but it doesn't!
Args:
model: The model that contains the field.
field_name: The name of the field.
using: The name of the database connection to use.
Raises:
django.core.exceptions.FieldDoesNotExist: When the field is not on the model.
NotAForeignKey: When the field is not a foreign key.
Returns:
The name of the foreign key constraint.
"""
field = model._meta.get_field(field_name)

Expand Down
16 changes: 16 additions & 0 deletions src/django_integrity/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def refine_integrity_error(rules: Mapping[_Rule, Exception]) -> Iterator[None]:
Convert a generic IntegrityError into a more specific exception.
The conversion is based on a mapping of rules to exceptions.
Args:
rules: A mapping of rules to the exceptions we'll raise if they match.
Raises:
An error from the rules mapping if an IntegrityError matches a rule.
Otherwise, the original IntegrityError.
"""
try:
yield
Expand All @@ -29,6 +36,15 @@ def refine_integrity_error(rules: Mapping[_Rule, Exception]) -> Iterator[None]:
class _Rule(abc.ABC):
@abc.abstractmethod
def is_match(self, error: django_db.IntegrityError) -> bool:
"""
Determine if the rule matches the given IntegrityError.
Args:
error: The IntegrityError to check.
Returns:
True if the rule matches the error, False otherwise.
"""
...


Expand Down

0 comments on commit 5475554

Please sign in to comment.