Skip to content

Commit 85f675c

Browse files
committed
Convert type comments to annotations
1 parent 6da7ec1 commit 85f675c

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

sphinxcontrib/websupport/builder.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
:license: BSD, see LICENSE for details.
1010
"""
1111

12+
from __future__ import annotations
13+
1214
from os import path
1315
import posixpath
1416
import shutil
17+
from typing import TYPE_CHECKING, Any
1518

1619
from docutils.io import StringOutput
1720

@@ -24,11 +27,11 @@
2427
from sphinxcontrib.websupport.utils import is_commentable
2528

2629

27-
if False:
28-
# For type annotation
29-
from typing import Any, Dict, Iterable, Tuple # NOQA
30-
from docutils import nodes # NOQA
31-
from sphinx.application import Sphinx # NOQA
30+
if TYPE_CHECKING:
31+
from collections.abc import Iterable
32+
33+
from docutils import nodes
34+
from sphinx.application import Sphinx
3235

3336
RESOURCES = [
3437
'ajax-loader.gif',
@@ -51,8 +54,7 @@ class WebSupportBuilder(PickleHTMLBuilder):
5154
default_translator_class = WebSupportTranslator
5255
versioning_compare = True # for commentable node's uuid stability.
5356

54-
def init(self):
55-
# type: () -> None
57+
def init(self) -> None:
5658
PickleHTMLBuilder.init(self)
5759
# templates are needed for this builder, but the serializing
5860
# builder does not initialize them
@@ -67,20 +69,22 @@ def init(self):
6769
def versioning_method(self):
6870
return is_commentable
6971

70-
def set_webinfo(self, staticdir, virtual_staticdir, search, storage):
71-
# type: (str, str, Any, str) -> None
72+
def set_webinfo(self,
73+
staticdir: str,
74+
virtual_staticdir: str,
75+
search: Any,
76+
storage: str,
77+
) -> None:
7278
self.staticdir = staticdir
7379
self.virtual_staticdir = virtual_staticdir
7480
self.search = search
7581
self.storage = storage
7682

77-
def prepare_writing(self, docnames):
78-
# type: (Iterable[str]) -> None
83+
def prepare_writing(self, docnames: Iterable[str]) -> None:
7984
PickleHTMLBuilder.prepare_writing(self, docnames)
8085
self.globalcontext['no_search_suffix'] = True
8186

82-
def write_doc(self, docname, doctree):
83-
# type: (str, nodes.document) -> None
87+
def write_doc(self, docname: str, doctree: nodes.document) -> None:
8488
destination = StringOutput(encoding='utf-8')
8589
doctree.settings = self.docsettings
8690

@@ -97,30 +101,31 @@ def write_doc(self, docname, doctree):
97101
ctx = self.get_doc_context(docname, body, metatags)
98102
self.handle_page(docname, ctx, event_arg=doctree)
99103

100-
def write_doc_serialized(self, docname, doctree):
101-
# type: (str, nodes.Node) -> None
104+
def write_doc_serialized(self, docname: str, doctree: nodes.Node) -> None:
102105
self.imgpath = '/' + posixpath.join(self.virtual_staticdir, self.imagedir)
103106
self.post_process_images(doctree)
104107
title = self.env.longtitles.get(docname)
105108
title = title and self.render_partial(title)['title'] or ''
106109
self.index_page(docname, doctree, title)
107110

108-
def load_indexer(self, docnames):
109-
# type: (Iterable[str]) -> None
111+
def load_indexer(self, docnames: Iterable[str]) -> None:
110112
self.indexer = self.search # type: ignore
111113
self.indexer.init_indexing(changed=docnames) # type: ignore
112114

113-
def _render_page(self, pagename, addctx, templatename, event_arg=None):
114-
# type: (str, Dict, str, Any) -> Tuple[Dict, Dict]
115+
def _render_page(self,
116+
pagename: str,
117+
addctx: dict,
118+
templatename: str,
119+
event_arg: Any = None,
120+
) -> tuple[dict, dict]:
115121
# This is mostly copied from StandaloneHTMLBuilder. However, instead
116122
# of rendering the template and saving the html, create a context
117123
# dict and pickle it.
118124
ctx = self.globalcontext.copy()
119125
ctx['pagename'] = pagename
120126

121-
def pathto(otheruri, resource=False,
122-
baseuri=self.get_target_uri(pagename)):
123-
# type: (str, bool, str) -> str
127+
def pathto(otheruri: str, resource: bool = False,
128+
baseuri: str = self.get_target_uri(pagename)) -> str:
124129
if resource and '://' in otheruri:
125130
return otheruri
126131
elif not resource:
@@ -156,9 +161,8 @@ def pathto(otheruri, resource=False,
156161

157162
return ctx, doc_ctx
158163

159-
def handle_page(self, pagename, addctx, templatename='page.html',
160-
outfilename=None, event_arg=None):
161-
# type: (str, Dict, str, str | None, Any) -> None
164+
def handle_page(self, pagename: str, addctx: dict, templatename: str = 'page.html',
165+
outfilename: str | None = None, event_arg: Any = None) -> None:
162166
ctx, doc_ctx = self._render_page(pagename, addctx,
163167
templatename, event_arg)
164168

@@ -176,8 +180,7 @@ def handle_page(self, pagename, addctx, templatename='page.html',
176180
ensuredir(path.dirname(source_name))
177181
copyfile(self.env.doc2path(pagename), source_name)
178182

179-
def handle_finish(self):
180-
# type: () -> None
183+
def handle_finish(self) -> None:
181184
# get global values for css and script files
182185
_, doc_ctx = self._render_page('tmp', {}, 'page.html')
183186
self.globalcontext['css'] = doc_ctx['css']
@@ -196,8 +199,7 @@ def handle_finish(self):
196199
shutil.move(src, dst)
197200
self.copy_resources()
198201

199-
def copy_resources(self):
200-
# type: () -> None
202+
def copy_resources(self) -> None:
201203
# copy resource files to static dir
202204
dst = path.join(self.staticdir, '_static')
203205

@@ -206,13 +208,11 @@ def copy_resources(self):
206208
src = path.join(package_dir, 'files', resource)
207209
shutil.copy(src, dst)
208210

209-
def dump_search_index(self):
210-
# type: () -> None
211+
def dump_search_index(self) -> None:
211212
self.indexer.finish_indexing() # type: ignore
212213

213214

214-
def setup(app):
215-
# type: (Sphinx) -> Dict[str, Any]
215+
def setup(app: Sphinx) -> dict[str, Any]:
216216
app.add_builder(WebSupportBuilder)
217217

218218
return {

sphinxcontrib/websupport/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
:license: BSD, see LICENSE for details.
1010
"""
1111

