Skip to content

Commit

Permalink
Merge pull request #489 from consideRatio/pr/add-pyupgrade-and-autoflake
Browse files Browse the repository at this point in the history
pre-commit: add pyupgrade and autoflake, simplify flake8 config
  • Loading branch information
consideRatio committed Jun 1, 2023
2 parents 1a14c6e + b311929 commit 7d72e76
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 31 deletions.
10 changes: 2 additions & 8 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
# E: style errors
# W: style warnings
# C: complexity
# F401: module imported but unused
# F403: import *
# F811: redefinition of unused `name` from line `N`
# F841: local variable assigned but never used
# E402: module level import not at top of file
# I100: Import statements are in the wrong order
# I101: Imported names are in the wrong order. Should be
ignore = E, C, W, F401, F403, F811, F841, E402, I100, I101, D400
# D: docstring warnings (unused pydocstyle extension)
ignore = E, C, W, D
builtins = c, get_config
exclude =
.cache,
Expand Down
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
#
# Config reference: https://pre-commit.com/#pre-commit-configyaml---top-level
#
# Common tasks
#
# - Run on all files: pre-commit run --all-files
# - Register git hooks: pre-commit install --install-hooks
#
repos:
# Autoformat: Python code, syntax patterns are modernized
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
- id: pyupgrade
args:
- --py38-plus

# Autoformat: Python code
- repo: https://github.com/PyCQA/autoflake
rev: v2.1.1
hooks:
- id: autoflake
# args ref: https://github.com/PyCQA/autoflake#advanced-usage
args:
- --in-place

# Autoformat: Python
- repo: https://github.com/pycqa/isort
rev: 5.12.0
Expand Down
10 changes: 6 additions & 4 deletions dockerspawner/dockerspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def validate(self, obj, value):
if value is False:
return value

return super(UnicodeOrFalse, self).validate(obj, value)
return super().validate(obj, value)


import jupyterhub
Expand Down Expand Up @@ -888,7 +888,7 @@ def _object_name_default(self):
return self._render_templates(self.name_template)

def load_state(self, state):
super(DockerSpawner, self).load_state(state)
super().load_state(state)
if "container_id" in state:
# backward-compatibility for dockerspawner < 0.10
self.object_id = state.get("container_id")
Expand All @@ -906,7 +906,7 @@ def load_state(self, state):
)

def get_state(self):
state = super(DockerSpawner, self).get_state()
state = super().get_state()
if self.object_id:
state["object_id"] = self.object_id
# persist object_name if running
Expand Down Expand Up @@ -1046,7 +1046,9 @@ async def check_allowed(self, image):
if image not in allowed_images:
raise web.HTTPError(
400,
"Image %s not in allowed list: %s" % (image, ', '.join(allowed_images)),
"Image {} not in allowed list: {}".format(
image, ', '.join(allowed_images)
),
)
# resolve image alias to actual image name
return allowed_images[image]
Expand Down
3 changes: 1 addition & 2 deletions dockerspawner/swarmspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,14 @@ async def start_object(self):
break
if state != "running":
raise RuntimeError(
"Service %s not running: %s" % (self.service_name, pformat(status))
f"Service {self.service_name} not running: {pformat(status)}"
)

async def stop_object(self):
"""Nothing to do here
There is no separate stop action for services
"""
pass

async def get_ip_and_port(self):
"""Queries Docker daemon for service's IP and port.
Expand Down
8 changes: 4 additions & 4 deletions dockerspawner/systemuserspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def volume_mount_points(self):
Returns a list of all the values in self.volumes or
self.read_only_volumes.
"""
mount_points = super(SystemUserSpawner, self).volume_mount_points
mount_points = super().volume_mount_points
mount_points.append(self.homedir)
return mount_points

Expand All @@ -128,12 +128,12 @@ def volume_binds(self):
host_location: {'bind': container_location, 'ro': True}
}
"""
volumes = super(SystemUserSpawner, self).volume_binds
volumes = super().volume_binds
volumes[self.host_homedir] = {'bind': self.homedir, 'ro': False}
return volumes

def get_env(self):
env = super(SystemUserSpawner, self).get_env()
env = super().get_env()
# relies on NB_USER and NB_UID handling in jupyter/docker-stacks
env.update(
dict(
Expand Down Expand Up @@ -223,4 +223,4 @@ def start(self, *, image=None, extra_create_kwargs=None, extra_host_config=None)
f"user_id for {self.user.name} not set, but group_id is {self.group_id}. This will have no effect."
)

return super(SystemUserSpawner, self).start()
return super().start()
3 changes: 0 additions & 3 deletions examples/internal-ssl/jupyterhub_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os
import socket
import sys
import time

c = get_config() # noqa
c.JupyterHub.authenticator_class = 'dummy'
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# autoflake is used for autoformatting Python code
#
# ref: https://github.com/PyCQA/autoflake#readme
#
[tool.autoflake]
ignore-init-module-imports = true
remove-all-unused-imports = true
remove-duplicate-keys = true
remove-unused-variables = true


# isort is used for autoformatting Python code
#
# ref: https://pycqa.github.io/isort/
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
# -----------------------------------------------------------------------------
# Minimal Python version sanity check (from IPython/Jupyterhub)
# -----------------------------------------------------------------------------
from __future__ import print_function

import os
import sys
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def pytest_collection_modifyitems(items):


@pytest.fixture
def app(jupyterhub_app):
def app(jupyterhub_app): # noqa: F811
app = jupyterhub_app
app.config.DockerSpawner.prefix = "dockerspawner-test"
# If it's a prerelease e.g. (2, 0, 0, 'rc4', '') use full tag
Expand Down
6 changes: 2 additions & 4 deletions tests/test_dockerspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ async def test_image_pull_policy(dockerspawner_configured_app):
tag = "1.29.1" # a version that's definitely not latest
# ensure image isn't present
try:
await asyncio.wrap_future(
spawner.docker("remove_image", "{}:{}".format(repo, tag))
)
await asyncio.wrap_future(spawner.docker("remove_image", f"{repo}:{tag}"))
except docker.errors.ImageNotFound:
pass

spawner.pull_policy = "ifnotpresent"
image = "{}:{}".format(repo, tag)
image = f"{repo}:{tag}"
# should trigger a pull
await spawner.pull_image(image)
# verify that the image exists now
Expand Down
1 change: 0 additions & 1 deletion tests/test_swarmspawner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tests for SwarmSpawner"""
import asyncio

import pytest
from jupyterhub.tests.mocking import public_url
from jupyterhub.tests.test_api import add_user, api_request
from jupyterhub.utils import url_path_join
Expand Down
1 change: 0 additions & 1 deletion tests/test_systemuserspawner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tests for SwarmSpawner"""
from getpass import getuser

import pytest
from jupyterhub.tests.mocking import public_url
from jupyterhub.tests.test_api import add_user, api_request
from jupyterhub.utils import url_path_join
Expand Down
1 change: 0 additions & 1 deletion tests/volumes_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test volume manipulation logic
"""
from __future__ import absolute_import, division, print_function

import types

Expand Down

0 comments on commit 7d72e76

Please sign in to comment.