Skip to content

Commit b014cc0

Browse files
authored
Merge pull request #252 from ultrabear/master
Add some typing (found with mypy --strict)
2 parents 94c422a + 39d4219 commit b014cc0

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

discord/__main__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import sys
2828
from pathlib import Path
2929

30+
from typing import Tuple
31+
3032
import discord
3133
import pkg_resources
3234
import aiohttp
3335
import platform
3436

35-
def show_version():
37+
def show_version() -> None:
3638
entries = []
3739

3840
entries.append('- Python v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}'.format(sys.version_info))
@@ -48,7 +50,7 @@ def show_version():
4850
entries.append('- system info: {0.system} {0.release} {0.version}'.format(uname))
4951
print('\n'.join(entries))
5052

51-
def core(parser, args):
53+
def core(parser, args) -> None:
5254
if args.version:
5355
show_version()
5456

@@ -173,7 +175,7 @@ async def cog_after_invoke(self, ctx):
173175

174176
_translation_table = str.maketrans(_base_table)
175177

176-
def to_path(parser, name, *, replace_spaces=False):
178+
def to_path(parser, name, *, replace_spaces=False) -> Path:
177179
if isinstance(name, Path):
178180
return name
179181

@@ -188,7 +190,7 @@ def to_path(parser, name, *, replace_spaces=False):
188190
name = name.replace(' ', '-')
189191
return Path(name)
190192

191-
def newbot(parser, args):
193+
def newbot(parser, args) -> None:
192194
new_directory = to_path(parser, args.directory) / to_path(parser, args.name)
193195

194196
# as a note exist_ok for Path is a 3.5+ only feature
@@ -229,7 +231,7 @@ def newbot(parser, args):
229231

230232
print('successfully made bot at', new_directory)
231233

232-
def newcog(parser, args):
234+
def newcog(parser, args) -> None:
233235
cog_dir = to_path(parser, args.directory)
234236
try:
235237
cog_dir.mkdir(exist_ok=True)
@@ -262,7 +264,7 @@ def newcog(parser, args):
262264
else:
263265
print('successfully made cog at', directory)
264266

265-
def add_newbot_args(subparser):
267+
def add_newbot_args(subparser: argparse._SubParsersAction) -> None:
266268
parser = subparser.add_parser('newbot', help='creates a command bot project quickly')
267269
parser.set_defaults(func=newbot)
268270

@@ -272,7 +274,7 @@ def add_newbot_args(subparser):
272274
parser.add_argument('--sharded', help='whether to use AutoShardedBot', action='store_true')
273275
parser.add_argument('--no-git', help='do not create a .gitignore file', action='store_true', dest='no_git')
274276

275-
def add_newcog_args(subparser):
277+
def add_newcog_args(subparser: argparse._SubParsersAction) -> None:
276278
parser = subparser.add_parser('newcog', help='creates a new cog template quickly')
277279
parser.set_defaults(func=newcog)
278280

@@ -283,7 +285,7 @@ def add_newcog_args(subparser):
283285
parser.add_argument('--hide-commands', help='whether to hide all commands in the cog', action='store_true')
284286
parser.add_argument('--full', help='add all special methods as well', action='store_true')
285287

286-
def parse_args():
288+
def parse_args() -> Tuple[argparse.ArgumentParser, argparse.Namespace]:
287289
parser = argparse.ArgumentParser(prog='discord', description='Tools for helping with discord.py')
288290
parser.add_argument('-v', '--version', action='store_true', help='shows the library version')
289291
parser.set_defaults(func=core)
@@ -293,7 +295,7 @@ def parse_args():
293295
add_newcog_args(subparser)
294296
return parser, parser.parse_args()
295297

296-
def main():
298+
def main() -> None:
297299
parser, args = parse_args()
298300
args.func(parser, args)
299301

docs/extensions/builder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
from sphinx.writers.html5 import HTML5Translator
44

55
class DPYHTML5Translator(HTML5Translator):
6-
def visit_section(self, node):
6+
def visit_section(self, node) -> None:
77
self.section_level += 1
88
self.body.append(
99
self.starttag(node, 'section'))
1010

11-
def depart_section(self, node):
11+
def depart_section(self, node) -> None:
1212
self.section_level -= 1
1313
self.body.append('</section>\n')
1414

15-
def visit_table(self, node):
15+
def visit_table(self, node) -> None:
1616
self.body.append('<div class="table-wrapper">')
1717
super().visit_table(node)
1818

19-
def depart_table(self, node):
19+
def depart_table(self, node) -> None:
2020
super().depart_table(node)
2121
self.body.append('</div>')
2222

@@ -51,12 +51,12 @@ def write_genindex(self) -> None:
5151
self.handle_page('genindex', genindexcontext, 'genindex.html')
5252

5353

54-
def add_custom_jinja2(app):
54+
def add_custom_jinja2(app) -> None:
5555
env = app.builder.templates.environment
5656
env.tests['prefixedwith'] = str.startswith
5757
env.tests['suffixedwith'] = str.endswith
5858

59-
def add_builders(app):
59+
def add_builders(app) -> None:
6060
"""This is necessary because RTD injects their own for some reason."""
6161
app.set_translator('html', DPYHTML5Translator, override=True)
6262
app.add_builder(DPYStandaloneHTMLBuilder, override=True)
@@ -72,6 +72,6 @@ def add_builders(app):
7272
app.set_translator('readthedocs', DPYHTML5Translator, override=True)
7373
app.add_builder(new_builder, override=True)
7474

75-
def setup(app):
75+
def setup(app) -> None:
7676
add_builders(app)
7777
app.connect('builder-inited', add_custom_jinja2)

setup.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88
version = ''
99
with open('discord/__init__.py') as f:
10-
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE).group(1)
10+
11+
search = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)
12+
13+
if search is not None:
14+
version = search.group(1)
15+
16+
else:
17+
raise RuntimeError("Could not grab version string")
1118

1219
if not version:
1320
raise RuntimeError('version is not set')

0 commit comments

Comments
 (0)