Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff config file #758

Closed
Andrei-Aksionov opened this issue Nov 20, 2023 · 3 comments
Closed

Ruff config file #758

Andrei-Aksionov opened this issue Nov 20, 2023 · 3 comments

Comments

@Andrei-Aksionov
Copy link
Contributor

Andrei-Aksionov commented Nov 20, 2023

Hi there 👋

Core maintainer of the repo decided for simplicity to not have a linter config, so every now and then something like this CLI command is applyed:

ruff check * \
    --select E,W,F,S \
    --extend-select ANN001,ANN201,ANN205,ANN206,ARG001,C4,I001,PT,RET,SIM \
    --ignore C408,E402,E501,E731,PT004,PT007,S101,S108,S113,S310,S603 \
    --per-file-ignores "*/**/__init__.py":I001,"*/**/__init__.py":F401,"tests/*":ANN,"*/**/unsloth/kernels/*":ALL \
    --line-length 120 \
    --target-version py38 \
    --fix-only; \
black \
    -l120 \
    -C \
    --preview \
    --target-version py38 \
    config_hub eval extensions litgpt tests \
    --exclude unsloth/kernels

If someone wants to apply changes inside, let's say VSCode, then this config should match the CLI command above:

# https://beta.ruff.rs/docs/rules/

target-version = "py38"

lint.select = [
  "ANN", # flake8-annotations
  "ARG001", # Unused function argument
  "C4", # flake8-comprehensions
  "E", # pycodestyle Error
  "F", # PyFlakes
  "I001", # isort: unsorted-imports
  "PT", # flake8-pytest-style
  "RET", # flake8-return
  "S", # flake8-bandit
  "SIM", # flake8-simplify
  "W", # pycodestyle Warning
]

lint.ignore = [
  "ANN101", # Missing type annotation for self in method
  "ANN102", # Missing type annotation for cls
  "ANN202", # Missing return type annotation for private function
  "ANN204", # Missing return type annotation for special method `__init__`
  "ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `value`
  "C408", # Unnecessary {obj_type} call (rewrite as a literal)
  "E402", # Module level import not at top of file
  "E501", # Line too long ({width} > {limit} characters)
  "E731", # Do not assign a lambda expression, use a def
  "PT004", # Fixture {function} does not return anything, add leading underscore
  "PT007", # Wrong values type in @pytest.mark.parametrize expected {values} of {row}
  "S101", # assert detected
  "S108", # Probable insecure usage of temporary file or directory: "{}"
  "S113", # Probable use of requests call without timeout
  "S310", # Audit URL open for permitted schemes. Allowing use of file: or custom schemes is often unexpected.
  "S603", # subprocess call: check for execution of untrusted input
]

line-length = 120

[lint.per-file-ignores]
"*/**/unsloth/kernels/*" = [
  "ALL",
]
"__init__.py" = [
  "I001", # isort: unsorted-imports
  "F401", # imported but unused
]
"conftest.py" = [
  "ANN001", # Missing type annotation for self in method
  "ANN201", # Missing return type annotation for public function {name}
  "D102", # Missing docstring in public method
  "S101", # assert detected
]
"test_*.py" = [
  "ANN", # Type annotations
  "D102", # Missing docstring in public method
  "S101", # assert detected
]

Just put it inside ruff.toml file and don't forget to add it to global .gitignore file.

Important

Sometimes Ruff/Black extensions for VSCode have a different view on how to apply code style changes in comparison to CLI command.
Remember that CLI command always supersedes VSCode extension!

@carmocca
Copy link
Contributor

carmocca commented Nov 20, 2023

If you can find a way to configure pre-commit/ruff to do this automatically on PRs without adding configuration files at the root, I would accept it.

This means no .pre-commit-config.yaml or pyproject.toml

@Andrei-Aksionov
Copy link
Contributor Author

You mean only .github folder is allowed?

Sounds suspiciously like a challenge 🤔.

@Andrei-Aksionov
Copy link
Contributor Author

Andrei-Aksionov commented Dec 19, 2023

This is for anyone like me who forgets to apply CLI command to format the code.
The problem becomes even harder since there is a discrepancy between CLI and VSCode's Black extension.
In addition, Black pre-commit hook also outputs a different to CLI command formatted code.
Note: VSCode extension and the hook output the same formatted code.
So I cannot rely on my memory, nor on VSCode or pre-commit hooks. Or can I? 🤔

Since the CLI command is the source of truth, one can force pre-commit to run the CLI command instead of hooks.
.pre-commit-config.yaml file should be like this:

repos:
  # default hooks
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: end-of-file-fixer
      - id: trailing-whitespace
      - id: check-yaml

  # Ruff
  - repo: local
    hooks:
    - id: ruff-CLI
      name: ruff-CLI
      entry: bash -c "ruff check * --select E,W,F,S --extend-select C4,SIM,RET,PT,I001,ANN001,ANN201,ANN205,ANN206,ARG001 --ignore E501,E731,S108,S101,S113,S603,PT007,S310,E402,PT004,C408 --per-file-ignores "*/**/__init__.py":I001,"*/**/__init__.py":F401,"tests/*":ANN,"*/**/unsloth/kernels/*":ALL --line-length 120 --target-version py38 --fix-only"
      language: system
      pass_filenames: false

  # Black
  - repo: local
    hooks:
    - id: black-CLI
      name: black-CLI
      entry: bash -c "black -l120 -C --preview --target-version py38 config_hub eval extensions litgpt tests --exclude unsloth/kernels"
      language: system
      pass_filenames: false

  # Markdown
  - repo: https://github.com/igorshubovych/markdownlint-cli
    rev: v0.39.0
    hooks:
      - id: markdownlint-fix
        args:
          - --disable
          - MD013 # line-length
          - MD033 # no-inline-html

Another way of doing via GitHub Actions is described in the #765.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants