Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flake8 and mypy settings #256

Merged
merged 8 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.6, 3.7, 3.8, 3.9, 3.10-dev, pypy-3.7]
python-version: [3.7, 3.8, 3.9, "3.10", pypy-3.7]
exclude:
# pywin32 not available
- os: windows-latest
python-version: 3.10-dev
python-version: "3.10"
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down Expand Up @@ -51,3 +51,23 @@ jobs:
run: |
pip install check-manifest
check-manifest -v

pre-commit:
name: pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/action@v2.0.0
with:
extra_args: --all-files --hook-stage=manual
- 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 --hook-stage=manual"
43 changes: 26 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
ci:
skip: [check-jsonschema]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand Down Expand Up @@ -47,22 +44,33 @@ repos:
hooks:
- id: doc8
args: [--max-line-length=200]
stages: [manual]

# - repo: https://github.com/pycqa/flake8
# rev: 4.0.1
# hooks:
# - id: flake8
# additional_dependencies:
# [
# "flake8-bugbear==20.1.4",
# "flake8-logging-format==0.6.0",
# "flake8-implicit-str-concat==0.2.0",
# ]
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies:
[
"flake8-bugbear==20.1.4",
"flake8-logging-format==0.6.0",
"flake8-implicit-str-concat==0.2.0",
]
stages: [manual]

# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v0.942
# hooks:
# - id: mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
hooks:
- id: mypy
args: ["--config-file", "pyproject.toml"]
stages: [manual]
additional_dependencies: [pytest]
exclude: |
exclude: |
(?x)^(
jupyter_core/tests/.*_config.py |
scripts/jupyter
)$

- repo: https://github.com/sirosen/check-jsonschema
rev: 0.14.2
Expand All @@ -72,3 +80,4 @@ repos:
files: ^\.github/workflows/
types: [yaml]
args: ["--schemafile", "https://json.schemastore.org/github-workflow"]
stages: [manual]
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include COPYING.md
include CONTRIBUTING.md
include README.md
include dev-requirements.txt
include jupyter_core/py.typed

exclude .pre-commit-config.yaml
exclude .git-blame-ignore-revs
Expand Down
11 changes: 1 addition & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,7 @@

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Latex figure (float) alignment
#'figure_align': 'htbp',
}
latex_elements: dict = {}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
Expand Down
2 changes: 1 addition & 1 deletion jupyter_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .version import __version__, version_info
from .version import __version__, version_info # noqa
2 changes: 1 addition & 1 deletion jupyter_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class JupyterParser(argparse.ArgumentParser):
@property
@property # type:ignore[override]
def epilog(self):
"""Add subcommands to epilog on request

Expand Down
54 changes: 30 additions & 24 deletions jupyter_core/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import warnings
from contextlib import contextmanager
from pathlib import Path
from typing import Optional

pjoin = os.path.join

Expand All @@ -43,7 +44,7 @@ def get_home_dir():
return homedir


_dtemps = {}
_dtemps: dict = {}


def _mkdtemp_once(name):
Expand Down Expand Up @@ -159,7 +160,7 @@ def jupyter_path(*subdirs):
['~/.local/jupyter/kernels', '/usr/local/share/jupyter/kernels']
"""

paths = []
paths: list = []

# highest priority is explicit environment variable
if os.environ.get("JUPYTER_PATH"):
Expand All @@ -170,13 +171,16 @@ def jupyter_path(*subdirs):
if site.ENABLE_USER_SITE:
# Check if site.getuserbase() exists to be compatible with virtualenv,
# which often does not have this method.
userbase: Optional[str]
if hasattr(site, "getuserbase"):
userbase = site.getuserbase()
else:
userbase = site.USER_BASE
userdir = os.path.join(userbase, "share", "jupyter")
if userdir not in user:
user.append(userdir)

if userbase:
userdir = os.path.join(userbase, "share", "jupyter")
if userdir not in user:
user.append(userdir)

env = [p for p in ENV_JUPYTER_PATH if p not in SYSTEM_JUPYTER_PATH]

Expand Down Expand Up @@ -225,7 +229,7 @@ def jupyter_config_path():
# jupyter_config_dir makes a blank config when JUPYTER_NO_CONFIG is set.
return [jupyter_config_dir()]

paths = []
paths: list = []

# highest priority is explicit environment variable
if os.environ.get("JUPYTER_CONFIG_PATH"):
Expand All @@ -234,16 +238,18 @@ def jupyter_config_path():
# Next is environment or user, depending on the JUPYTER_PREFER_ENV_PATH flag
user = [jupyter_config_dir()]
if site.ENABLE_USER_SITE:
userbase: Optional[str]
# Check if site.getuserbase() exists to be compatible with virtualenv,
# which often does not have this method.
if hasattr(site, "getuserbase"):
userbase = site.getuserbase()
else:
userbase = site.USER_BASE

userdir = os.path.join(userbase, "etc", "jupyter")
if userdir not in user:
user.append(userdir)
if userbase:
userdir = os.path.join(userbase, "etc", "jupyter")
if userdir not in user:
user.append(userdir)

env = [p for p in ENV_CONFIG_PATH if p not in SYSTEM_CONFIG_PATH]

Expand Down Expand Up @@ -298,7 +304,7 @@ def is_file_hidden_win(abs_path, stat_res=None):
raise

try:
if stat_res.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN:
if stat_res.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN: # type:ignore[attr-defined]
return True
except AttributeError:
# allow AttributeError on PyPy for Windows
Expand Down Expand Up @@ -424,12 +430,12 @@ def win32_restrict_file_to_user(fname):
The path to the file to secure
"""
try:
import win32api
import win32api # type:ignore[import]
except ImportError:
return _win32_restrict_file_to_user_ctypes(fname)

import ntsecuritycon as con
import win32security
import ntsecuritycon as con # type:ignore[import]
import win32security # type:ignore[import]

# everyone, _domain, _type = win32security.LookupAccountName("", "Everyone")
admins = win32security.CreateWellKnownSid(win32security.WinBuiltinAdministratorsSid)
Expand Down Expand Up @@ -470,8 +476,8 @@ def _win32_restrict_file_to_user_ctypes(fname):
import ctypes
from ctypes import wintypes

advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)
secur32 = ctypes.WinDLL("secur32", use_last_error=True)
advapi32 = ctypes.WinDLL("advapi32", use_last_error=True) # type:ignore[attr-defined]
secur32 = ctypes.WinDLL("secur32", use_last_error=True) # type:ignore[attr-defined]

NameSamCompatible = 2
WinBuiltinAdministratorsSid = 26
Expand Down Expand Up @@ -520,7 +526,7 @@ class ACL(ctypes.Structure):

def _nonzero_success(result, func, args):
if not result:
raise ctypes.WinError(ctypes.get_last_error())
raise ctypes.WinError(ctypes.get_last_error()) # type:ignore[attr-defined]
return args

secur32.GetUserNameExW.errcheck = _nonzero_success
Expand Down Expand Up @@ -627,7 +633,7 @@ def CreateWellKnownSid(WellKnownSidType):
try:
advapi32.CreateWellKnownSid(WellKnownSidType, None, pSid, ctypes.byref(cbSid))
except OSError as e:
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
raise
pSid = (ctypes.c_char * cbSid.value)()
advapi32.CreateWellKnownSid(WellKnownSidType, None, pSid, ctypes.byref(cbSid))
Expand All @@ -640,7 +646,7 @@ def GetUserNameEx(NameFormat):
try:
secur32.GetUserNameExW(NameFormat, None, nSize)
except OSError as e:
if e.winerror != ERROR_MORE_DATA:
if e.winerror != ERROR_MORE_DATA: # type:ignore[attr-defined]
raise
if not nSize.contents.value:
return None
Expand All @@ -665,7 +671,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
ctypes.byref(peUse),
)
except OSError as e:
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
raise
Sid = ctypes.create_unicode_buffer("", cbSid.value)
pSid = ctypes.cast(ctypes.pointer(Sid), wintypes.LPVOID)
Expand All @@ -680,7 +686,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
ctypes.byref(peUse),
)
if not success:
raise ctypes.WinError()
raise ctypes.WinError() # type:ignore[attr-defined]
return pSid, lpReferencedDomainName.value, peUse.value

def AddAccessAllowedAce(pAcl, dwAceRevision, AccessMask, pSid):
Expand All @@ -700,7 +706,7 @@ def GetFileSecurity(lpFileName, RequestedInformation):
ctypes.byref(nLength),
)
except OSError as e:
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
raise
if not nLength.value:
return None
Expand Down Expand Up @@ -750,7 +756,7 @@ def MakeAbsoluteSD(pSelfRelativeSecurityDescriptor):
ctypes.byref(lpdwPrimaryGroupSize),
)
except OSError as e:
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
raise
pAbsoluteSecurityDescriptor = (wintypes.BYTE * lpdwAbsoluteSecurityDescriptorSize.value)()
pDaclData = (wintypes.BYTE * lpdwDaclSize.value)()
Expand Down Expand Up @@ -788,7 +794,7 @@ def MakeSelfRelativeSD(pAbsoluteSecurityDescriptor):
ctypes.byref(lpdwBufferLength),
)
except OSError as e:
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
raise
pSelfRelativeSecurityDescriptor = (wintypes.BYTE * lpdwBufferLength.value)()
advapi32.MakeSelfRelativeSD(
Expand Down Expand Up @@ -907,7 +913,7 @@ def issue_insecure_write_warning():
def format_warning(msg, *args, **kwargs):
return str(msg) + "\n"

warnings.formatwarning = format_warning
warnings.formatwarning = format_warning # type:ignore[assignment]
warnings.warn(
"WARNING: Insecure writes have been enabled via environment variable "
"'JUPYTER_ALLOW_INSECURE_WRITES'! If this is not intended, remove the "
Expand Down
Empty file added jupyter_core/py.typed
Empty file.
3 changes: 1 addition & 2 deletions jupyter_core/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import pytest

from jupyter_core import __version__
from jupyter_core.command import list_subcommands
from jupyter_core.paths import (
jupyter_config_dir,
Expand Down Expand Up @@ -95,7 +94,7 @@ def test_paths_json():
output = get_jupyter_output(["--paths", "--json"])
data = json.loads(output)
assert sorted(data) == ["config", "data", "runtime"]
for key, path in data.items():
for _, path in data.items():
assert isinstance(path, list)


Expand Down
Loading