Skip to content

Commit 892cc8f

Browse files
committed
More cleanup, + mypy
Replace ImportError with ModuleNotFoundError, and cleanup a number of types
1 parent 1052ba6 commit 892cc8f

File tree

8 files changed

+28
-32
lines changed

8 files changed

+28
-32
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ jobs:
3535
- name: install mypy
3636
run: |
3737
python3 -m pip install mypy types-pycurl types-requests types-Markdown
38+
- name: run mypy
39+
run: |
40+
mypy nbviewer
3841
3942
- name: pip freeze
4043
run: |

nbviewer/app.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import markdown
1717
from jinja2 import Environment
1818
from jinja2 import FileSystemLoader
19-
from nbconvert import get_exporter
20-
from nbconvert.exporters.templateexporter import ExtensionTolerantLoader
19+
from nbconvert import get_exporter #type: ignore
20+
from nbconvert.exporters.templateexporter import ExtensionTolerantLoader #type: ignore
2121
from tornado import httpserver
2222
from tornado import ioloop
2323
from tornado import web
@@ -51,12 +51,9 @@
5151
from .utils import jupyter_info
5252
from .utils import url_path_join
5353

54-
try: # Python 3.8
55-
from functools import cached_property
56-
except ImportError:
57-
from .utils import cached_property
54+
from functools import cached_property
5855

59-
from jupyter_server.base.handlers import FileFindHandler as StaticFileHandler
56+
from jupyter_server.base.handlers import FileFindHandler as StaticFileHandler #type: ignore
6057

6158
# -----------------------------------------------------------------------------
6259
# Code
@@ -68,16 +65,16 @@
6865

6966
def nrhead():
7067
try:
71-
import newrelic.agent
72-
except ImportError:
68+
import newrelic.agent #type: ignore
69+
except ModuleNotFoundError:
7370
return ""
7471
return newrelic.agent.get_browser_timing_header()
7572

7673

7774
def nrfoot():
7875
try:
7976
import newrelic.agent
80-
except ImportError:
77+
except ModuleNotFoundError:
8178
return ""
8279
return newrelic.agent.get_browser_timing_footer()
8380

@@ -90,7 +87,7 @@ class NBViewer(Application):
9087

9188
name = Unicode("NBViewer")
9289

93-
aliases = Dict(
90+
aliases = Dict( #type: ignore
9491
{
9592
"base-url": "NBViewer.base_url",
9693
"binder-base-url": "NBViewer.binder_base_url",
@@ -129,7 +126,7 @@ class NBViewer(Application):
129126
}
130127
)
131128

132-
flags = Dict(
129+
flags = Dict( #type: ignore
133130
{
134131
"debug": (
135132
{"Application": {"log_level": logging.DEBUG}},
@@ -533,7 +530,7 @@ def frontpage_setup(self):
533530

534531
# Attribute inherited from traitlets.config.Application, automatically used to style logs
535532
# https://github.com/ipython/traitlets/blob/master/traitlets/config/application.py#L191
536-
_log_formatter_cls = LogFormatter
533+
_log_formatter_cls = LogFormatter #type: ignore
537534
# Need Tornado LogFormatter for color logs, keys 'color' and 'end_color' in log_format
538535

539536
# Observed traitlet inherited again from traitlets.config.Application

nbviewer/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from tornado.log import app_log
1414

1515
try:
16-
import pylibmc
17-
except ImportError:
18-
pylibmc = None
16+
import pylibmc #type: ignore
17+
except ModuleNotFoundError:
18+
pylibmc = None #type: ignore
1919

2020
# -----------------------------------------------------------------------------
2121
# Code

nbviewer/providers/base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from urllib.parse import urlparse
2020
from urllib.parse import urlunparse
2121

22-
import statsd
23-
from nbformat import current_nbformat
22+
import statsd #type: ignore
23+
from nbformat import current_nbformat #type:ignore
2424
from nbformat import reads
2525
from tornado import httpclient
2626
from tornado import web
@@ -40,10 +40,10 @@
4040
try:
4141
import pycurl
4242
from tornado.curl_httpclient import CurlError
43-
except ImportError:
44-
pycurl = None
43+
except ModuleNotFoundError:
44+
pycurl = None #type: ignore
4545

46-
class CurlError(Exception):
46+
class CurlError(Exception): #type: ignore
4747
pass
4848

4949

@@ -415,12 +415,12 @@ async def fetch(self, url, **overrides):
415415
response = await self.client.fetch(url, **kw)
416416
return response
417417

418-
def write_error(self, status_code: int, exc_info, **kwargs):
418+
def write_error(self, status_code: int, **kwargs):
419419
"""render custom error pages"""
420-
message = ""
420+
exc_info = kwargs.get('exc_info', None)
421+
message:str = ""
421422
status_message = responses.get(status_code, "Unknown")
422423
exception = None
423-
message = None
424424
if exc_info:
425425
# get the custom message, if defined
426426
exception = exc_info[1]

nbviewer/providers/github/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ async def get(self, user: str, repo: str, ref, path):
215215
assert isinstance(repo, str)
216216
assert isinstance(ref, str)
217217
assert isinstance(path, str)
218-
if not self.request.uri.endswith("/"):
218+
if isinstance(self.request.uri, str) and not self.request.uri.endswith("/"):
219219
self.redirect(self.request.uri + "/")
220220
return
221221
path = path.rstrip("/")

nbviewer/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Distributed under the terms of the BSD License. The full license is in
55
# the file COPYING, distributed as part of this software.
66
# -----------------------------------------------------------------------------
7-
from nbconvert.exporters import Exporter
7+
from nbconvert.exporters import Exporter #type: ignore
88
from tornado.log import app_log
99

1010
# -----------------------------------------------------------------------------

nbviewer/tests/test_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44
from unittest import TestCase
55

6-
from jsonschema import validate
6+
from jsonschema import validate #type: ignore
77

88

99
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))

nbviewer/utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def git_info(path, force_git=False):
199199

200200
def jupyter_info():
201201
"""Get Jupyter info dict"""
202-
import nbconvert
202+
import nbconvert #type: ignore
203203

204204
return dict(nbconvert_version=nbconvert.__version__)
205205

@@ -241,7 +241,3 @@ def time_block(message, logger, debug_limit=1):
241241
dt = time.time() - tic
242242
log = logger.info if dt > debug_limit else logger.debug
243243
log("%s in %.2f ms", message, 1e3 * dt)
244-
245-
246-
def cached_property(method):
247-
return property(lru_cache(1)(method))

0 commit comments

Comments
 (0)