Skip to content

Commit

Permalink
Update ruff config (ipython#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Dec 3, 2023
1 parent f6bd8d5 commit 7d13472
Show file tree
Hide file tree
Showing 46 changed files with 183 additions and 161 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.1
rev: 0.27.2
hooks:
- id: check-github-workflows

Expand All @@ -33,7 +33,7 @@ repos:
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.3"
rev: "v3.1.0"
hooks:
- id: prettier
types_or: [yaml, html, json]
Expand All @@ -51,7 +51,7 @@ repos:
args: ["-L", "sur,nd"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.7.0"
rev: "v1.7.1"
hooks:
- id: mypy
files: "^traitlets"
Expand All @@ -67,7 +67,7 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.1.6
hooks:
- id: ruff
types_or: [python, jupyter]
Expand All @@ -76,7 +76,7 @@ repos:
types_or: [python, jupyter]

- repo: https://github.com/scientific-python/cookie
rev: "2023.10.27"
rev: "2023.11.17"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Enhancements made

- Add option to load traitlets values from environement. [#856](https://github.com/ipython/traitlets/pull/856) ([@Carreau](https://github.com/Carreau))
- Add option to load traitlets values from environment. [#856](https://github.com/ipython/traitlets/pull/856) ([@Carreau](https://github.com/Carreau))

### Maintenance and upkeep improvements

Expand Down
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#
# All configuration values have a default; values that are commented out
# serve to show the default.
from __future__ import annotations

import os.path as osp
import shutil
Expand All @@ -22,7 +23,7 @@
HERE = osp.abspath(osp.dirname(__file__))
ROOT = osp.dirname(osp.dirname(HERE))

from traitlets import __version__, version_info
from traitlets import __version__, version_info # noqa: E402

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down
9 changes: 4 additions & 5 deletions docs/sphinxext/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#
# Original Copyright (c) 2010 Doug Hellmann. All rights reserved.
#
from __future__ import annotations

from docutils import nodes, utils
from docutils.parsers.rst.roles import set_classes
from sphinx.util.logging import getLogger # type:ignore
from sphinx.util.logging import getLogger

info = getLogger(__name__).info

Expand Down Expand Up @@ -50,8 +51,7 @@ def make_link_node(rawtext, app, type, slug, options):
prefix = "#"
if type == "pull":
prefix = "PR " + prefix
node = nodes.reference(rawtext, prefix + utils.unescape(slug), refuri=ref, **options)
return node
return nodes.reference(rawtext, prefix + utils.unescape(slug), refuri=ref, **options)


def ghissue_role(name, rawtext, text, lineno, inliner, options=None, content=None):
Expand Down Expand Up @@ -170,5 +170,4 @@ def setup(app):
app.add_role("ghcommit", ghcommit_role)
app.add_config_value("github_project_url", None, "env")

metadata = {"parallel_read_safe": True, "parallel_write_safe": True}
return metadata
return {"parallel_read_safe": True, "parallel_write_safe": True}
7 changes: 4 additions & 3 deletions examples/argcomplete_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@
If completions are not showing, you can set the environment variable ``_ARC_DEBUG=1``
to assist in debugging argcomplete. This was last checked with ``argcomplete==1.12.3``.
"""
from __future__ import annotations

import json
import os

try:
from argcomplete.completers import EnvironCompleter, SuppressCompleter
except ImportError:
EnvironCompleter = SuppressCompleter = None # type:ignore
EnvironCompleter = SuppressCompleter = None
from traitlets import Bool, Enum, Int, List, Unicode
from traitlets.config.application import Application
from traitlets.config.configurable import Configurable
Expand Down Expand Up @@ -106,8 +108,7 @@ def print(self):
if env_var not in os.environ:
if self.skip_if_missing:
continue
else:
raise KeyError(f"Environment variable not set: {env_var}")
raise KeyError(f"Environment variable not set: {env_var}")

value = os.environ[env_var]
if self.style == "posix":
Expand Down
1 change: 1 addition & 0 deletions examples/docs/aliases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of using Application aliases, for docs"""
from __future__ import annotations

from traitlets import Bool
from traitlets.config import Application, Configurable
Expand Down
3 changes: 2 additions & 1 deletion examples/docs/configs/base_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Example config used by load_config_app.py
from __future__ import annotations

c = get_config() # noqa
c = get_config() # noqa: F821
c.MyClass.name = "Harvard"
c.MyClass.ranking = 100
5 changes: 3 additions & 2 deletions examples/docs/configs/main_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Example config used by load_config_app.py
from __future__ import annotations

c = get_config() # noqa
c = get_config() # noqa: F821

# Load everything from base_config.py
load_subconfig("base_config.py") # noqa
load_subconfig("base_config.py") # noqa: F821

# Now override one of the values
c.School.name = "Caltech"
1 change: 1 addition & 0 deletions examples/docs/container.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of using container traits in Application command-line"""
from __future__ import annotations

from traitlets import Dict, Integer, List, Unicode
from traitlets.config import Application
Expand Down
1 change: 1 addition & 0 deletions examples/docs/flags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of using Application flags, for docs"""
from __future__ import annotations

from traitlets import Bool
from traitlets.config import Application, Configurable
Expand Down
1 change: 1 addition & 0 deletions examples/docs/from_string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of using TraitType.from_string, for docs"""
from __future__ import annotations

from binascii import a2b_hex

Expand Down
1 change: 1 addition & 0 deletions examples/docs/load_config_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$ ./examples/docs/load_config_app.py -c ""
The school MIT has a rank of 1.
"""
from __future__ import annotations

from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions examples/docs/multiple_apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of one application calling another"""
from __future__ import annotations

from traitlets.config import Application

Expand Down
1 change: 1 addition & 0 deletions examples/docs/subcommands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""A simple example of using Application subcommands, for docs"""
from __future__ import annotations

from traitlets.config import Application

Expand Down
6 changes: 4 additions & 2 deletions examples/myapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
When the config attribute of an Application is updated, it will fire all of
the trait's events for all of the config=True attributes.
"""
from __future__ import annotations

from traitlets import Bool, Dict, Enum, Int, List, Unicode
from traitlets.config.application import Application
from traitlets.config.configurable import Configurable
Expand Down Expand Up @@ -94,7 +96,7 @@ class MyApp(Application):
config_file = Unicode("", help="Load this config file").tag(config=True)

aliases = Dict( # type:ignore[assignment]
dict(
dict( # noqa: C408
i="Foo.i",
j="Foo.j",
name="Foo.name",
Expand All @@ -106,7 +108,7 @@ class MyApp(Application):
)

flags = Dict( # type:ignore[assignment]
dict(
dict( # noqa: C408
enable=({"Bar": {"enabled": True}}, "Enable Bar"),
disable=({"Bar": {"enabled": False}}, "Disable Bar"),
debug=({"MyApp": {"log_level": 10}}, "Set loglevel to DEBUG"),
Expand Down
1 change: 1 addition & 0 deletions examples/subcommands_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
bar
hello bob
"""
from __future__ import annotations

from traitlets import Enum, Unicode
from traitlets.config.application import Application
Expand Down
99 changes: 50 additions & 49 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ strict = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
pretty = true
show_error_context = true
show_error_codes = true
warn_unreachable = true
exclude = ["examples/docs/configs", "traitlets/tests/test_typing.py"]

Expand Down Expand Up @@ -156,58 +155,55 @@ source = ["traitlets"]
line-length = 100

[tool.ruff.lint]
select = [
"A", "B", "C", "E", "F", "FBT", "I", "N", "Q", "RUF", "S", "T",
"UP", "W", "YTT",
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
# "EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
# "PTH", # flake8-use-pathlib
"PT", # flake8-pytest-style
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"PYI", # flake8-pyi
"S", # flake8-bandit
]
ignore = [
# Allow non-abstract empty methods in abstract base classes
"B027",
# Ignore McCabe complexity
"C901",
# Allow boolean positional values in function calls, like `dict.get(... True)`
"FBT003",
# Use of `assert` detected
"S101",
# Line too long
"E501",
# Relative imports are banned
"TID252",
# Boolean ... in function definition
"FBT001", "FBT002",
# Module level import not at top of file
"E402",
# A001/A002/A003 .. is shadowing a python builtin
"A001", "A002", "A003",
# Possible hardcoded password
"S105", "S106",
# Q000 Single quotes found but double quotes preferred
"Q000",
# N806 Variable `B` in function should be lowercase
"N806",
# T201 `print` found
"T201",
# N802 Function name `CreateWellKnownSid` should be lowercase
"N802", "N803",
# C408 Unnecessary `dict` call (rewrite as a literal)
"C408",
# N801 Class name `directional_link` should use CapWords convention
"N801",
# S110 `try`-`except`-`pass` detected
"S110",
# RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
"RUF012",
# non-pep585-annotation
"UP006",
# non-pep604-annotation
"UP007",
"PLR", # Design related pylint codes
"B027", # Allow non-abstract empty methods in abstract base classes
"S101", # Use of `assert` detected
"E501", # Line too long
"S105", "S106", # Possible hardcoded password
"S110", # S110 `try`-`except`-`pass` detected
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
"UP006", # non-pep585-annotation
"UP007", # non-pep604-annotation
"ARG001", "ARG002", # Unused function argument
"RET503", # Missing explicit `return` at the end of function
"RET505", # Unnecessary `else` after `return` statement
"SIM102", # Use a single `if` statement instead of nested `if` statements
"SIM105", # Use `contextlib.suppress(ValueError)`
"SIM108", # Use ternary operator
"SIM114", # Combine `if` branches using logical `or` operator"
"PLW2901", # `for` loop variable `path` overwritten by assignment target
# https://github.com/astral-sh/ruff/issues/8796#issuecomment-1825907715
"PT001", "PT004", "PT005", "PT02", "PT009"
]
unfixable = [
# Don't touch print statements
"T201",
# Don't touch noqa lines
"RUF100",
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
# B011 Do not call assert False since python -O removes these calls
Expand All @@ -220,13 +216,18 @@ unfixable = [
# F841 Local variable `t` is assigned to but never used
# B018 Found useless expression
# S301 `pickle` and modules that wrap..."
# PGH003 Use specific rule codes when ignoring type issues
"tests/*" = ["B011", "F841", "C408", "E402", "T201", "B007", "N802", "F841",
"B018", "S301"]
"B018", "S301", "ARG", "PGH003"]
"traitlets/config/application.py" = ["T201"]
# B003 Assigning to os.environ doesn't clear the environment
"tests/config/*" = ["B003", "B018", "S301"]
# F401 `_version.__version__` imported but unused
# F403 `from .traitlets import *` used; unable to detect undefined names
"traitlets/*__init__.py" = ["F401", "F403"]
"examples/*" = ["T201"]

[tool.repo-review]
ignore = ["PY007"]
[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false
parametrize-names-type = "csv"
1 change: 1 addition & 0 deletions tests/_warnings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# From scikit-image: https://github.com/scikit-image/scikit-image/blob/c2f8c4ab123ebe5f7b827bc495625a32bb225c10/skimage/_shared/_warnings.py
# Licensed under modified BSD license
from __future__ import annotations

__all__ = ["all_warnings", "expected_warnings"]

Expand Down
Loading

0 comments on commit 7d13472

Please sign in to comment.