Skip to content

Commit

Permalink
Add Pre-Commit Config
Browse files Browse the repository at this point in the history
Adds support for [`pre-commit`](https://pre-commit.com/) following the example from [`jupyter_client`](jupyter/jupyter_client#631).
  • Loading branch information
fcollonval authored and Steven Silvester committed Aug 20, 2021
1 parent 1444737 commit e617a8a
Show file tree
Hide file tree
Showing 34 changed files with 136 additions and 58 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Initial pre-commit reformat
2 changes: 2 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[blame]
ignoreRevsFile = .git-blame-ignore-revs
24 changes: 24 additions & 0 deletions .github/workflows/python-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ on:
pull_request:
branches: '*'
jobs:
# Run "pre-commit run --all-files"
pre-commit:
runs-on: ubuntu-20.04
timeout-minutes: 2

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.8

# ref: https://github.com/pre-commit/action
- uses: pre-commit/[email protected]
- name: Help message if pre-commit fail
if: ${{ failure() }}
run: |
echo "You can install pre-commit hooks to automatically run formatting"
echo "on each commit with:"
echo " pre-commit install"
echo "or you can run by hand on staged files with"
echo " pre-commit run"
echo "or after-the-fact on already committed files with"
echo " pre-commit run --all-files"
build:
runs-on: ${{ matrix.os }}-latest
strategy:
Expand Down
34 changes: 34 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
repos:
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.9.0
hooks:
- id: reorder-python-imports
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
args: ["--line-length", "100"]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.2.1
hooks:
- id: prettier
- repo: https://gitlab.com/pycqa/flake8
rev: "3.8.4"
hooks:
- id: flake8
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: end-of-file-fixer
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.32.0
hooks:
- id: eslint
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v3.0.0a3
hooks:
- id: pylint
args: [--disable=all, --enable=unused-import]
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/templates/*.html
27 changes: 27 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ from any directory in your system with::

jupyter server


Code Styling
-----------------------------
`jupyter_server` has adopted automatic code formatting so you shouldn't
need to worry too much about your code style.
As long as your code is valid,
the pre-commit hook should take care of how it should look.
To install `pre-commit`, run the following::

pip install pre-commit
pre-commit install


You can invoke the pre-commit hook by hand at any time with::

pre-commit run

which should run any autoformatting on your code
and tell you about any errors it couldn't fix automatically.
You may also install [black integration](https://github.com/psf/black#editor-integration)
into your text editor to format code automatically.

If you have already committed files before setting up the pre-commit
hook with ``pre-commit install``, you can fix everything up using
``pre-commit run --all-files``. You need to make the fixing commit
yourself after that.

Troubleshooting the Installation
--------------------------------

Expand Down
3 changes: 1 addition & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import sys
import os
import os.path as osp
import shlex
import shutil

HERE = osp.abspath(osp.dirname(__file__))
Expand Down Expand Up @@ -378,7 +377,7 @@
spelling_word_list_filename='spelling_wordlist.txt'

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


def setup(app):
Expand Down
Empty file modified examples/simple/setup.py
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion examples/simple/simple_ext1/application.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, jinja2
import os
from traitlets import Unicode
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
from .handlers import (DefaultHandler, RedirectHandler,
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/simple_ext1/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def get(self):
# The name of the extension to which this handler is linked.
self.log.info("Extension Name in {} Default Handler: {}".format(self.name, self.name))
# A method for getting the url to static files (prefixed with /static/<name>).
self.log.info("Static URL for / in simple_ext1 Default Handler:".format(self.static_url(path='/')))
self.log.info("Static URL for / in simple_ext1 Default Handler: {}".format(self.static_url(path='/')))
self.write('<h1>Hello Simple 1 - I am the default...</h1>')
self.write('Config in {} Default Handler: {}'.format(self.name, self.config))

Expand Down
2 changes: 1 addition & 1 deletion examples/simple/simple_ext2/application.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, jinja2
import os
from traitlets import Unicode
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
from .handlers import ParameterHandler, TemplateHandler, IndexHandler, ErrorHandler
Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/_sysinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import os
import platform
import pprint
import sys
import subprocess

from ipython_genutils import py3compat, encoding
from ipython_genutils import encoding

import jupyter_server

Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/base/handlers.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import mimetypes
import os
import re
import sys
import traceback
import types
import warnings
from http.client import responses
from http.cookies import Morsel
from urllib.parse import urlparse
from jinja2 import TemplateNotFound
from tornado import web, gen, escape, httputil
from tornado import web, escape, httputil
from tornado.log import app_log
import prometheus_client

Expand Down
6 changes: 3 additions & 3 deletions jupyter_server/extension/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Instance,
default,
observe,
validate,
validate as validate_trait,
)

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

metadata = Dict()

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

_linked_points = {}

@validate("name")
@validate_trait("name")
def _validate_name(self, proposed):
name = proposed['value']
self._extension_points = {}
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import zipfile

from tornado import web, escape
from tornado import web
from tornado.log import app_log

from ..base.handlers import (
Expand Down
3 changes: 2 additions & 1 deletion jupyter_server/prometheus/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
# Try to de-duplicate by using the ones in Notebook if available.
# See https://github.com/jupyter/jupyter_server/issues/209
from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS, TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL
# pylint: disable=unused-import
from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS,TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL

except ImportError:

Expand Down
11 changes: 4 additions & 7 deletions jupyter_server/serverapp.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import socket
import stat
import sys
import tempfile
import threading
import time
import warnings
import webbrowser
import urllib
import inspect
Expand All @@ -43,7 +41,7 @@

from jupyter_core.paths import secure_write
from jupyter_server.transutils import trans, _i18n
from jupyter_server.utils import run_sync_in_loop
from jupyter_server.utils import run_sync_in_loop, urljoin, pathname2url

# the minimum viable tornado version: needs to be kept in sync with setup.py
MIN_TORNADO = (6, 1, 0)
Expand Down Expand Up @@ -93,22 +91,21 @@
)
from jupyter_core.paths import jupyter_config_path
from jupyter_client import KernelManager
from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel, NATIVE_KERNEL_NAME
from jupyter_client.kernelspec import KernelSpecManager
from jupyter_client.session import Session
from nbformat.sign import NotebookNotary
from traitlets import (
Any, Dict, Unicode, Integer, List, Bool, Bytes, Instance,
TraitError, Type, Float, observe, default, validate
)
from jupyter_core.paths import jupyter_runtime_dir, jupyter_path
from jupyter_core.paths import jupyter_runtime_dir
from jupyter_server._sysinfo import get_sys_info

from jupyter_server._tz import utcnow, utcfromtimestamp
from jupyter_server._tz import utcnow
from jupyter_server.utils import (
url_path_join,
check_pid,
url_escape,
urljoin,
pathname2url,
unix_socket_in_use,
urlencode_unix_socket_path,
Expand Down
3 changes: 0 additions & 3 deletions jupyter_server/services/config/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
import io
import errno
from tornado import web

from ...base.handlers import APIHandler
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .manager import AsyncContentsManager, ContentsManager

from ipython_genutils.importstring import import_item
from traitlets import Any, Unicode, Bool, TraitError, observe, default, validate
from traitlets import Any, Unicode, Bool, TraitError, default, validate

from jupyter_core.paths import exists, is_hidden, is_file_hidden
from jupyter_server import _tz as tz
Expand Down
1 change: 0 additions & 1 deletion jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from fnmatch import fnmatch
import itertools
import json
import os
import re

from tornado.web import HTTPError, RequestHandler
Expand Down
1 change: 0 additions & 1 deletion jupyter_server/tests/auth/test_security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest

from jupyter_server.auth.security import passwd, passwd_check

Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/tests/extension/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import pytest


Expand All @@ -13,4 +12,4 @@ def test_server_extension_list(jp_environ, script_runner):
'extension',
'list',
)
assert ret.success
assert ret.success
1 change: 0 additions & 1 deletion jupyter_server/tests/services/contents/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import pathlib
import pytest
from urllib.parse import ParseResult, urlunparse

import tornado

Expand Down
6 changes: 0 additions & 6 deletions jupyter_server/tests/services/kernels/test_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import sys
import time
import json
import pytest



import tornado
import urllib.parse
from tornado.escape import url_escape

from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
from jupyter_client.multikernelmanager import AsyncMultiKernelManager

from jupyter_server.utils import url_path_join
from ...utils import expected_http_error
Expand Down
1 change: 0 additions & 1 deletion jupyter_server/tests/services/kernels/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import pytest
from traitlets.config import Config
from jupyter_server.services.kernels.kernelmanager import AsyncMappingKernelManager
Expand Down
2 changes: 0 additions & 2 deletions jupyter_server/tests/services/kernels/test_cull.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import asyncio
import json
import platform
import sys
import time
import pytest
from traitlets.config import Config
from tornado.httpclient import HTTPClientError
Expand Down
1 change: 0 additions & 1 deletion jupyter_server/tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import datetime
from tornado.web import HTTPError
from tornado.httpclient import HTTPRequest, HTTPResponse
from jupyter_server.serverapp import ServerApp
from jupyter_server.gateway.managers import GatewayClient
from jupyter_server.utils import ensure_async

Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/tests/test_serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from jupyter_server.serverapp import (
ServerApp,
list_running_servers,
JupyterPasswordApp,
JupyterServerStopApp
JupyterPasswordApp
)
from jupyter_server.auth.security import passwd_check

Expand Down
1 change: 0 additions & 1 deletion jupyter_server/tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
import json
import asyncio
import sys

from tornado.httpclient import HTTPClientError
from traitlets.config import Config
Expand Down
5 changes: 0 additions & 5 deletions jupyter_server/tests/unix_sockets/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
reason="Unix sockets are not available on Windows."
)

import os
import urllib
import pathlib

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

from tornado.escape import url_escape

import jupyter_server.serverapp
from jupyter_server import DEFAULT_JUPYTER_SERVER_PORT
from jupyter_server.utils import (
url_path_join,
urlencode_unix_socket,
Expand Down
Loading

0 comments on commit e617a8a

Please sign in to comment.