Skip to content

Commit 2dc7771

Browse files
committed
Convert type comments to annotations
1 parent 14b2931 commit 2dc7771

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

sphinxcontrib/serializinghtml/__init__.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
:license: BSD, see LICENSE for details.
77
"""
88

9+
from __future__ import annotations
10+
911
import pickle
1012
import types
1113
from os import path
12-
from typing import Any, Dict
14+
from typing import Any
1315

1416
from sphinx.application import ENV_PICKLE_FILENAME, Sphinx
1517
from sphinx.builders.html import BuildInfo, StandaloneHTMLBuilder
@@ -18,10 +20,6 @@
1820

1921
from sphinxcontrib.serializinghtml import jsonimpl
2022

21-
if False:
22-
# For type annotation
23-
from typing import Any, Dict, Tuple # NOQA
24-
2523
__version__ = '1.1.6'
2624
__version_info__ = (1, 1, 6)
2725

@@ -41,19 +39,18 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder):
4139
#: the serializing implementation to use. Set this to a module that
4240
#: implements a `dump`, `load`, `dumps` and `loads` functions
4341
#: (pickle, simplejson etc.)
44-
implementation = None # type: Any
42+
implementation: Any = None
4543
implementation_dumps_unicode = False
4644
#: additional arguments for dump()
47-
additional_dump_args = () # type: Tuple
45+
additional_dump_args = ()
4846

4947
#: the filename for the global context file
50-
globalcontext_filename = None # type: str
48+
globalcontext_filename: str | None = None
5149

5250
supported_image_types = ['image/svg+xml', 'image/png',
5351
'image/gif', 'image/jpeg']
5452

55-
def init(self):
56-
# type: () -> None
53+
def init(self) -> None:
5754
self.build_info = BuildInfo(self.config, self.tags)
5855
self.imagedir = '_images'
5956
self.current_docname = ''
@@ -65,26 +62,23 @@ def init(self):
6562
self.init_js_files()
6663
self.use_index = self.get_builder_config('use_index', 'html')
6764

68-
def get_target_uri(self, docname, typ=None):
69-
# type: (str, str | None) -> str
65+
def get_target_uri(self, docname: str, typ: str | None = None) -> str:
7066
if docname == 'index':
7167
return ''
7268
if docname.endswith(SEP + 'index'):
7369
return docname[:-5] # up to sep
7470
return docname + SEP
7571

76-
def dump_context(self, context, filename):
77-
# type: (Dict, str) -> None
72+
def dump_context(self, context: dict, filename: str) -> None:
7873
if self.implementation_dumps_unicode:
7974
with open(filename, 'w', encoding='utf-8') as ft:
8075
self.implementation.dump(context, ft, *self.additional_dump_args)
8176
else:
8277
with open(filename, 'wb') as fb:
8378
self.implementation.dump(context, fb, *self.additional_dump_args)
8479

85-
def handle_page(self, pagename, ctx, templatename='page.html',
86-
outfilename=None, event_arg=None):
87-
# type: (str, Dict, str, str | None, Any) -> None
80+
def handle_page(self, pagename: str, ctx: dict, templatename: str = 'page.html',
81+
outfilename: str | None = None, event_arg: Any = None) -> None:
8882
ctx['current_page_name'] = pagename
8983
ctx.setdefault('pathto', lambda p: p)
9084
self.add_sidebars(pagename, ctx)
@@ -113,8 +107,7 @@ def handle_page(self, pagename, ctx, templatename='page.html',
113107
ensuredir(path.dirname(source_name))
114108
copyfile(self.env.doc2path(pagename), source_name)
115109

116-
def handle_finish(self):
117-
# type: () -> None
110+
def handle_finish(self) -> None:
118111
# dump the global context
119112
outfilename = path.join(self.outdir, self.globalcontext_filename)
120113
self.dump_context(self.globalcontext, outfilename)
@@ -165,7 +158,7 @@ class JSONHTMLBuilder(SerializingHTMLBuilder):
165158
searchindex_filename = 'searchindex.json'
166159

167160

168-
def setup(app: Sphinx) -> Dict[str, Any]:
161+
def setup(app: Sphinx) -> dict[str, Any]:
169162
app.setup_extension('sphinx.builders.html')
170163
app.add_builder(JSONHTMLBuilder)
171164
app.add_builder(PickleHTMLBuilder)

sphinxcontrib/serializinghtml/jsonimpl.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,34 @@
88
:license: BSD, see LICENSE for details.
99
"""
1010

11+
from __future__ import annotations
12+
1113
import json
1214
from collections import UserString
13-
14-
if False:
15-
# For type annotation
16-
from typing import Any, IO # NOQA
15+
from typing import Any, IO
1716

1817

1918
class SphinxJSONEncoder(json.JSONEncoder):
2019
"""JSONEncoder subclass that forces translation proxies."""
21-
def default(self, obj):
22-
# type: (Any) -> str
20+
def default(self, obj: Any) -> str:
2321
if isinstance(obj, UserString):
2422
return str(obj)
2523
return super().default(obj)
2624

2725

28-
def dump(obj, fp, *args, **kwds):
29-
# type: (Any, IO, Any, Any) -> None
26+
def dump(obj: Any, fp: IO, *args: Any, **kwds: Any) -> None:
3027
kwds['cls'] = SphinxJSONEncoder
3128
json.dump(obj, fp, *args, **kwds)
3229

3330

34-
def dumps(obj, *args, **kwds):
35-
# type: (Any, Any, Any) -> str
31+
def dumps(obj: Any, *args: Any, **kwds: Any) -> str:
3632
kwds['cls'] = SphinxJSONEncoder
3733
return json.dumps(obj, *args, **kwds)
3834

3935

40-
def load(*args, **kwds):
41-
# type: (Any, Any) -> Any
36+
def load(*args: Any, **kwds: Any) -> Any:
4237
return json.load(*args, **kwds)
4338

4439

45-
def loads(*args, **kwds):
46-
# type: (Any, Any) -> Any
40+
def loads(*args: Any, **kwds: Any) -> Any:
4741
return json.loads(*args, **kwds)

0 commit comments

Comments
 (0)