Skip to content

Commit

Permalink
Switch from flake8 to ruff (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Apr 24, 2023
1 parent 355b3c2 commit d72defa
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 106 deletions.
3 changes: 1 addition & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include *.rst
include tox.ini
include pyproject.toml
include requirements-dev.txt
include requirements-flake8.txt
include Makefile
include LICENSE
include .readthedocs.yaml
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ test: ## Run tests, linting, and static typechecking
.PHONY: lint
lint: ## Lint and black reformat files
# NOTE(willkg): Make sure this matches what's in tox.ini.
black --target-version=py37 --line-length=88 src setup.py tests docs examples
tox -e py37-flake8
black src setup.py tests docs examples
tox -e py37-lint

.PHONY: clean
clean: ## Clean build artifacts
Expand Down
4 changes: 2 additions & 2 deletions examples/recipes_appconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
def parse_loglevel(value):
try:
return TEXT_TO_LOGGING_LEVEL[value.upper()]
except KeyError:
except KeyError as exc:
raise ValueError(
f'"{value}" is not a valid logging level. Try CRITICAL, ERROR, '
"WARNING, INFO, DEBUG"
)
) from exc


class AppConfig:
Expand Down
78 changes: 78 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[tool.ruff]
# Enable pycodestyle (E), pyflakes (F), and bugbear (B) rules
select = ["E", "F", "B"]

# Ignore line length violations--Black handles those
line-length = 88
ignore = ["E501"]

target-version = "py37"

src = ["src"]

[tool.ruff.flake8-quotes]
docstring-quotes = "double"


[tool.black]
line-length = 88
target-version = ["py37"]

[tool.mypy]
python_version = "3.8"
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = "configobj.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "docutils.*"
ignore_missing_imports = true


[tool.pytest.ini_options]
filterwarnings = [
"error",
"ignore:::babel[.*]",
"ignore:::jinja2[.*]",
"ignore:::yaml[.*]",
# Sphinx 4.2.0 uses distutils and it's deprecated in 3.10
"ignore::DeprecationWarning:sphinx",
]


