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.
This also means that pre-commit is a bit overkill.
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.

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 3efe10d commit 3c4acfd
Show file tree
Hide file tree
Showing 26 changed files with 346 additions and 481 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
9 changes: 0 additions & 9 deletions .pre-commit-config.yaml

This file was deleted.

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: 5 additions & 7 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,12 @@ 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:

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

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
2 changes: 1 addition & 1 deletion examples/arguments_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
-r make report
--left use left-hand side
--right use right-hand side
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/calculator_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Options:
-h, --help
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions examples/config_file_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Usage:
"""
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
2 changes: 1 addition & 1 deletion examples/counted_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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
5 changes: 3 additions & 2 deletions examples/git/git_add.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""usage: git add [options] [--] [<filepattern>...]
"""
usage: git add [options] [--] [<filepattern>...]
-h, --help
-n, --dry-run dry run
Expand All @@ -14,8 +15,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
2 changes: 1 addition & 1 deletion examples/git/git_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,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
5 changes: 3 additions & 2 deletions examples/git/git_checkout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""usage: git checkout [options] <branch>
"""
usage: git checkout [options] <branch>
git checkout [options] <branch> -- <file>...
-q, --quiet suppress progress reporting
Expand All @@ -14,8 +15,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
5 changes: 3 additions & 2 deletions examples/git/git_clone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""usage: git clone [options] [--] <repo> [<dir>]
"""
usage: git clone [options] [--] <repo> [<dir>]
-v, --verbose be more verbose
-q, --quiet be more quiet
Expand All @@ -21,8 +22,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
5 changes: 3 additions & 2 deletions examples/git/git_commit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""usage: git commit [options] [--] [<filepattern>...]
"""
usage: git commit [options] [--] [<filepattern>...]
-h, --help
-q, --quiet suppress summary after successful commit
Expand Down Expand Up @@ -41,8 +42,8 @@
-u, --untracked-files=<mode>
show untracked files, optional modes: all, normal, no.
[default: all]
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions examples/git/git_push.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""usage: git push [options] [<repository> [<refspec>...]]
"""
usage: git push [options] [<repository> [<refspec>...]]
-h, --help
-v, --verbose be more verbose
Expand All @@ -18,8 +19,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
2 changes: 1 addition & 1 deletion examples/git/git_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,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
2 changes: 1 addition & 1 deletion examples/naval_fate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/odd_even_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Options:
-h, --help
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/options_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,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
2 changes: 1 addition & 1 deletion examples/options_shortcut_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
-t, --timeout TIMEOUT set timeout TIMEOUT seconds
--apply apply changes to database
-q operate in quiet mode
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions examples/quick_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Usage:
"""
Usage:
quick_example.py tcp <host> <port> [--timeout=<seconds>]
quick_example.py serial <port> [--baud=9600] [--timeout=<seconds>]
quick_example.py -h | --help | --version
"""

from docopt import docopt

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/validation_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
Options:
--count=N number of operations
"""

import os

from docopt import docopt
Expand Down

0 comments on commit 3c4acfd

Please sign in to comment.