Skip to content

Commit ca653bf

Browse files
committed
Enable isort + pytest (#11)
Enable pytest
1 parent d9c9564 commit ca653bf

35 files changed

+350
-96
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ repos:
3434
- repo: https://github.com/python-poetry/poetry
3535
rev: 1.6.1
3636
hooks:
37-
- id: poetry-check
38-
- id: poetry-lock
39-
- id: poetry-export
37+
- id: poetry-check
38+
additional_dependencies:
39+
- poetry-plugin-sort==0.2.0
40+
# FIXME: poetry lock export more platform on the CI
41+
# - id: poetry-lock
42+
# args: ["--no-update"]
43+
- id: poetry-export
4044

4145
- repo: https://github.com/astral-sh/ruff-pre-commit
42-
rev: v0.0.291
46+
rev: v0.1.9
4347
hooks:
4448
- id: ruff-format
4549
- id: ruff

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ on a model instance with a protected FSMField will cause an exception.
153153

154154
``source`` parameter accepts a list of states, or an individual state or ``django_fsm.State`` implementation.
155155

156-
You can use ``*`` for ``source`` to allow switching to ``target`` from any state.
156+
You can use ``*`` for ``source`` to allow switching to ``target`` from any state.
157157

158158
You can use ``+`` for ``source`` to allow switching to ``target`` from any state excluding ``target`` state.
159159

@@ -163,7 +163,7 @@ You can use ``+`` for ``source`` to allow switching to ``target`` from any state
163163
``target`` state parameter could point to a specific state or ``django_fsm.State`` implementation
164164

165165
.. code:: python
166-
166+
167167
from django_fsm import FSMField, transition, RETURN_VALUE, GET_STATE
168168
@transition(field=state,
169169
source='*',

django_fsm/__init__.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
"""
22
State tracking functionality for django models
33
"""
4+
from __future__ import annotations
5+
46
import inspect
5-
from functools import partialmethod, wraps
7+
from functools import partialmethod
8+
from functools import wraps
69

7-
import django
810
from django.apps import apps as django_apps
911
from django.db import models
1012
from django.db.models import Field
1113
from django.db.models.query_utils import DeferredAttribute
1214
from django.db.models.signals import class_prepared
1315

14-
from django_fsm.signals import pre_transition, post_transition
15-
16+
from django_fsm.signals import post_transition
17+
from django_fsm.signals import pre_transition
1618

1719
__all__ = [
1820
"TransitionNotAllowed",
@@ -251,26 +253,9 @@ def deconstruct(self):
251253
return name, path, args, kwargs
252254

253255
def get_state(self, instance):
254-
# The state field may be deferred. We delegate the logic of figuring
255-
# this out and loading the deferred field on-demand to Django's
256-
# built-in DeferredAttribute class. DeferredAttribute's instantiation
257-
# signature changed over time, so we need to check Django version
258-
# before proceeding to call DeferredAttribute. An alternative to this
259-
# would be copying the latest implementation of DeferredAttribute to
260-
# django_fsm, but this comes with the added responsibility of keeping
261-
# the copied code up to date.
262-
if django.VERSION[:3] >= (3, 0, 0):
263-
return DeferredAttribute(self).__get__(instance)
264-
elif django.VERSION[:3] >= (2, 1, 0):
265-
return DeferredAttribute(self.name).__get__(instance)
266-
elif django.VERSION[:3] >= (1, 10, 0):
267-
return DeferredAttribute(self.name, model=None).__get__(instance)
268-
else:
269-
# The field was either not deferred (in which case we can return it
270-
# right away) or ir was, but we are running on an unknown version
271-
# of Django and we do not know the appropriate DeferredAttribute
272-
# interface, and accessing the field will raise KeyError.
273-
return instance.__dict__[self.name]
256+
# The state field may be deferred. We delegate the logic of figuring this out
257+
# and loading the deferred field on-demand to Django's built-in DeferredAttribute class.
258+
return DeferredAttribute(self).__get__(instance)
274259

275260
def set_state(self, instance, state):
276261
instance.__dict__[self.name] = state
@@ -435,14 +420,15 @@ def set_state(self, instance, state):
435420
instance.__dict__[self.attname] = self.to_python(state)
436421

437422

438-
class FSMModelMixin(object):
423+
class FSMModelMixin:
439424
"""
440425
Mixin that allows refresh_from_db for models with fsm protected fields
441426
"""
442427

443428
def _get_protected_fsm_fields(self):
444429
def is_fsm_and_protected(f):
445430
return isinstance(f, FSMFieldMixin) and f.protected
431+
446432
protected_fields = filter(is_fsm_and_protected, self._meta.concrete_fields)
447433
return {f.attname for f in protected_fields}
448434

@@ -455,13 +441,13 @@ def refresh_from_db(self, *args, **kwargs):
455441
protected_fields = self._get_protected_fsm_fields()
456442
skipped_fields = deferred_fields.union(protected_fields)
457443

458-
fields = [f.attname for f in self._meta.concrete_fields
459-
if f.attname not in skipped_fields]
444+
fields = [f.attname for f in self._meta.concrete_fields if f.attname not in skipped_fields]
445+
446+
kwargs["fields"] = fields
447+
super().refresh_from_db(*args, **kwargs)
460448

461-
kwargs['fields'] = fields
462-
super(FSMModelMixin, self).refresh_from_db(*args, **kwargs)
463449

464-
class ConcurrentTransitionMixin(object):
450+
class ConcurrentTransitionMixin:
465451
"""
466452
Protects a Model from undesirable effects caused by concurrently executed transitions,
467453
e.g. running the same transition multiple times at the same time, or running different

django_fsm/management/commands/graph_transitions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import graphviz
1+
from __future__ import annotations
2+
23
from itertools import chain
34

5+
import graphviz
46
from django.apps import apps
57
from django.core.management.base import BaseCommand
68
from django.utils.encoding import force_str
79

8-
from django_fsm import FSMFieldMixin, GET_STATE, RETURN_VALUE
10+
from django_fsm import GET_STATE
11+
from django_fsm import RETURN_VALUE
12+
from django_fsm import FSMFieldMixin
913

1014

1115
def all_fsm_fields_data(model):

django_fsm/signals.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from django.dispatch import Signal
24

35
pre_transition = Signal()

django_fsm/tests/test_abstract_inheritance.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from __future__ import annotations
2+
13
from django.db import models
24
from django.test import TestCase
35

4-
from django_fsm import FSMField, transition, can_proceed
6+
from django_fsm import FSMField
7+
from django_fsm import can_proceed
8+
from django_fsm import transition
59

610

711
class BaseAbstractModel(models.Model):

django_fsm/tests/test_basic_transitions.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
from __future__ import annotations
2+
13
from django.db import models
24
from django.test import TestCase
35

4-
from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed, Transition
5-
from django_fsm.signals import pre_transition, post_transition
6+
from django_fsm import FSMField
7+
from django_fsm import Transition
8+
from django_fsm import TransitionNotAllowed
9+
from django_fsm import can_proceed
10+
from django_fsm import transition
11+
from django_fsm.signals import post_transition
12+
from django_fsm.signals import pre_transition
613

714

815
class BlogPost(models.Model):

django_fsm/tests/test_conditions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
from __future__ import annotations
2+
13
from django.db import models
24
from django.test import TestCase
3-
from django_fsm import FSMField, TransitionNotAllowed, transition, can_proceed
5+
6+
from django_fsm import FSMField
7+
from django_fsm import TransitionNotAllowed
8+
from django_fsm import can_proceed
9+
from django_fsm import transition
410

511

612
def condition_func(instance):

django_fsm/tests/test_integer_field.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
from __future__ import annotations
2+
13
from django.db import models
24
from django.test import TestCase
3-
from django_fsm import FSMIntegerField, TransitionNotAllowed, transition
5+
6+
from django_fsm import FSMIntegerField
7+
from django_fsm import TransitionNotAllowed
8+
from django_fsm import transition
49

510

611
class BlogPostStateEnum:

django_fsm/tests/test_key_field.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from __future__ import annotations
2+
13
from django.db import models
24
from django.test import TestCase
3-
from django_fsm import FSMKeyField, TransitionNotAllowed, transition, can_proceed
45

6+
from django_fsm import FSMKeyField
7+
from django_fsm import TransitionNotAllowed
8+
from django_fsm import can_proceed
9+
from django_fsm import transition
510

611
FK_AVAILABLE_STATES = (
712
("New", "_NEW_"),

0 commit comments

Comments
 (0)