Skip to content

Commit e617a8a

Browse files
fcollonvalSteven Silvester
authored and
Steven Silvester
committed
Add Pre-Commit Config
Adds support for [`pre-commit`](https://pre-commit.com/) following the example from [`jupyter_client`](jupyter/jupyter_client#631).
1 parent 1444737 commit e617a8a

34 files changed

+136
-58
lines changed

.git-blame-ignore-revs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Initial pre-commit reformat

.gitconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[blame]
2+
ignoreRevsFile = .git-blame-ignore-revs

.github/workflows/python-linux.yml

+24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ on:
55
pull_request:
66
branches: '*'
77
jobs:
8+
# Run "pre-commit run --all-files"
9+
pre-commit:
10+
runs-on: ubuntu-20.04
11+
timeout-minutes: 2
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-python@v2
16+
with:
17+
python-version: 3.8
18+
19+
# ref: https://github.com/pre-commit/action
20+
- uses: pre-commit/[email protected]
21+
- name: Help message if pre-commit fail
22+
if: ${{ failure() }}
23+
run: |
24+
echo "You can install pre-commit hooks to automatically run formatting"
25+
echo "on each commit with:"
26+
echo " pre-commit install"
27+
echo "or you can run by hand on staged files with"
28+
echo " pre-commit run"
29+
echo "or after-the-fact on already committed files with"
30+
echo " pre-commit run --all-files"
31+
832
build:
933
runs-on: ${{ matrix.os }}-latest
1034
strategy:

.pre-commit-config.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
repos:
2+
- repo: https://github.com/asottile/reorder_python_imports
3+
rev: v1.9.0
4+
hooks:
5+
- id: reorder-python-imports
6+
- repo: https://github.com/psf/black
7+
rev: 20.8b1
8+
hooks:
9+
- id: black
10+
args: ["--line-length", "100"]
11+
- repo: https://github.com/pre-commit/mirrors-prettier
12+
rev: v2.2.1
13+
hooks:
14+
- id: prettier
15+
- repo: https://gitlab.com/pycqa/flake8
16+
rev: "3.8.4"
17+
hooks:
18+
- id: flake8
19+
- repo: https://github.com/pre-commit/pre-commit-hooks
20+
rev: v3.4.0
21+
hooks:
22+
- id: end-of-file-fixer
23+
- id: check-case-conflict
24+
- id: check-executables-have-shebangs
25+
- id: requirements-txt-fixer
26+
- repo: https://github.com/pre-commit/mirrors-eslint
27+
rev: v7.32.0
28+
hooks:
29+
- id: eslint
30+
- repo: https://github.com/pre-commit/mirrors-pylint
31+
rev: v3.0.0a3
32+
hooks:
33+
- id: pylint
34+
args: [--disable=all, --enable=unused-import]

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/templates/*.html

CONTRIBUTING.rst

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ from any directory in your system with::
3737

3838
jupyter server
3939

40+
41+
Code Styling
42+
-----------------------------
43+
`jupyter_server` has adopted automatic code formatting so you shouldn't
44+
need to worry too much about your code style.
45+
As long as your code is valid,
46+
the pre-commit hook should take care of how it should look.
47+
To install `pre-commit`, run the following::
48+
49+
pip install pre-commit
50+
pre-commit install
51+
52+
53+
You can invoke the pre-commit hook by hand at any time with::
54+
55+
pre-commit run
56+
57+
which should run any autoformatting on your code
58+
and tell you about any errors it couldn't fix automatically.
59+
You may also install [black integration](https://github.com/psf/black#editor-integration)
60+
into your text editor to format code automatically.
61+
62+
If you have already committed files before setting up the pre-commit
63+
hook with ``pre-commit install``, you can fix everything up using
64+
``pre-commit run --all-files``. You need to make the fixing commit
65+
yourself after that.
66+
4067
Troubleshooting the Installation
4168
--------------------------------
4269

docs/source/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import sys
1717
import os
1818
import os.path as osp
19-
import shlex
2019
import shutil
2120

2221
HERE = osp.abspath(osp.dirname(__file__))
@@ -378,7 +377,7 @@
378377
spelling_word_list_filename='spelling_wordlist.txt'
379378

380379
# import before any doc is built, so _ is guaranteed to be injected
381-
import jupyter_server.transutils
380+
import jupyter_server.transutils # pylint: disable=unused-import
382381

383382

384383
def setup(app):

examples/simple/setup.py

100755100644
File mode changed.

examples/simple/simple_ext1/application.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, jinja2
1+
import os
22
from traitlets import Unicode
33
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
44
from .handlers import (DefaultHandler, RedirectHandler,

examples/simple/simple_ext1/handlers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def get(self):
77
# The name of the extension to which this handler is linked.
88
self.log.info("Extension Name in {} Default Handler: {}".format(self.name, self.name))
99
# A method for getting the url to static files (prefixed with /static/<name>).
10-
self.log.info("Static URL for / in simple_ext1 Default Handler:".format(self.static_url(path='/')))
10+
self.log.info("Static URL for / in simple_ext1 Default Handler: {}".format(self.static_url(path='/')))
1111
self.write('<h1>Hello Simple 1 - I am the default...</h1>')
1212
self.write('Config in {} Default Handler: {}'.format(self.name, self.config))
1313

examples/simple/simple_ext2/application.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, jinja2
1+
import os
22
from traitlets import Unicode
33
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
44
from .handlers import ParameterHandler, TemplateHandler, IndexHandler, ErrorHandler

jupyter_server/_sysinfo.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import os
1010
import platform
11-
import pprint
1211
import sys
1312
import subprocess
1413

15-
from ipython_genutils import py3compat, encoding
14+
from ipython_genutils import encoding
1615

1716
import jupyter_server
1817

jupyter_server/base/handlers.py

100755100644
+1-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
import mimetypes
1111
import os
1212
import re
13-
import sys
1413
import traceback
1514
import types
1615
import warnings
1716
from http.client import responses
1817
from http.cookies import Morsel
1918
from urllib.parse import urlparse
2019
from jinja2 import TemplateNotFound
21-
from tornado import web, gen, escape, httputil
20+
from tornado import web, escape, httputil
2221
from tornado.log import app_log
2322
import prometheus_client
2423

jupyter_server/extension/manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Instance,
1616
default,
1717
observe,
18-
validate,
18+
validate as validate_trait,
1919
)
2020

2121
from .config import ExtensionConfigManager
@@ -36,7 +36,7 @@ class ExtensionPoint(HasTraits):
3636

3737
metadata = Dict()
3838

39-
@validate('metadata')
39+
@validate_trait('metadata')
4040
def _valid_metadata(self, proposed):
4141
metadata = proposed['value']
4242
# Verify that the metadata has a "name" key.
@@ -180,7 +180,7 @@ def __init__(self, *args, **kwargs):
180180

181181
_linked_points = {}
182182

183-
@validate("name")
183+
@validate_trait("name")
184184
def _validate_name(self, proposed):
185185
name = proposed['value']
186186
self._extension_points = {}

jupyter_server/nbconvert/handlers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import zipfile
99

10-
from tornado import web, escape
10+
from tornado import web
1111
from tornado.log import app_log
1212

1313
from ..base.handlers import (

jupyter_server/prometheus/metrics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
1010
# Try to de-duplicate by using the ones in Notebook if available.
1111
# See https://github.com/jupyter/jupyter_server/issues/209
12-
from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS, TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL
12+
# pylint: disable=unused-import
13+
from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS,TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL
1314

1415
except ImportError:
1516

jupyter_server/serverapp.py

100755100644
+4-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import socket
2424
import stat
2525
import sys
26-
import tempfile
2726
import threading
2827
import time
29-
import warnings
3028
import webbrowser
3129
import urllib
3230
import inspect
@@ -43,7 +41,7 @@
4341

4442
from jupyter_core.paths import secure_write
4543
from jupyter_server.transutils import trans, _i18n
46-
from jupyter_server.utils import run_sync_in_loop
44+
from jupyter_server.utils import run_sync_in_loop, urljoin, pathname2url
4745

4846
# the minimum viable tornado version: needs to be kept in sync with setup.py
4947
MIN_TORNADO = (6, 1, 0)
@@ -93,22 +91,21 @@
9391
)
9492
from jupyter_core.paths import jupyter_config_path
9593
from jupyter_client import KernelManager
96-
from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel, NATIVE_KERNEL_NAME
94+
from jupyter_client.kernelspec import KernelSpecManager
9795
from jupyter_client.session import Session
9896
from nbformat.sign import NotebookNotary
9997
from traitlets import (
10098
Any, Dict, Unicode, Integer, List, Bool, Bytes, Instance,
10199
TraitError, Type, Float, observe, default, validate
102100
)
103-
from jupyter_core.paths import jupyter_runtime_dir, jupyter_path
101+
from jupyter_core.paths import jupyter_runtime_dir
104102
from jupyter_server._sysinfo import get_sys_info
105103

106-
from jupyter_server._tz import utcnow, utcfromtimestamp
104+
from jupyter_server._tz import utcnow
107105
from jupyter_server.utils import (
108106
url_path_join,
109107
check_pid,
110108
url_escape,
111-
urljoin,
112109
pathname2url,
113110
unix_socket_in_use,
114111
urlencode_unix_socket_path,

jupyter_server/services/config/handlers.py

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
55
import json
6-
import os
7-
import io
8-
import errno
96
from tornado import web
107

118
from ...base.handlers import APIHandler

jupyter_server/services/contents/filemanager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .manager import AsyncContentsManager, ContentsManager
2222

2323
from ipython_genutils.importstring import import_item
24-
from traitlets import Any, Unicode, Bool, TraitError, observe, default, validate
24+
from traitlets import Any, Unicode, Bool, TraitError, default, validate
2525

2626
from jupyter_core.paths import exists, is_hidden, is_file_hidden
2727
from jupyter_server import _tz as tz

jupyter_server/services/contents/manager.py

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from fnmatch import fnmatch
77
import itertools
88
import json
9-
import os
109
import re
1110

1211
from tornado.web import HTTPError, RequestHandler

jupyter_server/tests/auth/test_security.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21

32
from jupyter_server.auth.security import passwd, passwd_check
43

jupyter_server/tests/extension/test_entrypoint.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import pytest
32

43

@@ -13,4 +12,4 @@ def test_server_extension_list(jp_environ, script_runner):
1312
'extension',
1413
'list',
1514
)
16-
assert ret.success
15+
assert ret.success

jupyter_server/tests/services/contents/test_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import pathlib
44
import pytest
5-
from urllib.parse import ParseResult, urlunparse
65

76
import tornado
87

jupyter_server/tests/services/kernels/test_api.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import sys
21
import time
32
import json
43
import pytest
54

6-
7-
85
import tornado
9-
import urllib.parse
10-
from tornado.escape import url_escape
116

127
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
13-
from jupyter_client.multikernelmanager import AsyncMultiKernelManager
148

159
from jupyter_server.utils import url_path_join
1610
from ...utils import expected_http_error

jupyter_server/tests/services/kernels/test_config.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import pytest
32
from traitlets.config import Config
43
from jupyter_server.services.kernels.kernelmanager import AsyncMappingKernelManager

jupyter_server/tests/services/kernels/test_cull.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import asyncio
22
import json
33
import platform
4-
import sys
5-
import time
64
import pytest
75
from traitlets.config import Config
86
from tornado.httpclient import HTTPClientError

jupyter_server/tests/test_gateway.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from datetime import datetime
88
from tornado.web import HTTPError
99
from tornado.httpclient import HTTPRequest, HTTPResponse
10-
from jupyter_server.serverapp import ServerApp
1110
from jupyter_server.gateway.managers import GatewayClient
1211
from jupyter_server.utils import ensure_async
1312

jupyter_server/tests/test_serverapp.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from jupyter_server.serverapp import (
1313
ServerApp,
1414
list_running_servers,
15-
JupyterPasswordApp,
16-
JupyterServerStopApp
15+
JupyterPasswordApp
1716
)
1817
from jupyter_server.auth.security import passwd_check
1918

jupyter_server/tests/test_terminal.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
import json
55
import asyncio
6-
import sys
76

87
from tornado.httpclient import HTTPClientError
98
from traitlets.config import Config

jupyter_server/tests/unix_sockets/test_api.py

-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
reason="Unix sockets are not available on Windows."
88
)
99

10-
import os
1110
import urllib
12-
import pathlib
1311

1412
if not sys.platform.startswith('win'):
1513
from tornado.netutil import bind_unix_socket
1614

17-
from tornado.escape import url_escape
18-
1915
import jupyter_server.serverapp
20-
from jupyter_server import DEFAULT_JUPYTER_SERVER_PORT
2116
from jupyter_server.utils import (
2217
url_path_join,
2318
urlencode_unix_socket,

0 commit comments

Comments
 (0)