Skip to content

Commit 73eb091

Browse files
authored
Merge pull request #256 from blink1073/flake8-mypy
Add flake8 and mypy settings
2 parents 071adf8 + 9925476 commit 73eb091

File tree

14 files changed

+160
-88
lines changed

14 files changed

+160
-88
lines changed

.github/workflows/test.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
os: [ubuntu-latest, windows-latest, macos-latest]
18-
python-version: [3.6, 3.7, 3.8, 3.9, 3.10-dev, pypy-3.7]
18+
python-version: [3.7, 3.8, 3.9, "3.10", pypy-3.7]
1919
exclude:
2020
# pywin32 not available
2121
- os: windows-latest
22-
python-version: 3.10-dev
22+
python-version: "3.10"
2323
steps:
2424
- uses: actions/checkout@v2
2525
- name: Set up Python ${{ matrix.python-version }}
@@ -51,3 +51,23 @@ jobs:
5151
run: |
5252
pip install check-manifest
5353
check-manifest -v
54+
55+
pre-commit:
56+
name: pre-commit
57+
runs-on: ubuntu-latest
58+
steps:
59+
- uses: actions/checkout@v2
60+
- uses: actions/setup-python@v2
61+
- uses: pre-commit/[email protected]
62+
with:
63+
extra_args: --all-files --hook-stage=manual
64+
- name: Help message if pre-commit fail
65+
if: ${{ failure() }}
66+
run: |
67+
echo "You can install pre-commit hooks to automatically run formatting"
68+
echo "on each commit with:"
69+
echo " pre-commit install"
70+
echo "or you can run by hand on staged files with"
71+
echo " pre-commit run"
72+
echo "or after-the-fact on already committed files with"
73+
echo " pre-commit run --all-files --hook-stage=manual"

.pre-commit-config.yaml

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
ci:
2-
skip: [check-jsonschema]
3-
41
repos:
52
- repo: https://github.com/pre-commit/pre-commit-hooks
63
rev: v4.2.0
@@ -47,22 +44,33 @@ repos:
4744
hooks:
4845
- id: doc8
4946
args: [--max-line-length=200]
47+
stages: [manual]
5048

51-
# - repo: https://github.com/pycqa/flake8
52-
# rev: 4.0.1
53-
# hooks:
54-
# - id: flake8
55-
# additional_dependencies:
56-
# [
57-
# "flake8-bugbear==20.1.4",
58-
# "flake8-logging-format==0.6.0",
59-
# "flake8-implicit-str-concat==0.2.0",
60-
# ]
49+
- repo: https://github.com/pycqa/flake8
50+
rev: 4.0.1
51+
hooks:
52+
- id: flake8
53+
additional_dependencies:
54+
[
55+
"flake8-bugbear==20.1.4",
56+
"flake8-logging-format==0.6.0",
57+
"flake8-implicit-str-concat==0.2.0",
58+
]
59+
stages: [manual]
6160

62-
# - repo: https://github.com/pre-commit/mirrors-mypy
63-
# rev: v0.942
64-
# hooks:
65-
# - id: mypy
61+
- repo: https://github.com/pre-commit/mirrors-mypy
62+
rev: v0.942
63+
hooks:
64+
- id: mypy
65+
args: ["--config-file", "pyproject.toml"]
66+
stages: [manual]
67+
additional_dependencies: [pytest]
68+
exclude: |
69+
exclude: |
70+
(?x)^(
71+
jupyter_core/tests/.*_config.py |
72+
scripts/jupyter
73+
)$
6674
6775
- repo: https://github.com/sirosen/check-jsonschema
6876
rev: 0.14.2
@@ -72,3 +80,4 @@ repos:
7280
files: ^\.github/workflows/
7381
types: [yaml]
7482
args: ["--schemafile", "https://json.schemastore.org/github-workflow"]
83+
stages: [manual]

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include COPYING.md
22
include CONTRIBUTING.md
33
include README.md
44
include dev-requirements.txt
5+
include jupyter_core/py.typed
56

67
exclude .pre-commit-config.yaml
78
exclude .git-blame-ignore-revs

docs/conf.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,7 @@
208208

209209
# -- Options for LaTeX output ---------------------------------------------
210210

