lhenault/simpleAI

Styling configuration

Nintorac opened this issue · 1 comments

Better to automate styling and reduce toil.

I would suggest ruff for import sorting and pep compliance and black for general styling (ruff is WIP for black functionality so when that is done then black can be removed)

as a starting point for the configuration I have stolen this from dagster .

As part of this commit I would also suggest we move the pyproject.toml to the root dir. This will allow ruff + black to pick up the config for entire repo, it should also have the nice side effect of allowing pip install git@github.com:lhenault/simpleAI.git

# ########################
# ##### BLACK
# ########################

# [Docs root]
#   https://black.readthedocs.io/en/stable/
# [Config option reference]
#   https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#command-line-options

[tool.black]
line-length = 100

# Enable "preview" mode-- this adds style rules likely to be incorporated into black's next major
# release. The reason for enabling this, as of 2022-12-04, is to turn on formatting of long string
# literals.
preview = true

# Black will refuse to run if it's not this version.
required-version = "22.12.0"

# Ensure black's output will be compatible with all listed versions.
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']

# ########################
# ##### RUFF
# ########################

# [Docs root]
#   https://github.com/charliermarsh/ruff#ruff
# [Config option reference]
#   https://github.com/charliermarsh/ruff#reference
#
# As of 2022-12-05, the entire documentation of Ruff is in its very long
# README.

[tool.ruff]

ignore = [

  "D101",
  "D102",
  "D103",
  "D104",
  "D105",
  "D106",
  "D107",

  # (docstring imperative mood) Overly restrictive.
  "D401",

  # (line too long): This fires for comments, which black won't wrap.
  # Disabling until there is an autoformat solution available for comments.
  "E501",

  # (no type comparison): There are a few places where we use `== type(None)` which are more clear
  # than the equivalent `isinstance` check.
  'E721',

  # (bare exception): There are many places where we want to catch a maximally generic exception.
  'E722',

  # (no assign lambda): existing code assigns lambdas in a few places. With black formatting
  # requiring extra empty lines between defs, disallowing lambda assignment can make code less
  # readable.
  "E731",

  # (no concatenation) Existing codebase has many concatentations, no reason to disallow them.
  "RUF005",

  ##### TEMPORARY DISABLES

  # (assorted docstring rules) There are too many violations of these to enable
  # right now, but we should enable after fixing the violations.
  "D200",  # (one-line docstring should fit)
  "D205",  # (blank line after summary)
  "D417",  # (missing arg in docstring)

]

# Match black. Note that this also checks comment line length, but black does not format comments.
line-length = 100

# By default, ruff only uses all "E" (pycodestyle) and "F" (pyflakes) rules.
# Here we append to the defaults.
select = [

  # (flake8-builtins) detect shadowing of python builtin symbols by variables and arguments.
  # Attributes are OK (which is why A003) is not included here.
  "A001",
  "A002",

  # (useless expression): Expressions that aren't assigned to anything are typically bugs.
  "B018",

  # (pydocstyle) Docstring-related rules. A large subset of these are ignored by the
  # "convention=google" setting, we set under tool.ruff.pydocstyle.
  "D",

  # (pycodestyle) use all pycodestyle rules
  "E",

  # (pyflakes) use all pyflakes rules
  "F",

  # (isort) detect improperly sorted imports
  "I001",

  # (pylint) use all pylint rules from categories "Convention", "Error", and "Warning" (ruff
  # currently implements only a subset of pylint's rules)
  "PLE",
  "PLW",

  # (no commented out code) keep commented out code blocks out of the codebase
  # "ERA001",

  # (ruff-specific) Enable all ruff-specific checks (i.e. not ports of
  # functionality from an existing linter).
  "RUF",

  # (private member access) Flag access to `_`-prefixed symbols. By default the various special
  # methods on `NamedTuple` are ignored (e.g. `_replace`).
  "SLF001",

  # (disallow print statements) keep debugging statements out of the codebase
  "T20",

  # (f-strings) use f-strings instead of .format()
  "UP032",

  # (invalid escape sequence) flag errant backslashes
  "W605",
]

# Fail if Ruff is not running this version.
required-version = "0.0.255"

[tool.ruff.flake8-builtins]

# We use `id` in many places and almost never want to use the python builtin.
builtins-ignorelist = ["id"]

[tool.ruff.isort]

# Combine multiple `from foo import bar as baz` statements with the same source
# (`foo`) into a single statement.
combine-as-imports = true

# Imports of the form `from foo import bar as baz` show one `import bar as baz`
# per line. Useful for __init__.py files that just re-export symbols.
force-wrap-aliases = true

[tool.ruff.per-file-ignores]

# Don't format docstrings in alembic migrations.
"**/alembic/versions/*.py" = ["D"]


[tool.ruff.pydocstyle]

# Enforce google-style docstrings. This is equivalent to ignoring a large number of pydocstyle (D)
# rules incompatible with google-style docstrings. See:
# https://google.github.io/styleguide/pyguide.html#383-functions-and-methods
convention = "google"

Better to automate styling and reduce toil.

Completely agree, thanks for this. Also I've only heard great things about Ruff, so good pick.

Moving forward, the config "stolen" from dagster can be a good starting point, albeit we might want to change / remove a few parts that aren't relevant so far to this project. For instance this bit might be removed:

[tool.ruff.flake8-builtins]

# We use `id` in many places and almost never want to use the python builtin.
builtins-ignorelist = ["id"]

As part of this commit I would also suggest we move the pyproject.toml to the root dir.

LGTM