[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py37,py38,py39,py310,py311,py37-doctest,py37-lint,py38-typecheck
[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310
3.11: py311
[testenv]
deps = -rrequirements-dev.txt
commands = pytest {posargs} tests/
[testenv:py37-doctest]
deps = -rrequirements-dev.txt
commands = pytest --doctest-modules src/
[testenv:py37-lint]
basepython = python3.7
changedir = {toxinidir}
commands =
black --check src setup.py tests docs examples
ruff src setup.py tests docs examples
[testenv:py38-typecheck]
basepython = python3.8
changedir = {toxinidir}
commands =
mypy src/everett/
"""
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ check-manifest==0.49
cogapp==3.3.0
mypy==1.1.1
pytest==7.2.2
ruff==0.0.262
tox==4.4.7
tox-gh-actions==3.1.0
twine==4.0.2
Expand Down
3 changes: 0 additions & 3 deletions requirements-flake8.txt

This file was deleted.

37 changes: 0 additions & 37 deletions setup.cfg

This file was deleted.

30 changes: 16 additions & 14 deletions src/everett/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ def get_config_for_class(cls: Type) -> Dict[str, Tuple[Option, Type]]:
"""
options = {}
for cls in reversed(cls.__mro__):
if not hasattr(cls, "Config"):
for subcls in reversed(cls.__mro__):
if not hasattr(subcls, "Config"):
continue

cls_config = cls.Config
for attr in cls_config.__dict__.keys():
subcls_config = subcls.Config
for attr in subcls_config.__dict__.keys():
if attr.startswith("__"):
continue

val = getattr(cls_config, attr)
val = getattr(subcls_config, attr)
if isinstance(val, Option):
options[attr] = (val, cls)
options[attr] = (val, subcls)
return options


Expand Down Expand Up @@ -341,8 +341,10 @@ def parse_class(val: str) -> Any:
module = importlib.import_module(module_name)
try:
return getattr(module, class_name)
except AttributeError:
raise ValueError(f"{class_name!r} is not a valid member of {qualname(module)}")
except AttributeError as exc:
raise ValueError(
f"{class_name!r} is not a valid member of {qualname(module)}"
) from exc


_DATA_SIZE_METRIC_TO_MULTIPLIER = {
Expand Down Expand Up @@ -1274,11 +1276,11 @@ def __call__(
if self.bound_component:
try:
option, cls = self.bound_component_options[key]
except KeyError:
except KeyError as exc:
if raise_error:
raise InvalidKeyError(
f"{key!r} is not a valid key for this component"
)
) from exc
return None

default = option.default
Expand Down Expand Up @@ -1326,7 +1328,7 @@ def __call__(
# Re-raise ConfigurationError and friends since that's
# what we want to be raising.
raise
except Exception:
except Exception as exc:
exc_type, exc_value, exc_traceback = sys.exc_info()
exc_type_name = exc_type.__name__ if exc_type else "None"

Expand All @@ -1339,7 +1341,7 @@ def __call__(
config_doc=self.doc,
)

raise InvalidValueError(msg, namespace, key, parser)
raise InvalidValueError(msg, namespace, key, parser) from exc

# Return the default if there is one
if default is not NO_VALUE:
Expand All @@ -1353,7 +1355,7 @@ def __call__(
# Re-raise ConfigurationError and friends since that's
# what we want to be raising.
raise
except Exception:
except Exception as exc:
# FIXME(willkg): This is a programmer error--not a user
# configuration error. We might want to denote that better.
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand All @@ -1368,7 +1370,7 @@ def __call__(
config_doc=self.doc,
)

raise InvalidValueError(msg, namespace, key, parser)
raise InvalidValueError(msg, namespace, key, parser) from exc

# No value specified and no default, so raise an error to the user
if raise_error:
Expand Down
14 changes: 7 additions & 7 deletions src/everett/sphinxext.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from sphinx.addnodes import desc_signature, pending_xref
from sphinx.directives import ObjectDescription
from sphinx.domains import Domain, ObjType
from sphinx.locale import _
from sphinx.locale import _ as gettext
from sphinx.roles import XRefRole
from sphinx.util import ws_re
from sphinx.util import logging
Expand Down Expand Up @@ -165,7 +165,7 @@ def add_target_and_index(

objects[key] = (self.env.docname, targetname)

indextext = _("%s (component)") % name
indextext = gettext("%s (component)") % name
if self.indexnode is not None:
self.indexnode["entries"].append(
("single", indextext, targetname, "", None)
Expand Down Expand Up @@ -206,7 +206,7 @@ class EverettComponent(ObjectDescription):
Field(
"options",
names=("option",),
label=_("Options"),
label=gettext("Options"),
rolename="option",
)
]
Expand Down Expand Up @@ -261,7 +261,7 @@ def add_target_and_index(
)
objects[key] = (self.env.docname, targetname)

indextext = _("%s (component)") % name
indextext = gettext("%s (component)") % name
if self.indexnode is not None:
self.indexnode["entries"].append(
("single", indextext, targetname, "", None)
Expand All @@ -282,8 +282,8 @@ class EverettDomain(Domain):
label = "Everett"

object_types = {
"component": ObjType(_("component"), "component"),
"option": ObjType(_("option"), "option"),
"component": ObjType(gettext("component"), "component"),
"option": ObjType(gettext("option"), "option"),
}
directives = {
"component": EverettComponent,
Expand Down Expand Up @@ -474,7 +474,7 @@ def extract_configuration(
options: List[Dict] = []

# Go through options and figure out relevant information
for key, (option, cls) in config.items():
for key, (option, _) in config.items():
if namespace:
namespaced_key = namespace + "_" + key
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ class Namespace:
pass

obj = Namespace()
setattr(obj, "foo", "bar")
setattr(obj, "foo_baz", "bar")
setattr(obj, "foo", "bar") # noqa
setattr(obj, "foo_baz", "bar") # noqa

coe = ConfigObjEnv(obj)
assert coe.get("foo") == "bar"
Expand Down
37 changes: 0 additions & 37 deletions tox.ini

This file was deleted.

0 comments on commit d72defa

Please sign in to comment.