211-
latex_elements = {
212-
# The paper size ('letterpaper' or 'a4paper').
213-
#'papersize': 'letterpaper',
214-
# The font size ('10pt', '11pt' or '12pt').
215-
#'pointsize': '10pt',
216-
# Additional stuff for the LaTeX preamble.
217-
#'preamble': '',
218-
# Latex figure (float) alignment
219-
#'figure_align': 'htbp',
220-
}
211+
latex_elements: dict = {}
221212

222213
# Grouping the document tree into LaTeX files. List of tuples
223214
# (source start file, target name, title,

jupyter_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .version import __version__, version_info
1+
from .version import __version__, version_info # noqa

jupyter_core/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class JupyterParser(argparse.ArgumentParser):
24-
@property
24+
@property # type:ignore[override]
2525
def epilog(self):
2626
"""Add subcommands to epilog on request
2727

jupyter_core/paths.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import warnings
1818
from contextlib import contextmanager
1919
from pathlib import Path
20+
from typing import Optional
2021

2122
pjoin = os.path.join
2223

@@ -43,7 +44,7 @@ def get_home_dir():
4344
return homedir
4445

4546

46-
_dtemps = {}
47+
_dtemps: dict = {}
4748

4849

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

162-
paths = []
163+
paths: list = []
163164

164165
# highest priority is explicit environment variable
165166
if os.environ.get("JUPYTER_PATH"):
@@ -170,13 +171,16 @@ def jupyter_path(*subdirs):
170171
if site.ENABLE_USER_SITE:
171172
# Check if site.getuserbase() exists to be compatible with virtualenv,
172173
# which often does not have this method.
174+
userbase: Optional[str]
173175
if hasattr(site, "getuserbase"):
174176
userbase = site.getuserbase()
175177
else:
176178
userbase = site.USER_BASE
177-
userdir = os.path.join(userbase, "share", "jupyter")
178-
if userdir not in user:
179-
user.append(userdir)
179+
180+
if userbase:
181+
userdir = os.path.join(userbase, "share", "jupyter")
182+
if userdir not in user:
183+
user.append(userdir)
180184

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

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

228-
paths = []
232+
paths: list = []
229233

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

244-
userdir = os.path.join(userbase, "etc", "jupyter")
245-
if userdir not in user:
246-
user.append(userdir)
249+
if userbase:
250+
userdir = os.path.join(userbase, "etc", "jupyter")
251+
if userdir not in user:
252+
user.append(userdir)
247253

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

@@ -298,7 +304,7 @@ def is_file_hidden_win(abs_path, stat_res=None):
298304
raise
299305

300306
try:
301-
if stat_res.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN:
307+
if stat_res.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN: # type:ignore[attr-defined]
302308
return True
303309
except AttributeError:
304310
# allow AttributeError on PyPy for Windows
@@ -424,12 +430,12 @@ def win32_restrict_file_to_user(fname):
424430
The path to the file to secure
425431
"""
426432
try:
427-
import win32api
433+
import win32api # type:ignore[import]
428434
except ImportError:
429435
return _win32_restrict_file_to_user_ctypes(fname)
430436

431-
import ntsecuritycon as con
432-
import win32security
437+
import ntsecuritycon as con # type:ignore[import]
438+
import win32security # type:ignore[import]
433439

434440
# everyone, _domain, _type = win32security.LookupAccountName("", "Everyone")
435441
admins = win32security.CreateWellKnownSid(win32security.WinBuiltinAdministratorsSid)
@@ -470,8 +476,8 @@ def _win32_restrict_file_to_user_ctypes(fname):
470476
import ctypes
471477
from ctypes import wintypes
472478

473-
advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)
474-
secur32 = ctypes.WinDLL("secur32", use_last_error=True)
479+
advapi32 = ctypes.WinDLL("advapi32", use_last_error=True) # type:ignore[attr-defined]
480+
secur32 = ctypes.WinDLL("secur32", use_last_error=True) # type:ignore[attr-defined]
475481

476482
NameSamCompatible = 2
477483
WinBuiltinAdministratorsSid = 26
@@ -520,7 +526,7 @@ class ACL(ctypes.Structure):
520526

521527
def _nonzero_success(result, func, args):
522528
if not result:
523-
raise ctypes.WinError(ctypes.get_last_error())
529+
raise ctypes.WinError(ctypes.get_last_error()) # type:ignore[attr-defined]
524530
return args
525531

