Skip to content

Commit

Permalink
add flake8 and mypy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Apr 7, 2022
1 parent 6efee94 commit 9d9455c
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 78 deletions.
34 changes: 20 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,27 @@ repos:
- id: doc8
args: [--max-line-length=200]

# - 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",
]

# - 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
exclude: |
exclude: |
(?x)^(
jupyter_core/tests/.*_config.py |
scripts/jupyter
)$
- repo: https://github.com/sirosen/check-jsonschema
rev: 0.14.2
Expand Down
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
4 changes: 2 additions & 2 deletions 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 All @@ -30,7 +30,7 @@ def epilog(self):
return "Available subcommands: %s" % " ".join(list_subcommands())

@epilog.setter
def epilog(self, x):
def epilog(self):
"""Ignore epilog set in Parser.__init__"""
pass

Expand Down
48 changes: 27 additions & 21 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 @@ -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
13 changes: 6 additions & 7 deletions jupyter_core/tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from jupyter_core import paths
from jupyter_core.paths import (
ENV_JUPYTER_PATH,
is_file_hidden,
is_hidden,
jupyter_config_dir,
Expand Down Expand Up @@ -148,12 +147,12 @@ def test_data_dir_darwin():
@pytest.mark.skipif(sys.platform != "win32", reason="only run on windows")
def test_data_dir_windows():
data = jupyter_data_dir()
assert data == realpath(pjoin(os.environ.get("APPDATA", None), "jupyter"))
assert data == realpath(pjoin(os.environ.get("APPDATA", ""), "jupyter"))

with xdg:
# windows should ignore xdg
data = jupyter_data_dir()
assert data == realpath(pjoin(os.environ.get("APPDATA", None), "jupyter"))
assert data == realpath(pjoin(os.environ.get("APPDATA", ""), "jupyter"))


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
Expand Down Expand Up @@ -189,12 +188,12 @@ def test_runtime_dir_darwin():
@pytest.mark.skipif(sys.platform != "win32", reason="only run on windows")
def test_runtime_dir_windows():
runtime = jupyter_runtime_dir()
assert runtime == realpath(pjoin(os.environ.get("APPDATA", None), "jupyter", "runtime"))
assert runtime == realpath(pjoin(os.environ.get("APPDATA", ""), "jupyter", "runtime"))

with xdg:
# windows should ignore xdg
runtime = jupyter_runtime_dir()
assert runtime == realpath(pjoin(os.environ.get("APPDATA", None), "jupyter", "runtime"))
assert runtime == realpath(pjoin(os.environ.get("APPDATA", ""), "jupyter", "runtime"))


@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
Expand Down Expand Up @@ -364,7 +363,7 @@ def test_is_hidden():
reason="only run on windows/cpython or pypy >= 7.3.6: https://foss.heptapod.net/pypy/pypy/-/issues/3469",
)
def test_is_hidden_win32_cpython():
import ctypes
import ctypes # noqa

with tempfile.TemporaryDirectory() as root:
subdir1 = os.path.join(root, "subdir")
Expand All @@ -384,7 +383,7 @@ def test_is_hidden_win32_cpython():
reason="only run on windows/pypy < 7.3.6: https://foss.heptapod.net/pypy/pypy/-/issues/3469",
)
def test_is_hidden_win32_pypy():
import ctypes
import ctypes # noqa

with tempfile.TemporaryDirectory() as root:
subdir1 = os.path.join(root, "subdir")
Expand Down
Loading

0 comments on commit 9d9455c

Please sign in to comment.