Use pyupgrade, black, isort, and flake8 via pre-commit
MODIFY
.github/workflows/ci.yml
.github/workflows/ci.yml
@@ -7,21 +7,6 @@ defaults:
shell: bash
jobs:
- lint:
- runs-on: ubuntu-latest
-
- steps:
- - name: Checkout Repository
- uses: actions/checkout@v2
- - name: Set up Python 3.8
- uses: actions/setup-python@v2
- with:
- python-version: 3.8
- - name: Install dependencies
- run: python3.8 -m pip install nox
- - name: Lint the code
- run: nox -s lint
-
package:
runs-on: ubuntu-latest

ADD
.github/workflows/lint.yml
+name: lint
+
+on: [push, pull_request]
+
+jobs:
+ lint:
+ runs-on: ubuntu-20.04
+
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v2
+ - name: Set up Python 3.8
+ uses: actions/setup-python@v2
+ with:
+ python-version: 3.8
+ - name: Lint the code
+ uses: pre-commit/action@v2.0.0
+ - name: Install dependencies
+ run: python3.8 -m pip install nox
+ - name: Run mypy
+ run: nox -s mypy

ADD
.pre-commit-config.yaml
+repos:
+ - repo: https://github.com/asottile/pyupgrade
+ rev: v2.7.4
+ hooks:
+ - id: pyupgrade
+ args: ["--py36-plus"]
+
+ - repo: https://github.com/psf/black
+ rev: 20.8b1
+ hooks:
+ - id: black
+ args: ["--target-version", "py36"]
+
+ - repo: https://github.com/PyCQA/isort
+ rev: 5.6.4
+ hooks:
+ - id: isort
+
+ - repo: https://gitlab.com/pycqa/flake8
+ rev: 3.8.4
+ hooks:
+ - id: flake8
+ additional_dependencies: [flake8-2020]

MODIFY
noxfile.py
noxfile.py
@@ -86,23 +86,33 @@ def unsupported_python2(session):
@nox.session()
def format(session):
"""Run code formatters."""
- session.install("black", "isort")
- session.run("black", *SOURCE_FILES)
- session.run("isort", *SOURCE_FILES)
+ session.install("pre-commit")
+ session.run("pre-commit", "--version")
- lint(session)
+ process = subprocess.run(
+ ["pre-commit", "run", "--all-files"],
+ env=session.env,
+ text=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ )
+ # Ensure that pre-commit itself ran successfully
+ assert process.returncode in (0, 1)
@nox.session
def lint(session):
- session.install("flake8", "flake8-2020", "black", "isort", "mypy")
- session.run("flake8", "--version")
- session.run("black", "--version")
- session.run("isort", "--version")
+ session.install("pre-commit")
+ session.run("pre-commit", "run", "--all-files")
+
+ mypy(session)
+
+
+@nox.session()
+def mypy(session):
+ """Run mypy."""
+ session.install("mypy")
session.run("mypy", "--version")
- session.run("black", "--check", *SOURCE_FILES)
- session.run("isort", "--check", *SOURCE_FILES)
- session.run("flake8", *SOURCE_FILES)
session.log("mypy --strict src/urllib3")
all_errors, errors = [], []

MODIFY
src/urllib3/request.py
src/urllib3/request.py
@@ -48,7 +48,7 @@ class RequestMethods:
headers=None,
encode_multipart=True,
multipart_boundary=None,
- **kw
+ **kw,
) -> BaseHTTPResponse: # Abstract
raise NotImplementedError(
"Classes extending RequestMethods must implement "
@@ -107,7 +107,7 @@ class RequestMethods:
headers=None,
encode_multipart=True,
multipart_boundary=None,
- **urlopen_kw
+ **urlopen_kw,
) -> BaseHTTPResponse:
"""
Make a request using :meth:`urlopen` with the ``fields`` encoded in

MODIFY
test/contrib/test_socks.py
test/contrib/test_socks.py
-from __future__ import absolute_import
-
import socket
import threading
from socket import getaddrinfo as real_getaddrinfo

Use pyupgrade, black, isort, and flake8 via pre-commit