Skip to content

Commit

Permalink
Merge pull request #30 from srfoster65:ruff
Browse files Browse the repository at this point in the history
Support Ruff linter
  • Loading branch information
srfoster65 authored Nov 13, 2023
2 parents 9691730 + 15450a1 commit 391d375
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 185 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: lint

on:
- push
- workflow_call

jobs:
build:
name: ruff
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PDM
uses: pdm-project/setup-pdm@v3
with:
python-version: 3.11
cache: true

- name: Install dependencies
run: pdm install -G test

- name: Run Ruff check
run: pdm run ruff check .

- name: Run Ruff format
run: pdm run ruff format .
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.formatting.provider": "none"
}
32 changes: 28 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test = [
"mypy>=1.6.1",
"types-PyYAML>=6.0.12.12",
"pydantic>=2.4.2",
"ruff>=0.1.5",
]
docs = [
"mkdocs>=1.5.3",
Expand All @@ -68,6 +69,7 @@ testpaths = [
"tests",
]


[tool.mypy]
plugins = [
"pydantic.mypy"
Expand All @@ -90,3 +92,82 @@ init_typed = true
warn_required_dynamic_aliases = true

show-error-content = true


[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
# "tests/**"
]

# Same as Black.
line-length = 120
indent-width = 4

# Assume Python 3.8
target-version = "py311"

[tool.ruff.lint]
exclude = [
"tests/**"
]
# select = [
# "A", # prevent using keywords that clobber python builtins
# "B", # bugbear: security warnings
# "E", # pycodestyle
# "F", # pyflakes
# "ISC", # implicit string concatenation
# "UP", # alert you when better syntax is available in your python version
# "RUF", # the ruff developer's own rules
# ]
select = ["ALL"]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# select = ["E4", "E7", "E9", "F"]
ignore = ["ANN002", "ANN003", "ANN101", "ANN102", "D203", "D212", "COM812", "ISC001", "D205"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.per-file-ignores]
"__init__.py" = ["D104"]


[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"


3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![tests][tests_badge]][tests_url]
[![codecov][codecov_badge]][codecov_url]
[![mypy][mypy_badge]][mypy_url]
[![Ruff][ruff_badge]][ruff_url]
[![Docs][docs_badge]][docs_url]
[![PyPI][pypi_badge]][pypi_url]
[![PyPI - License][license_badge]][license_url]
Expand Down Expand Up @@ -152,6 +153,8 @@ Please see the [documentation](https://srfoster65.github.io/arg_init/) for furth
[codecov_url]: https://codecov.io/gh/srfoster65/arg_init
[mypy_badge]: https://github.com/srfoster65/arg_init/actions/workflows/mypy.yml/badge.svg
[mypy_url]: https://github.com/srfoster65/arg_init/actions/workflows/mypy.yml
[ruff_badge]: https://github.com/srfoster65/arg_init/actions/workflows/ruff.yml/badge.svg
[ruff_url]: https://github.com/srfoster65/arg_init/actions/workflows/ruff.yml
[docs_badge]: https://github.com/srfoster65/arg_init/actions/workflows/docs.yml/badge.svg
[docs_url]: https://srfoster65.github.io/arg_init/
[pypi_badge]: https://img.shields.io/pypi/v/arg-init?logo=python&logoColor=%23cccccc
Expand Down
8 changes: 5 additions & 3 deletions src/arg_init/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# pylint: disable=missing-module-docstring

from ._class_arg_init import ClassArgInit
from ._arg_defaults import ArgDefaults
from ._class_arg_init import ClassArgInit
from ._exceptions import UnsupportedFileFormatError
from ._function_arg_init import FunctionArgInit
from ._priority import (
Priority,
ARG_PRIORITY,
CONFIG_PRIORITY,
ENV_PRIORITY,
ARG_PRIORITY,
Priority,
)

# External API
Expand All @@ -19,4 +20,5 @@
"CONFIG_PRIORITY",
"ENV_PRIORITY",
"ARG_PRIORITY",
"UnsupportedFileFormatError",
]
12 changes: 6 additions & 6 deletions src/arg_init/_aliases.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
mypy type aliases
"""
"""mypy type aliases."""

from typing import Any, Optional, Callable
from collections.abc import Callable
from typing import Any

from ._arg_defaults import ArgDefaults
from ._priority import Priority

Defaults = Optional[list[ArgDefaults]]
Priorities= tuple[Priority, Priority, Priority, Priority]
ClassCallback = Callable[[Any], None]
Defaults = list[ArgDefaults] | None
LoaderCallback = Callable[[Any], dict[Any, Any]]
Priorities = tuple[Priority, Priority, Priority, Priority]
27 changes: 11 additions & 16 deletions src/arg_init/_arg.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
"""
Data Class used to customise ArgInit behaviour
"""
"""Class to represent an Argument."""

from typing import Any
import logging
from typing import Any

from ._aliases import Priorities
from ._priority import Priority
from ._values import Values

logger = logging.getLogger(__name__)
# Typing aliases
Priorities = tuple[Priority, Priority, Priority, Priority]


class Arg:
"""Class to represent argument attributes."""

_mapping = {
_mapping = { # noqa: RUF012
Priority.CONFIG: "config",
Priority.ENV: "env",
Priority.ARG: "arg",
Expand All @@ -36,7 +33,7 @@ def __init__(
self._values = values
self._value = None

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
"""When testing for equality, test only the value attribute."""
return self.value == other

Expand All @@ -61,29 +58,27 @@ def name(self) -> str:
return self._name

@property
def value(self) -> Any:
def value(self) -> object | None:
"""Resolved value of Arg."""
return self._value

@property
def env_name(self) -> str | None:
"""env attribute."""
"""Env attribute."""
return self._env_name

@property
def config_name(self) -> str | None:
"""env attribute."""
"""Config_name attribute."""
return self._config_name

@property
def values(self) -> Values | None:
"""Values to use when resolving Arg."""
return self._values

def resolve(self, name: str, priority_order: Priorities) -> Any:
"""
Resolve the value Arg using the selected priority system.
"""
def resolve(self, name: str, priority_order: Priorities) -> object | None:
"""Resolve the value Arg using the selected priority system."""
logger.debug("Resolving value for %s", repr(self))
for priority in priority_order:
logger.debug("Checking %s value", priority)
Expand All @@ -94,5 +89,5 @@ def resolve(self, name: str, priority_order: Priorities) -> Any:
break
return self

def _get_value(self, priority: Priority) -> Any:
def _get_value(self, priority: Priority) -> Any | None: # noqa: ANN401
return getattr(self._values, self._mapping[priority])
16 changes: 3 additions & 13 deletions src/arg_init/_arg_defaults.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
"""
Dataclass torepresent argument defaults that may be overridden
on a per argument basis.
"""
"""Dataclass torepresent argument defaults that may be overridden on a per argument basis."""

from dataclasses import dataclass
from typing import Any


@dataclass
class ArgDefaults:
"""
Dataclass to represent argument defaults that may be overridden
on a per argument basis.
"""
"""Dataclass to represent argument defaults that may be overridden on a per argument basis."""

name: str
default_value: Any | None = None
alt_name: str | None = None

def __repr__(self) -> str:
return (
f"<ArgDefaults(name={self.name}, "
f"default_value={self.default_value}, "
f"alt_name={self.alt_name})>"
)
return f"<ArgDefaults(name={self.name}, default_value={self.default_value}, alt_name={self.alt_name})>"
Loading

0 comments on commit 391d375

Please sign in to comment.