Skip to content

Commit

Permalink
dev: swap black to ruff, drop pre-commit
Browse files Browse the repository at this point in the history
now that ruff has formatting capabilities, we can drop one more dependency.

Also, the version of black and ruff in the pre-commit config was
out of sync from the version pinned in pdm.lock. That is bad.
So just drop pre-commit, and run stuff manually, there
are only a few commands. But I can't remove it entirely,
since pre-commit.ci is enabled on all jazzband repos
and I can't turn it off. So there needs to be some content in there, so I added some of the generic builtin ones.

I had to adjust a few of the example docstrings
so that ruff wouldn't autoformat away the needed leading indentation.

I was lazy and just added a bunch of `# noqa: E721` for the
`type(match.value) == str` checks that should be isinstance()
checks, because when I tried isinstance, some tests failed.
The current method isn't that egregious.
  • Loading branch information
NickCrews committed Mar 29, 2024
1 parent 649869e commit 344d70e
Show file tree
Hide file tree
Showing 27 changed files with 482 additions and 418 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ on:
- "*"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up PDM
uses: pdm-project/setup-pdm@v2
- name: Install dependencies
run: pdm sync -d -G dev
- name: Check formatting
run: pdm run ruff format --check .
- name: Lint
run: pdm run ruff check .

test:
runs-on: ubuntu-latest
strategy:
Expand All @@ -25,11 +38,8 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
pdm sync -d -G dev
run: pdm sync -d -G dev

# Linting is taken care of by pre-commit
# in a different github action
- name: Run tests
run: |
pdm run -v pytest --cov-report term-missing --cov-report xml --cov=docopt --mypy
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.*
!.github
!.gitignore
!.pre-commit-config.yaml

*.py[co]

Expand Down
31 changes: 24 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.270
hooks:
- id: ruff
- id: check-added-large-files # prevents giant files from being committed.
- id: check-ast # simply checks whether the files parse as valid python.
- id: check-byte-order-marker # forbids files which have a utf-8 byte-order marker.
- id: check-case-conflict # checks for files that would conflict in case-insensitive filesystems.
- id: check-executables-have-shebangs # ensures that (non-binary) executables have a shebang.
- id: check-json # checks json files for parseable syntax.
- id: check-shebang-scripts-are-executable # ensures that (non-binary) files with a shebang are executable.
- id: pretty-format-json # sets a standard for formatting json files.
- id: check-merge-conflict # checks for files that contain merge conflict strings.
- id: check-symlinks # checks for symlinks which do not point to anything.
- id: check-toml # checks toml files for parseable syntax.
- id: check-vcs-permalinks # ensures that links to vcs websites are permalinks.
- id: check-xml # checks xml files for parseable syntax.
- id: check-yaml # checks yaml files for parseable syntax.
- id: debug-statements # checks for debugger imports and py37+ `breakpoint()` calls in python source.
- id: destroyed-symlinks # detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to.
- id: detect-private-key # detects the presence of private keys.
- id: end-of-file-fixer # ensures that a file is either empty, or ends with one newline.
- id: fix-byte-order-marker # removes utf-8 byte order marker.
- id: mixed-line-ending # replaces or checks mixed line ending.
- id: sort-simple-yaml # sorts simple yaml files which consist only of top-level keys, preserving comments and blocks.
- id: trailing-whitespace # trims trailing whitespace.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## UNRELEASED

### Changed

- Switched from black to ruff for formatting. Dropped use of pre-commit.

## Version 0.9.0:

