Skip to content

Commit

Permalink
Merge pull request Pycord-Development#252 from ultrabear/master
Browse files Browse the repository at this point in the history
Add some typing (found with mypy --strict)
  • Loading branch information
BobDotCom authored Oct 22, 2021
2 parents 94c422a + 39d4219 commit b014cc0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
20 changes: 11 additions & 9 deletions discord/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import sys
from pathlib import Path

from typing import Tuple

import discord
import pkg_resources
import aiohttp
import platform

def show_version():
def show_version() -> None:
entries = []

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

def core(parser, args):
def core(parser, args) -> None:
if args.version:
show_version()

Expand Down Expand Up @@ -173,7 +175,7 @@ async def cog_after_invoke(self, ctx):

_translation_table = str.maketrans(_base_table)

def to_path(parser, name, *, replace_spaces=False):
def to_path(parser, name, *, replace_spaces=False) -> Path:
if isinstance(name, Path):
return name

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

def newbot(parser, args):
def newbot(parser, args) -> None:
new_directory = to_path(parser, args.directory) / to_path(parser, args.name)

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

print('successfully made bot at', new_directory)

def newcog(parser, args):
def newcog(parser, args) -> None:
cog_dir = to_path(parser, args.directory)
try:
cog_dir.mkdir(exist_ok=True)
Expand Down Expand Up @@ -262,7 +264,7 @@ def newcog(parser, args):
else:
print('successfully made cog at', directory)

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

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

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

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

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

def main():
def main() -> None:
parser, args = parse_args()
args.func(parser, args)

Expand Down
14 changes: 7 additions & 7 deletions docs/extensions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
from sphinx.writers.html5 import HTML5Translator

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

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

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

def depart_table(self, node):
def depart_table(self, node) -> None:
super().depart_table(node)
self.body.append('</div>')

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


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

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

def setup(app):
def setup(app) -> None:
add_builders(app)
app.connect('builder-inited', add_custom_jinja2)
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

version = ''
with open('discord/__init__.py') as f:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE).group(1)

search = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)

if search is not None:
version = search.group(1)

else:
raise RuntimeError("Could not grab version string")

if not version:
raise RuntimeError('version is not set')
Expand Down

0 comments on commit b014cc0

Please sign in to comment.