Skip to content

Commit

Permalink
chore(su6): more ruff rules and fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed May 31, 2023
1 parent 60a1010 commit f1714a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 19 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ build_command = "hatch build"
directory = "src"
include = []
exclude = []
coverage = 99
coverage = 100
badge = true

[tool.black]
Expand All @@ -75,7 +75,7 @@ extend-exclude = '''
'''

[tool.mypy]
python_version = "3.10"
python_version = "3.11"

# `some: int = None` looks nicer than `some: int | None = None` and pycharm still understands it
no_implicit_optional = false # I guess 'strict_optional' should be true, but disable this one because it's double!
Expand All @@ -96,14 +96,28 @@ exclude = ["venv", ".bak"]
[tool.ruff]
target-version = "py310"
line-length = 120

select = [
"F", # pyflake error
"E", # pycodestyle error
"W", # pycodestyle warning
"Q", # quotes
"A", # builtins
# "C4", # comprehensions - NO: doesn't allow dict()
# "RET", # return - NO: annoying
"SIM", # simplify
"ARG", # unused arguments
# "COM", # comma's - NO: annoying
# "PTH", # use pathlib - NO: annoying
"RUF", # ruff rules
]
unfixable = [
# Don't touch unused imports
"F401",
]

extend-ignore = [
# db.field == None should NOT be fixed to db.field is None
"E711"
"E711",
]

extend-exclude = ["*.bak/", "venv*/"]
Expand Down Expand Up @@ -145,4 +159,4 @@ add_ignore = [
[tool.pytest.ini_options]
pythonpath = [
"src",
]
]
19 changes: 8 additions & 11 deletions src/typedal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pydal
from pydal.objects import Field, Query, Row, Rows, Table

BASIC_MAPPINGS = {
BASIC_MAPPINGS: dict[type, str] = {
str: "string",
int: "integer",
bool: "boolean",
Expand Down Expand Up @@ -92,8 +92,8 @@ def __call__(self, *_args: Query, **kwargs: typing.Any) -> pydal.objects.Set:
# todo: insert etc shadowen?

@classmethod
def _build_field(cls, name: str, type: str, **kw: typing.Any) -> Field:
return Field(name, type, **{**cls.default_kwargs, **kw})
def _build_field(cls, name: str, _type: str, **kw: typing.Any) -> Field:
return Field(name, _type, **{**cls.default_kwargs, **kw})

@classmethod
def _to_field(cls, fname: str, ftype: type, **kw: typing.Any) -> Field:
Expand Down Expand Up @@ -168,7 +168,7 @@ def __getattr__(self, key: str) -> Field:


class TypedTable(Table, metaclass=TypedTableMeta): # type: ignore
id: int
id: int # noqa: 'id' has to be id since that's the db column

# set up by db.define:
__db: TypeDAL | None = None
Expand Down Expand Up @@ -224,11 +224,11 @@ def insert(cls, **fields: typing.Any) -> int:

class TypedFieldType(Field): # type: ignore
_table = "<any table>"
type: type
_type: type
kwargs: typing.Any

def __init__(self, _type: typing.Type[typing.Any], **kwargs: typing.Any) -> None:
self.type = _type
self._type = _type
self.kwargs = kwargs

def __repr__(self) -> str:
Expand All @@ -239,16 +239,13 @@ def __str__(self) -> str:
if "type" in self.kwargs:
t = self.kwargs["type"]
else:
t = self.type.__name__ if issubclass(type(self.type), type) else self.type
t = self._type.__name__ if issubclass(type(self._type), type) else self._type
return f"TypedField.{t}"

def _to_field(self, name: str, **extra_kwargs: typing.Any) -> Field:
other_kwargs = self.kwargs.copy()
other_kwargs.update(extra_kwargs)
if "type" in other_kwargs:
_type = other_kwargs.pop("type")
else:
_type = self._to_field_type(self.type)
_type = other_kwargs.pop("type", False) or self._to_field_type(self._type)

return TypeDAL._build_field(name, _type, **other_kwargs)

Expand Down

0 comments on commit f1714a3

Please sign in to comment.