### Changed
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/jazzband/docopt-ng/branch/master/graph/badge.svg)](https://codecov.io/gh/jazzband/docopt-ng)
[![image](https://img.shields.io/pypi/v/docopt-ng.svg)](https://pypi.python.org/pypi/docopt-ng)
[![Jazzband](https://jazzband.co/static/img/badge.svg)](https://jazzband.co/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

**docopt-ng** is a fork of the [original docopt](https://github.com/docopt/docopt), now maintained by the
[jazzband](https://jazzband.co/) project. Now with maintenance, typehints, and complete test coverage!
Expand Down Expand Up @@ -322,14 +322,14 @@ To setup your dev environment, fork this repo and clone it locally.
We use [pdm](https://pdm.fming.dev/latest/#installation) to
manage the project, so install that first.

Then install dev requirements and the package itself as editable, then
install the pre-commit hooks:
Then create a virtual env, install dev requirements and the package itself as editable,
then install the pre-commit hooks:

pdm sync -d -G dev
pdm sync --dev --group dev
pdm run pre-commit install

Useful testing, linting, and formatting commands:

pdm run pytest
pdm run black .
pdm run ruff .
pdm run ruff check .
pdm run ruff format .
13 changes: 7 additions & 6 deletions docopt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Copyright (c) 2019 itdaniher, itdaniher@gmail.com
"""

from __future__ import annotations

import re
Expand Down Expand Up @@ -181,21 +182,21 @@ def match(
return False, left, collected
left_ = left[:pos] + left[(pos + 1) :]
same_name = [a for a in collected if a.name == self.name]
if type(self.value) == int and len(same_name) > 0:
if type(self.value) == int and len(same_name) > 0: # noqa: E721
if isinstance(same_name[0].value, int):
same_name[0].value += 1
return True, left_, collected
if type(self.value) == int and not same_name:
if type(self.value) == int and not same_name: # noqa: E721
match.value = 1
return True, left_, collected + [match]
if same_name and type(self.value) == list:
if type(match.value) == str:
if same_name and type(self.value) == list: # noqa: E721
if type(match.value) == str: # noqa: E721
increment = [match.value]
if same_name[0].value is not None and increment is not None:
if isinstance(same_name[0].value, type(increment)):
same_name[0].value += increment
return True, left_, collected
elif not same_name and type(self.value) == list:
elif not same_name and type(self.value) == list: # noqa: E721
if isinstance(match.value, str):
match.value = [match.value]
return True, left_, collected + [match]
Expand Down Expand Up @@ -238,7 +239,7 @@ def fix_repeating_arguments(self) -> _BranchPattern:
if type(e) is _Argument or type(e) is _Option and e.argcount:
if e.value is None:
e.value = []
elif type(e.value) is not list:
elif type(e.value) is not list: # noqa: E721
e.value = cast(str, e.value)
e.value = e.value.split()
if type(e) is _Command or type(e) is _Option and e.argcount == 0:
Expand Down
3 changes: 2 additions & 1 deletion examples/arguments_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Usage: arguments_example.py [-vqrh] [FILE] ...
arguments_example.py (--left | --right) CORRECTION FILE
Expand All @@ -15,8 +16,8 @@
-r make report
--left use left-hand side
--right use right-hand side
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/calculator_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Not a serious example.
Usage:
Expand All @@ -12,8 +13,8 @@
Options:
-h, --help
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/config_file_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Usage:
#!/usr/bin/env python3
"""
Usage:
quick_example.py tcp [<host>] [--force] [--timeout=<seconds>]
quick_example.py serial <port> [--baud=<rate>] [--timeout=<seconds>]
quick_example.py -h | --help | --version
"""

from docopt import docopt


Expand Down
3 changes: 2 additions & 1 deletion examples/counted_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Usage: counted_example.py --help
counted_example.py -v...
counted_example.py go [go]
Expand All @@ -8,8 +9,8 @@
counted_example.py go go
counted_example.py --path ./here --path ./there
counted_example.py this.txt that.txt
"""

from docopt import docopt

print(docopt(__doc__))
2 changes: 1 addition & 1 deletion examples/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
remote Manage set of tracked repositories
See 'git help <command>' for more information on a specific command.
"""

from subprocess import call

from docopt import docopt
Expand Down
6 changes: 4 additions & 2 deletions examples/git/git_add.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""usage: git add [options] [--] [<filepattern>...]
#!/usr/bin/env python3
"""
usage: git add [options] [--] [<filepattern>...]
-h, --help
-n, --dry-run dry run
Expand All @@ -14,8 +16,8 @@
--refresh don't add, only refresh the index
--ignore-errors just skip files which cannot be added because of errors
--ignore-missing check if - even missing - files are ignored in dry run
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/git/git_branch.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
usage: git branch [options] [-r | -a] [--merged=<commit> | --no-merged=<commit>]
git branch [options] [-l] [-f] <branchname> [<start-point>]
Expand All @@ -24,8 +25,8 @@
-f, --force force creation (when already exists)
--no-merged=<commit> print only not merged branches
--merged=<commit> print only merged branches
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/git/git_checkout.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""usage: git checkout [options] <branch>
#!/usr/bin/env python3
"""
usage: git checkout [options] <branch>
git checkout [options] <branch> -- <file>...
-q, --quiet suppress progress reporting
Expand All @@ -14,8 +16,8 @@
-m, --merge perform a 3-way merge with the new branch
--conflict <style> conflict style (merge or diff3)
-p, --patch select hunks interactively
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/git/git_clone.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""usage: git clone [options] [--] <repo> [<dir>]
#!/usr/bin/env python3
"""
usage: git clone [options] [--] <repo> [<dir>]
-v, --verbose be more verbose
-q, --quiet be more quiet
Expand All @@ -21,8 +23,8 @@
-u, --upload-pack <path>
path to git-upload-pack on the remote
--depth <depth> create a shallow clone of that depth
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/git/git_commit.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""usage: git commit [options] [--] [<filepattern>...]
#!/usr/bin/env python3
"""
usage: git commit [options] [--] [<filepattern>...]
-h, --help
-q, --quiet suppress summary after successful commit
Expand Down Expand Up @@ -41,8 +43,8 @@
-u, --untracked-files=<mode>
show untracked files, optional modes: all, normal, no.
[default: all]
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/git/git_push.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""usage: git push [options] [<repository> [<refspec>...]]
#!/usr/bin/env python3
"""
usage: git push [options] [<repository> [<refspec>...]]
-h, --help
-v, --verbose be more verbose
Expand All @@ -18,8 +20,8 @@
receive pack program
-u, --set-upstream set upstream for git pull/status
--progress force progress reporting
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/git/git_remote.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
usage: git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>
Expand All @@ -13,8 +14,8 @@
git remote set-url --delete <name> <url>
-v, --verbose be verbose; must be placed before a subcommand
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
Empty file modified examples/interactive_example.py
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion examples/naval_fate.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Naval Fate.
Usage:
Expand All @@ -14,8 +15,8 @@
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/odd_even_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python3
"""Usage: odd_even_example.py [-h | --help] (ODD EVEN)...
Example, try:
odd_even_example.py 1 2 3 4
Options:
-h, --help
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/options_example.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""Example of program with many options using docopt.
Usage:
Expand Down Expand Up @@ -29,8 +30,8 @@
--benchmark measure processing speed
--testsuite=DIR run regression tests from dir
--doctest run doctest on myself
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down

0 comments on commit 344d70e

Please sign in to comment.