Skip to content

Commit 4586ed9

Browse files
Require black and isort for contributions (#3329)
* Add explanation to CONTRIBUTNG.md * Add sample pre-commit script * Check for correctly formatted files in CI Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 5d553c9 commit 4586ed9

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

.travis.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@ jobs:
2727
- name: "check file consistency"
2828
script: ./tests/check_consistent.py
2929
- name: "flake8"
30-
install: pip install -r requirements-tests-py3.txt
30+
install: pip install $(grep flake8 requirements-tests-py3.txt)
3131
script: flake8
32+
- name: "black"
33+
install: pip install $(grep black requirements-tests-py3.txt)
34+
script: black --check --diff stdlib third_party
35+
allow_failures:
36+
- name: "isort"
37+
install: pip install $(grep isort requirements-tests-py3.txt)
38+
script: isort --check-only --diff --recursive stdlib third_party

CONTRIBUTING.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ are important to the project's success.
1515
but [contact us](#discussion) before starting significant work.
1616
* Create your stubs [conforming to the coding style](#stub-file-coding-style).
1717
* Make sure your tests pass cleanly on `mypy`, `pytype`, and `flake8`.
18+
* Reformat your stubs with `black` and `isort`.
1819
4. [Submit your changes](#submitting-changes) by opening a pull request.
1920
5. You can expect a reply within a few days:
2021
* Diffs are merged when considered ready by the core team.
@@ -222,8 +223,14 @@ rule is that they should be as concise as possible. Specifically:
222223
* use variable annotations instead of type comments, even for stubs
223224
that target older versions of Python;
224225
* for arguments with a type and a default, use spaces around the `=`.
225-
The code formatter [black](https://github.com/psf/black) will format
226-
stubs according to this standard.
226+
227+
Stubs should be reformatted with the formatters
228+
[black](https://github.com/psf/black) and
229+
[isort](https://github.com/timothycrosley/isort) before submission.
230+
These formatters are included in typeshed's `requirements-tests-py3.txt` file.
231+
A sample `pre-commit` file is included in the typeshed repository. Copy it
232+
to `.git/hooks` and adjust the path to your virtual environment's `bin`
233+
directory to automatically reformat stubs before commit.
227234

228235
Stub files should only contain information necessary for the type
229236
checker, and leave out unnecessary detail:

pre-commit

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script that will run flake8, black, and isort
4+
# prior to committing and will stop the commit if there are any
5+
# warnings. Adjust BIN_DIR to the virtual environment where flake8,
6+
# black, and isort are installed.
7+
#
8+
# To enable this hook, copy this file to ".git/hooks".
9+
10+
BIN_DIR=./.venv/bin
11+
12+
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=AM | grep .pyi || true)
13+
14+
if test -n "${CHANGED_FILES}" -a -d "${BIN_DIR}"; then
15+
${BIN_DIR}/flake8 ${CHANGED_FILES}
16+
${BIN_DIR}/black --check ${CHANGED_FILES}
17+
${BIN_DIR}/isort --check-only ${CHANGED_FILES}
18+
19+
# Replace the last two lines with the following lines
20+
# if you want to reformat changed files automatically
21+
# before committing. Please note that partial commits
22+
# (git add -p) will not work and will commit the whole
23+
# file!
24+
#
25+
# ${BIN_DIR}/black ${CHANGED_FILES} || true
26+
# ${BIN_DIR}/isort -y ${CHANGED_FILES} || true
27+
# git add ${CHANGED_FILES}
28+
fi

requirements-tests-py3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ black==19.10b0
44
flake8==3.8.2
55
flake8-bugbear==20.1.4
66
flake8-pyi==20.5.0
7-
isort==4.3.21
7+
isort[pyproject]==4.3.21
88
pytype>=2020.06.26

stdlib/2and3/difflib.pyi

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ class SequenceMatcher(Generic[_T]):
5757
# mypy thinks the signatures of the overloads overlap, but the types still work fine
5858
@overload
5959
def get_close_matches( # type: ignore
60-
word: AnyStr,
61-
possibilities: Iterable[AnyStr],
62-
n: int = ...,
63-
cutoff: float = ...,
60+
word: AnyStr, possibilities: Iterable[AnyStr], n: int = ..., cutoff: float = ...,
6461
) -> List[AnyStr]: ...
6562
@overload
6663
def get_close_matches(

stdlib/3/asyncio/base_events.pyi

+1-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
7373
# run_in_executor is defined as a coroutine in AbstractEventLoop but returns a Future in concrete implementation.
7474
# Need to ignore mypy Return type error as a result.
7575
def run_in_executor( # type: ignore
76-
self,
77-
executor: Any,
78-
func: Callable[..., _T],
79-
*args: Any,
76+
self, executor: Any, func: Callable[..., _T], *args: Any,
8077
) -> Future[_T]: ...
8178
def set_default_executor(self, executor: Any) -> None: ...
8279
# Network I/O methods returning Futures.

0 commit comments

Comments
 (0)