Skip to content

Commit

Permalink
Use Ruff instead of black
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Apr 16, 2024
1 parent 38d44c8 commit 6173bf8
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 117 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ guard-%:

#---- Basic

init-dev: ## Install dependencies and create envirionment
@poetry install
lint: ## Lint
@shopt -s globstar; poetry run ruff check owlmixin/**/*.py

test: ## Unit test
format: ## Format
@shopt -s globstar; poetry run ruff format owlmixin/**/*.py

test: ## Test
@poetry run pytest -vv --doctest-modules --doctest-continue-on-failure --cov-report=xml --cov=.

ci: ## lint & format & test & test-e2e
@make lint format test

#---- Docs

_clean-docs: ## Clean documentation
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ Don't you think smart?

```bash
# Create env
$ make init-dev
$ poetry env use <path of python 3.8>
$ poetry install

# Build documentation and run server locally
$ make serve-docs
Expand Down
129 changes: 103 additions & 26 deletions owlmixin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

import inspect
import sys
from typing import TypeVar, Optional, Sequence, Iterable, List, Any
from typing import Any, Iterable, List, Optional, Sequence, TypeVar

from owlmixin import util
from owlmixin.errors import RequiredError, UnknownPropertiesError, InvalidTypeError
from owlmixin.errors import InvalidTypeError, RequiredError, UnknownPropertiesError
from owlmixin.owlcollections import TDict, TIterator, TList
from owlmixin.owlenum import OwlEnum, OwlObjectEnum
from owlmixin.transformers import (
DictTransformer,
JsonTransformer,
YamlTransformer,
ValueTransformer,
traverse_dict,
TOption,
ValueTransformer,
YamlTransformer,
)

T = TypeVar("T", bound="OwlMixin")
Expand All @@ -38,7 +36,9 @@ def assert_none(value, type_, cls, name):

def assert_types(value, types: tuple, cls, name):
if not isinstance(value, types):
raise InvalidTypeError(cls=cls, prop=name, value=value, expected=types, actual=type(value))
raise InvalidTypeError(
cls=cls, prop=name, value=value, expected=types, actual=type(value)
)


def traverse(
Expand All @@ -63,7 +63,10 @@ def traverse(
if issubclass(type_, OwlMixin):
assert_types(value, (type_, dict), cls, name)
return type_.from_dict(
value, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
value,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
if issubclass(type_, ValueTransformer):
return type_.from_value(value)
Expand All @@ -81,15 +84,25 @@ def traverse(
assert_types(value, (list,), cls, name)
return TList(
[
traverse(g_type[0], f"{name}.{i}", v, cls, force_snake_case, force_cast, restrict)
traverse(
g_type[0],
f"{name}.{i}",
v,
cls,
force_snake_case,
force_cast,
restrict,
)
for i, v in enumerate(value)
]
)
if o_type == TIterator:
assert_none(value, type_, cls, name)
assert_types(value, (Iterable,), cls, name)
return TIterator(
traverse(g_type[0], f"{name}.{i}", v, cls, force_snake_case, force_cast, restrict)
traverse(
g_type[0], f"{name}.{i}", v, cls, force_snake_case, force_cast, restrict
)
for i, v in enumerate(value)
)
if o_type == TDict:
Expand All @@ -98,7 +111,13 @@ def traverse(
return TDict(
{
k: traverse(
g_type[0], f"{name}.{k}", v, cls, force_snake_case, force_cast, restrict
g_type[0],
f"{name}.{k}",
v,
cls,
force_snake_case,
force_cast,
restrict,
)
for k, v in value.items()
}
Expand All @@ -108,7 +127,9 @@ def traverse(
# TODO: Fot `from_csvf`... need to more simple!!
if (isinstance(v, str) and v) or (not isinstance(v, str) and v is not None):
return TOption(
traverse(g_type[0], name, v, cls, force_snake_case, force_cast, restrict)
traverse(
g_type[0], name, v, cls, force_snake_case, force_cast, restrict
)
)
return TOption(None)

Expand Down Expand Up @@ -344,7 +365,10 @@ def from_optional_dict(
"""
return TOption(
cls.from_dict(
d, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
d,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
if d is not None
else None
Expand Down Expand Up @@ -382,7 +406,10 @@ def from_dicts(
return TList(
[
cls.from_dict(
d, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
d,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
for d in ds
]
Expand Down Expand Up @@ -419,7 +446,10 @@ def from_iterable_dicts(
"""
return TIterator(
cls.from_dict(
d, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
d,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
for d in ds
)
Expand Down Expand Up @@ -451,7 +481,10 @@ def from_optional_dicts(
"""
return TOption(
cls.from_dicts(
ds, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
ds,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
if ds is not None
else None
Expand Down Expand Up @@ -484,7 +517,10 @@ def from_optional_iterable_dicts(
"""
return TOption(
cls.from_iterable_dicts(
ds, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
ds,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
if ds is not None
else None
Expand Down Expand Up @@ -522,7 +558,10 @@ def from_dicts_by_key(
return TDict(
{
k: cls.from_dict(
v, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
v,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
for k, v in ds.items()
}
Expand Down Expand Up @@ -555,15 +594,23 @@ def from_optional_dicts_by_key(
"""
return TOption(
cls.from_dicts_by_key(
ds, force_snake_case=force_snake_case, force_cast=force_cast, restrict=restrict
ds,
force_snake_case=force_snake_case,
force_cast=force_cast,
restrict=restrict,
)
if ds is not None
else None
)

@classmethod
def from_json(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = False
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = False,
) -> T:
"""From json string to instance
Expand Down Expand Up @@ -626,7 +673,12 @@ def from_jsonf(

@classmethod
def from_json_to_list(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = False
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = False,
) -> TList[T]:
"""From json string to list of instance
Expand Down Expand Up @@ -657,7 +709,12 @@ def from_json_to_list(

@classmethod
def from_json_to_iterator(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = False
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = False,
) -> TIterator[T]:
"""From json string to iterable instance
Expand Down Expand Up @@ -740,7 +797,12 @@ def from_jsonf_to_iterator(

@classmethod
def from_yaml(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = True
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = True,
) -> T:
"""From yaml string to instance
Expand Down Expand Up @@ -805,7 +867,12 @@ def from_yamlf(

@classmethod
def from_yaml_to_list(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = True
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = True,
) -> TList[T]:
"""From yaml string to list of instance
Expand Down Expand Up @@ -844,7 +911,12 @@ def from_yaml_to_list(

@classmethod
def from_yaml_to_iterator(
cls, data: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = True
cls,
data: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = True,
) -> TIterator[T]:
"""From yaml string to iterable instance
Expand Down Expand Up @@ -988,7 +1060,12 @@ def from_csvf_to_iterator(

@classmethod
def from_json_url(
cls, url: str, *, force_snake_case=True, force_cast: bool = False, restrict: bool = False
cls,
url: str,
*,
force_snake_case=True,
force_cast: bool = False,
restrict: bool = False,
) -> T:
"""From url which returns json to instance
Expand Down
2 changes: 1 addition & 1 deletion owlmixin/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
from typing import List, Sequence, Any
from typing import Any, List, Sequence


class OwlMixinError(Exception):
Expand Down
Loading

0 comments on commit 6173bf8

Please sign in to comment.