526532
secur32.GetUserNameExW.errcheck = _nonzero_success
@@ -627,7 +633,7 @@ def CreateWellKnownSid(WellKnownSidType):
627633
try:
628634
advapi32.CreateWellKnownSid(WellKnownSidType, None, pSid, ctypes.byref(cbSid))
629635
except OSError as e:
630-
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
636+
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
631637
raise
632638
pSid = (ctypes.c_char * cbSid.value)()
633639
advapi32.CreateWellKnownSid(WellKnownSidType, None, pSid, ctypes.byref(cbSid))
@@ -640,7 +646,7 @@ def GetUserNameEx(NameFormat):
640646
try:
641647
secur32.GetUserNameExW(NameFormat, None, nSize)
642648
except OSError as e:
643-
if e.winerror != ERROR_MORE_DATA:
649+
if e.winerror != ERROR_MORE_DATA: # type:ignore[attr-defined]
644650
raise
645651
if not nSize.contents.value:
646652
return None
@@ -665,7 +671,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
665671
ctypes.byref(peUse),
666672
)
667673
except OSError as e:
668-
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
674+
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
669675
raise
670676
Sid = ctypes.create_unicode_buffer("", cbSid.value)
671677
pSid = ctypes.cast(ctypes.pointer(Sid), wintypes.LPVOID)
@@ -680,7 +686,7 @@ def LookupAccountName(lpSystemName, lpAccountName):
680686
ctypes.byref(peUse),
681687
)
682688
if not success:
683-
raise ctypes.WinError()
689+
raise ctypes.WinError() # type:ignore[attr-defined]
684690
return pSid, lpReferencedDomainName.value, peUse.value
685691

686692
def AddAccessAllowedAce(pAcl, dwAceRevision, AccessMask, pSid):
@@ -700,7 +706,7 @@ def GetFileSecurity(lpFileName, RequestedInformation):
700706
ctypes.byref(nLength),
701707
)
702708
except OSError as e:
703-
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
709+
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
704710
raise
705711
if not nLength.value:
706712
return None
@@ -750,7 +756,7 @@ def MakeAbsoluteSD(pSelfRelativeSecurityDescriptor):
750756
ctypes.byref(lpdwPrimaryGroupSize),
751757
)
752758
except OSError as e:
753-
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
759+
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
754760
raise
755761
pAbsoluteSecurityDescriptor = (wintypes.BYTE * lpdwAbsoluteSecurityDescriptorSize.value)()
756762
pDaclData = (wintypes.BYTE * lpdwDaclSize.value)()
@@ -788,7 +794,7 @@ def MakeSelfRelativeSD(pAbsoluteSecurityDescriptor):
788794
ctypes.byref(lpdwBufferLength),
789795
)
790796
except OSError as e:
791-
if e.winerror != ERROR_INSUFFICIENT_BUFFER:
797+
if e.winerror != ERROR_INSUFFICIENT_BUFFER: # type:ignore[attr-defined]
792798
raise
793799
pSelfRelativeSecurityDescriptor = (wintypes.BYTE * lpdwBufferLength.value)()
794800
advapi32.MakeSelfRelativeSD(
@@ -907,7 +913,7 @@ def issue_insecure_write_warning():
907913
def format_warning(msg, *args, **kwargs):
908914
return str(msg) + "\n"
909915

910-
warnings.formatwarning = format_warning
916+
warnings.formatwarning = format_warning # type:ignore[assignment]
911917
warnings.warn(
912918
"WARNING: Insecure writes have been enabled via environment variable "
913919
"'JUPYTER_ALLOW_INSECURE_WRITES'! If this is not intended, remove the "

jupyter_core/py.typed

Whitespace-only changes.

jupyter_core/tests/test_command.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import pytest
1111

12-
from jupyter_core import __version__
1312
from jupyter_core.command import list_subcommands
1413
from jupyter_core.paths import (
1514
jupyter_config_dir,
@@ -95,7 +94,7 @@ def test_paths_json():
9594
output = get_jupyter_output(["--paths", "--json"])
9695
data = json.loads(output)
9796
assert sorted(data) == ["config", "data", "runtime"]
98-
for key, path in data.items():
97+
for _, path in data.items():
9998
assert isinstance(path, list)
10099

101100

0 commit comments

Comments
 (0)