Skip to content

Commit

Permalink
ci(ruff): Set additional rules, code fixes (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Jul 1, 2023
2 parents 6f1a723 + f77b862 commit 2aacd6f
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -4,6 +4,10 @@

<!-- Maintainers, insert changes / features for the next release here -->

### Development

- Ruff: Add additional rules, fix linting issues (#353)

## cihai 0.25.0 (2023-06-24)

_Maintenance only, no bug fixes, or new features_
Expand Down
4 changes: 1 addition & 3 deletions docs/conf.py
Expand Up @@ -176,9 +176,7 @@
autodoc_member_order = "groupwise"


def linkcode_resolve(
domain: str, info: t.Dict[str, str]
) -> t.Union[None, str]: # NOQA: C901
def linkcode_resolve(domain: str, info: t.Dict[str, str]) -> t.Union[None, str]:
"""
Determine the URL corresponding to Python object
Expand Down
6 changes: 3 additions & 3 deletions examples/variant_ts_difficulties.py
Expand Up @@ -24,7 +24,7 @@ def run(unihan_options: t.Optional[t.Dict[str, object]] = None) -> None:
print("3.7.1 bullet 4")

for char in c.unihan.with_fields(["kTraditionalVariant", "kSimplifiedVariant"]):
print("Character: {}".format(char.char))
print(f"Character: {char.char}")
trad = set(char.untagged_vars("kTraditionalVariant"))
simp = set(char.untagged_vars("kSimplifiedVariant"))
Unihan = c.sql.base.classes.Unihan
Expand All @@ -33,9 +33,9 @@ def run(unihan_options: t.Optional[t.Dict[str, object]] = None) -> None:
else:
print("Case 2 (non-idempotent)")
for trad_var in trad:
print("s2t: {}".format(trad_var))
print(f"s2t: {trad_var}")
for simp_var in simp:
print("t2s: {}".format(simp_var))
print(f"t2s: {simp_var}")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/variants.py
Expand Up @@ -7,7 +7,7 @@

def variant_list(unihan: Unihan, field: str) -> None:
for char in unihan.with_fields([field]):
print("Character: {}".format(char.char))
print(f"Character: {char.char}")
for var in char.untagged_vars(field):
print(var)

Expand Down
20 changes: 20 additions & 0 deletions pyproject.toml
Expand Up @@ -129,6 +129,26 @@ files = [
]

[tool.ruff]
target-version = "py38"
select = [
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"Q", # flake8-quotes
"PTH", # flake8-use-pathlib
"ERA", # eradicate
"SIM", # flake8-simplify
"TRY", # Trycertatops
"PERF", # Perflint
"RUF" # Ruff-specific rules
]

[tool.ruff.isort]
known-first-party = ["unihan_etl", "cihai"]
combine-as-imports = true

[tool.ruff.per-file-ignores]
"*/__init__.py" = ["F401"]
Expand Down
2 changes: 1 addition & 1 deletion src/cihai/__init__.py
@@ -1 +1 @@
from .__about__ import __version__ as __version__ # NOQA: F401
from .__about__ import __version__ as __version__
18 changes: 14 additions & 4 deletions src/cihai/_internal/config_reader.py
Expand Up @@ -12,6 +12,16 @@
FormatLiteral = t.Literal["json", "yaml"]


class ConfigFormatNotImplementedError(NotImplementedError):
def __init__(self, format: str):
return super().__init__(f"{format} not supported in configuration")


class ConfigExtensionNotImplementedError(NotImplementedError):
def __init__(self, ext: str, path: t.Union[str, pathlib.Path]):
return super().__init__(f"{ext} not supported in {path}")


class ConfigReader:
r"""Parse string data (YAML and JSON) into a dictionary.
Expand Down Expand Up @@ -46,7 +56,7 @@ def _load(format: "FormatLiteral", content: str) -> t.Dict[str, t.Any]:
elif format == "json":
return t.cast(t.Dict[str, t.Any], json.loads(content))
else:
raise NotImplementedError(f"{format} not supported in configuration")
raise NotImplementedError(format=format)

@classmethod
def load(cls, format: "FormatLiteral", content: str) -> "ConfigReader":
Expand Down Expand Up @@ -102,14 +112,14 @@ def _from_file(cls, path: pathlib.Path) -> t.Dict[str, t.Any]:
{'session_name': 'my session'}
"""
assert isinstance(path, pathlib.Path)
content = open(path).read()
content = path.open().read()

if path.suffix in [".yaml", ".yml"]:
format: "FormatLiteral" = "yaml"
elif path.suffix == ".json":
format = "json"
else:
raise NotImplementedError(f"{path.suffix} not supported in {path}")
raise ConfigExtensionNotImplementedError(ext=path.suffix, path=path)

return cls._load(
format=format,
Expand Down Expand Up @@ -184,7 +194,7 @@ def _dump(
indent=2,
)
else:
raise NotImplementedError(f"{format} not supported in config")
raise ConfigFormatNotImplementedError(format=format)

def dump(self, format: "FormatLiteral", indent: int = 2, **kwargs: t.Any) -> str:
r"""Dump via ConfigReader instance.
Expand Down
4 changes: 2 additions & 2 deletions src/cihai/_internal/types.py
Expand Up @@ -6,9 +6,9 @@
:class:`StrPath` and :class:`StrOrBytesPath` is based on `typeshed's`_.
.. _typeshed's: https://github.com/python/typeshed/blob/5df8de7/stdlib/_typeshed/__init__.pyi#L115-L118
""" # NOQA E501
from os import PathLike
"""
import typing as t
from os import PathLike

if t.TYPE_CHECKING:
from typing_extensions import TypeAlias
Expand Down
4 changes: 3 additions & 1 deletion src/cihai/config.py
Expand Up @@ -58,7 +58,9 @@ def expand_config(d: "UntypedDict", dirs: "AppDirs" = app_dirs) -> None:
if isinstance(v, dict):
expand_config(v, dirs)
if isinstance(v, str):
d[k] = os.path.expanduser(os.path.expandvars(v).format(**context))
d[k] = os.path.expanduser( # NOQA: PTH111
os.path.expandvars(v).format(**context),
)

path = pathlib.Path(t.cast(str, d[k]))
if path.exists() or any(
Expand Down
1 change: 0 additions & 1 deletion src/cihai/constants.py
Expand Up @@ -26,5 +26,4 @@
UNIHAN_CONFIG: "UntypedDict" = {
"datasets": {"unihan": "cihai.data.unihan.dataset.Unihan"},
# Turn off by default for using as a plugin example in examples/
# "plugins": {"unihan": {"variants": "cihai.data.unihan.dataset.UnihanVariants"}},
}
17 changes: 12 additions & 5 deletions src/cihai/core.py
@@ -1,12 +1,11 @@
"""Cihai core functionality."""
import inspect
import logging
import os
import pathlib
import typing as t

from cihai._internal.config_reader import ConfigReader
from cihai.data.unihan.dataset import Unihan

from unihan_etl.util import merge_dict

from . import exc, extend
Expand All @@ -17,6 +16,7 @@

if t.TYPE_CHECKING:
from typing_extensions import TypeGuard

from cihai.types import ConfigDict, UntypedDict

DS = t.TypeVar("DS", bound=t.Type[extend.Dataset])
Expand All @@ -25,6 +25,11 @@
log = logging.getLogger(__name__)


class CihaiConfigError(exc.CihaiException):
def __init__(self) -> None:
return super().__init__("Invalid exception with configuration")


def is_valid_config(config: "UntypedDict") -> "TypeGuard[ConfigDict]":
return True

Expand Down Expand Up @@ -101,12 +106,14 @@ def __init__(
expand_config(_config, app_dirs)

if not is_valid_config(config=_config):
raise exc.CihaiException("Invalid exception with configuration")
raise CihaiConfigError()

self.config = _config

if not os.path.exists(app_dirs.user_data_dir):
os.makedirs(app_dirs.user_data_dir)
user_data_dir = pathlib.Path(app_dirs.user_data_dir)

if not user_data_dir.exists():
user_data_dir.mkdir(parents=True)

#: :class:`cihai.db.Database` : Database instance
self.sql = Database(self.config)
Expand Down
11 changes: 4 additions & 7 deletions src/cihai/data/unihan/bootstrap.py
@@ -1,7 +1,7 @@
import typing as t

import sqlalchemy.sql.schema
import sqlalchemy
import sqlalchemy.sql.schema
from sqlalchemy import Column, String, Table

from unihan_etl import core as unihan
Expand Down Expand Up @@ -40,19 +40,16 @@ def bootstrap_unihan(
try:
DEFAULT_FIELDS = [f for c, f in UNIHAN_MANIFEST.items() if c in ["Unihan"]]
except Exception:
DEFAULT_FIELDS = [f for f in UNIHAN_MANIFEST.values()]
DEFAULT_FIELDS = list(UNIHAN_MANIFEST.values())


def is_bootstrapped(metadata: sqlalchemy.sql.schema.MetaData) -> bool:
"""Return True if cihai is correctly bootstrapped."""
fields = UNIHAN_FIELDS + DEFAULT_COLUMNS
if TABLE_NAME in metadata.tables.keys():
if TABLE_NAME in metadata.tables:
table = metadata.tables[TABLE_NAME]

if set(fields) == {c.name for c in table.columns}:
return True
else:
return False
return set(fields) == {c.name for c in table.columns}
else:
return False

Expand Down
1 change: 1 addition & 0 deletions src/cihai/data/unihan/dataset.py
Expand Up @@ -10,6 +10,7 @@

if t.TYPE_CHECKING:
from sqlalchemy.sql.schema import Table

from ...conversion import ParsedVars, UntaggedVars


Expand Down
1 change: 1 addition & 0 deletions src/cihai/db.py
@@ -1,5 +1,6 @@
"""Cihai core functionality."""
import typing as t

from sqlalchemy import MetaData, create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
Expand Down
8 changes: 3 additions & 5 deletions src/cihai/extend.py
Expand Up @@ -17,12 +17,13 @@
from . import utils

if t.TYPE_CHECKING:
from cihai.db import Database
from sqlalchemy.engine import Engine
from sqlalchemy.ext.automap import AutomapBase
from sqlalchemy.orm.session import Session
from sqlalchemy.sql.schema import MetaData

from cihai.db import Database

DSP = t.TypeVar("DSP", bound=t.Type["DatasetPlugin"])


Expand Down Expand Up @@ -106,10 +107,7 @@ def add_plugin(
namespace: str,
bootstrap: bool = True,
) -> None:
if isinstance(_cls, str):
cls = utils.import_string(_cls)
else:
cls = _cls
cls = utils.import_string(_cls) if isinstance(_cls, str) else _cls
setattr(self, namespace, cls())
plugin = getattr(self, namespace)

Expand Down
4 changes: 2 additions & 2 deletions src/cihai/log.py
Expand Up @@ -119,7 +119,7 @@ def template(self, record: logging.LogRecord) -> str:
Style.RESET_ALL,
" ",
]
module_funcName = [Fore.GREEN, Style.BRIGHT, "%(module)s.%(funcName)s()"]
module_funcname = [Fore.GREEN, Style.BRIGHT, "%(module)s.%(funcName)s()"]
lineno = [
Fore.BLACK,
Style.DIM,
Expand All @@ -131,7 +131,7 @@ def template(self, record: logging.LogRecord) -> str:
]

tpl = "".join(
reset + levelname + asctime + name + module_funcName + lineno + reset
reset + levelname + asctime + name + module_funcname + lineno + reset
)

return tpl
1 change: 1 addition & 0 deletions src/cihai/types.py
Expand Up @@ -5,6 +5,7 @@

if t.TYPE_CHECKING:
from typing_extensions import TypeAlias

from cihai.extend import Dataset


Expand Down
7 changes: 2 additions & 5 deletions tests/conftest.py
@@ -1,15 +1,12 @@
import os
import pathlib
import typing as t
import zipfile

import pytest

import sqlalchemy

from cihai.data.unihan.constants import UNIHAN_FILES


if t.TYPE_CHECKING:
from .types import UnihanOptions

Expand All @@ -25,8 +22,8 @@ def fixture_path(tests_path: pathlib.Path) -> pathlib.Path:


@pytest.fixture
def test_config_file(fixture_path: str) -> str:
return os.path.join(fixture_path, "test_config.yml")
def test_config_file(fixture_path: pathlib.Path) -> pathlib.Path:
return fixture_path / "test_config.yml"


@pytest.fixture
Expand Down
9 changes: 6 additions & 3 deletions tests/test_cihai.py
Expand Up @@ -5,6 +5,7 @@
"""
import typing as t

import sqlalchemy

import cihai
Expand Down Expand Up @@ -54,9 +55,11 @@ def test_unihan_options(
)
assert "Unihan" in app.sql.metadata.tables
assert app.sql.metadata.tables["Unihan"].columns
assert set(app.sql.metadata.tables["Unihan"].columns.keys()) == set(
unihan_constants.UNIHAN_FIELDS + ["ucn", "char"]
)
assert set(app.sql.metadata.tables["Unihan"].columns.keys()) == {
*unihan_constants.UNIHAN_FIELDS,
"ucn",
"char",
}
assert bootstrap.is_bootstrapped(app.sql.metadata)


Expand Down
4 changes: 3 additions & 1 deletion tests/test_config.py
@@ -1,9 +1,11 @@
import os
import pathlib
import typing as t

import pytest
from appdirs import AppDirs

from cihai.config import expand_config
import pytest

if t.TYPE_CHECKING:
from cihai.types import UntypedDict
Expand Down
1 change: 0 additions & 1 deletion tests/test_datasets.py
Expand Up @@ -11,7 +11,6 @@
import typing as t

import pytest

import sqlalchemy

from cihai import conversion
Expand Down

0 comments on commit 2aacd6f

Please sign in to comment.