12+
from __future__ import annotations
13+
1214
import html
1315
import sys
1416
import pickle
@@ -31,10 +33,6 @@
3133
except ImportError:
3234
from sphinx.util.jsonimpl import dumps as dump_json
3335

34-
if False:
35-
# For type annotation
36-
from typing import Dict # NOQA
37-
3836

3937
class WebSupport(object):
4038
"""The main API class for the web support package. All interactions
@@ -402,7 +400,7 @@ def _make_base_comment_options(self):
402400
that remains the same throughout the lifetime of the
403401
:class:`~sphinxcontrib.websupport.WebSupport` object.
404402
"""
405-
self.base_comment_opts = {} # type: Dict[str, str]
403+
self.base_comment_opts: dict[str, str] = {}
406404

407405
if self.docroot != '':
408406
comment_urls = [

sphinxcontrib/websupport/storage/sqlalchemy_db.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
:license: BSD, see LICENSE for details.
1111
"""
1212

13+
from __future__ import annotations
14+
1315
from datetime import datetime
1416

1517
from sqlalchemy import Column, Integer, Text, String, Boolean, \
1618
ForeignKey, DateTime
1719
from sqlalchemy.orm import relation, sessionmaker, aliased
1820
from sqlalchemy.ext.declarative import declarative_base
1921

20-
if False:
21-
# For type annotation
22-
from typing import List # NOQA
23-
2422
Base = declarative_base()
2523
Session = sessionmaker()
2624

@@ -78,7 +76,7 @@ def _nest_comments(self, results, username):
7876
:param results: the flat list of comments
7977
:param username: the name of the user requesting the comments.
8078
"""
81-
comments = [] # type: List
79+
comments = []
8280
list_stack = [comments]
8381
for r in results:
8482
if username:

sphinxcontrib/websupport/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
77
:license: BSD, see LICENSE for details.
88
"""
9+
from __future__ import annotations
910

10-
if False:
11-
# For type annotation
12-
from docutils import nodes # NOQA
11+
from typing import TYPE_CHECKING
1312

13+
if TYPE_CHECKING:
14+
from docutils import nodes
1415

15-
def is_commentable(node):
16-
# type: (nodes.Node) -> bool
16+
17+
def is_commentable(node: nodes.Node) -> bool:
1718
# return node.__class__.__name__ in ('paragraph', 'literal_block')
1819
return node.__class__.__name__ == 'paragraph'

0 commit comments

Comments
 (0)