Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Oct 21, 2023
1 parent 622a20e commit 5faf960
Show file tree
Hide file tree
Showing 36 changed files with 567 additions and 438 deletions.
11 changes: 11 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[flake8]
max-complexity = 20
max-line-length = 120
exclude = ~*
ignore = E401,W391,E128,E261,E731,Q000,W504,W606,W503,E203
;putty-ignore =
; tests/test_choice_as_instance.py : E501

per-file-ignores =
*/__init__.py:F401,F403
*/migrations/*:E501
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.11", "3.12" ]
python-version: ["3.9", "3.11"]
django-version: [ "3.2", "4.2" ]
env:
PY_VER: ${{ matrix.python-version}}
Expand Down
12 changes: 12 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[isort]
profile = black
;combine_as_imports = true
;default_section = THIRDPARTY
;include_trailing_comma = true
;line_length=80
;known_future_library=future,pies
;known_standard_library = six
;known_third_party = django
;known_first_party = strategy_field, demoproject.demoapp
;multi_line_output = 0
;sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ DJANGO_SETTINGS_MODULE=demoproject.settings
django_find_project = false
norecursedirs = .tox .venv
python_files=tests/test_*.py
log_print = false
log_cli = false
addopts =
-rs
Expand Down
39 changes: 19 additions & 20 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
[isort]
combine_as_imports = true
default_section = THIRDPARTY
include_trailing_comma = true
line_length=80
known_future_library=future,pies
known_standard_library = six
known_third_party = django
known_first_party = strategy_field, demoproject.demoapp
multi_line_output = 0
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER

[bdist_wheel]
universal=1

Expand All @@ -28,11 +16,22 @@ ignore =
*.sqlite
.tox/*

[flake8]
max-complexity = 12
max-line-length = 110
exclude = .tox,migrations,.git,docs,diff_match_patch.py, deploy/**,settings,~*
ignore = E401,W391,E128,E261,E731
putty-ignore =
; tests/test_choice_as_instance.py : E501
tests/test_multiple.py : E501
[black]
line-length = 120
include = '\.pyi?$'
exclude = .git
;/(
; \.git
; | \.hg
; | \.mypy_cache
; | \.tox
; | \.venv
; | venv
; | _build
; | buck-out
; | build
; | dist
; | migrations
; | snapshots
;)/
;'''
2 changes: 1 addition & 1 deletion src/strategy_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NAME = "django-strategy-field"
VERSION = __version__ = "3.0.0"
VERSION = __version__ = "3.1.0"
5 changes: 3 additions & 2 deletions src/strategy_field/contrib/drf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging

from django.core.validators import BaseValidator
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from strategy_field.fields import ClassnameValidator
from strategy_field.utils import fqn, import_by_name
from ..fields import ClassnameValidator
from ..utils import fqn, import_by_name

logger = logging.getLogger(__name__)

Expand Down
18 changes: 6 additions & 12 deletions src/strategy_field/fields.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import logging
from inspect import isclass

from django import forms
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.fields import BLANK_CHOICE_DASH, NOT_PROVIDED
from django.db.models.lookups import Contains, IContains, In
from django.utils.text import capfirst
from inspect import isclass

from strategy_field.exceptions import StrategyClassError, StrategyNameError
from strategy_field.forms import (StrategyFormField,
StrategyMultipleChoiceFormField,)
from strategy_field.utils import fqn, get_class, stringify
from strategy_field.validators import ClassnameValidator, RegistryValidator
from .exceptions import StrategyClassError, StrategyNameError
from .forms import StrategyFormField, StrategyMultipleChoiceFormField
from .utils import fqn, get_class, stringify
from .validators import ClassnameValidator, RegistryValidator

NOCONTEXT = object()

Expand Down Expand Up @@ -123,12 +123,6 @@ def __init__(self, *args, **kwargs):
if self.registry:
self.validators.append(RegistryValidator(self.registry))

def __str__(self):
return "-oooooo"

def __repr__(self):
return "-oooooo"

def contribute_to_class(
self, cls, name, private_only=False, virtual_only=NOT_PROVIDED
):
Expand Down
2 changes: 1 addition & 1 deletion src/strategy_field/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.exceptions import ValidationError
from django.forms.fields import ChoiceField, TypedMultipleChoiceField

from strategy_field.utils import fqn, stringify
from .utils import fqn, stringify


class StrategyFormField(ChoiceField):
Expand Down
21 changes: 11 additions & 10 deletions src/strategy_field/registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from django.utils.functional import cached_property
from inspect import isclass

from django.utils.functional import cached_property

from .utils import fqn, get_attr, get_display_string, import_by_name # noqa

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -71,12 +72,12 @@ def __contains__(self, y):
return False
return super().__contains__(y)

def get_class(self, value):
if not value:
return value
elif isinstance(value, str):
return import_by_name(value)
elif isclass(value):
return value
else:
return type(value)
# def get_class(self, value):
# if not value:
# return value
# elif isinstance(value, str):
# return import_by_name(value)
# elif isclass(value):
# return value
# else:
# return type(value)
6 changes: 3 additions & 3 deletions src/strategy_field/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import importlib
import logging
import types
from django.utils.module_loading import import_string
from inspect import isclass

from django.utils.module_loading import import_string

from . import config
from .exceptions import (StrategyAttributeError, StrategyClassError,
StrategyNameError,)
from .exceptions import StrategyAttributeError, StrategyClassError, StrategyNameError

logger = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions src/strategy_field/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.utils.deconstruct import deconstructible
from django.utils.translation import gettext_lazy as _

from strategy_field.exceptions import StrategyNameError
from strategy_field.utils import get_class
from .exceptions import StrategyNameError
from .utils import get_class


@deconstructible
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def democustommodel():
def demo_multiplecustom_model():
from demoproject.demoapp.models import DemoMultipleCustomModel, Strategy
from strategy_field.utils import fqn

return DemoMultipleCustomModel.objects.get_or_create(sender=[fqn(Strategy)])[0]


Expand All @@ -51,7 +52,7 @@ def demo_multiple_model():
return DemoMultipleModel.objects.get_or_create(sender=[Sender1])[0]


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def webapp(django_app):
return django_app
# import django_webtest
Expand All @@ -60,4 +61,6 @@ def webapp(django_app):
# wtm.csrf_checks = False
# wtm._patch_settings()
# request.addfinalizer(wtm._unpatch_settings)


# return django_webtest.DjangoTestApp()
2 changes: 1 addition & 1 deletion tests/demo/demoproject/demoapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'demoproject.demoapp.apps.DemoConfig'
default_app_config = "demoproject.demoapp.apps.DemoConfig"
4 changes: 2 additions & 2 deletions tests/demo/demoproject/demoapp/admin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from django.contrib import admin
from django.forms import ModelForm, TextInput

from strategy_field.utils import fqn

from .models import (
DemoAllModel,
DemoCustomModel,
DemoModel,
DemoModelCallableDefault,
DemoModelDefault,
DemoModelNone,
DemoModelProxy,
DemoMultipleCustomModel,
DemoMultipleModel,
DemoModelNone,
)


Expand Down
8 changes: 3 additions & 5 deletions tests/demo/demoproject/demoapp/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import logging

from demoproject.demoapp.models import DemoModelNone, DemoMultipleModel, registry
from rest_framework import serializers
from rest_framework.viewsets import ModelViewSet

from demoproject.demoapp.models import (DemoModelNone, DemoMultipleModel,
registry,)
from strategy_field.contrib.drf import (DrfMultipleStrategyField,
DrfStrategyField,)
from strategy_field.contrib.drf import DrfMultipleStrategyField, DrfStrategyField

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion tests/demo/demoproject/demoapp/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class DemoConfig(AppConfig):
name = 'demoproject.demoapp'
name = "demoproject.demoapp"
# verbose_name = "demoapp"
35 changes: 18 additions & 17 deletions tests/demo/demoproject/demoapp/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import six

import logging

import six
from django.core.mail.backends.base import BaseEmailBackend
from django.db import models

from strategy_field.fields import (MultipleStrategyClassField,
MultipleStrategyField, StrategyClassField,
StrategyField, )
from strategy_field.fields import (
MultipleStrategyClassField,
MultipleStrategyField,
StrategyClassField,
StrategyField,
)
from strategy_field.registry import Registry
from strategy_field.utils import fqn, import_by_name

Expand Down Expand Up @@ -42,7 +44,7 @@ class SenderWrong:


class AbstractStrategy:
def __init__(self, context, label=''):
def __init__(self, context, label=""):
if not context:
raise ValueError("Invalid context for strategy ({})".format(context))
self.context = context
Expand All @@ -51,8 +53,9 @@ def __init__(self, context, label=''):
def __str__(self):
return "oooooo"


class Strategy(AbstractStrategy):
label = 'strategy'
label = "strategy"
none = None

@classmethod
Expand All @@ -68,7 +71,7 @@ class StrategyRegistry(Registry):
def deserialize(self, value, obj=None):
ret = []
if isinstance(value, six.string_types):
value = value.split(',')
value = value.split(",")
for v in value:
if isinstance(v, six.string_types):
v = import_by_name(v)
Expand Down Expand Up @@ -110,19 +113,17 @@ class DemoModelNone(models.Model):


class DemoModelDefault(models.Model):
sender = StrategyClassField(null=True,
registry=registry,
default='demoproject.demoapp.models.Sender1')
sender = StrategyClassField(
null=True, registry=registry, default="demoproject.demoapp.models.Sender1"
)


def cc():
return 'demoproject.demoapp.models.Sender1'
return "demoproject.demoapp.models.Sender1"


class DemoModelCallableDefault(models.Model):
sender = StrategyClassField(registry=registry, null=True,
default=cc
)
sender = StrategyClassField(registry=registry, null=True, default=cc)


class DemoModelProxy(DemoModel):
Expand All @@ -149,7 +150,7 @@ class DemoModelContext(models.Model):
# funny code. just for tests
def factory(klass, context):
if issubclass(klass, BaseEmailBackend):
return klass(file_path='')
return klass(file_path="")
return klass()


Expand Down
Loading

0 comments on commit 5faf960

Please sign in to comment.