typed __main__ and acitivity
This commit is contained in:
parent
ad038d8979
commit
aacfe10e21
@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE.
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import discord
|
||||
import pkg_resources
|
||||
@ -32,14 +33,14 @@ import aiohttp
|
||||
import platform
|
||||
|
||||
|
||||
def show_version():
|
||||
entries = []
|
||||
def show_version() -> None:
|
||||
entries: List[str] = []
|
||||
|
||||
entries.append("- Python v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}".format(sys.version_info))
|
||||
version_info = discord.version_info
|
||||
version_info: discord.version_info = discord.version_info
|
||||
entries.append("- discord.py v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}".format(version_info))
|
||||
if version_info.releaselevel != "final":
|
||||
pkg = pkg_resources.get_distribution("discord.py")
|
||||
pkg: pkg_resources.Distribution = pkg_resources.get_distribution("discord.py")
|
||||
if pkg:
|
||||
entries.append(f" - discord.py pkg_resources: v{pkg.version}")
|
||||
|
||||
@ -49,12 +50,12 @@ def show_version():
|
||||
print("\n".join(entries))
|
||||
|
||||
|
||||
def core(parser, args):
|
||||
def core(parser, args) -> None:
|
||||
if args.version:
|
||||
show_version()
|
||||
|
||||
|
||||
_bot_template = """#!/usr/bin/env python3
|
||||
_bot_template: str = """#!/usr/bin/env python3
|
||||
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
@ -80,7 +81,7 @@ bot = Bot()
|
||||
bot.run(config.token)
|
||||
"""
|
||||
|
||||
_gitignore_template = """# Byte-compiled / optimized / DLL files
|
||||
_gitignore_template: str = """# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
@ -123,7 +124,7 @@ def setup(bot):
|
||||
bot.add_cog({name}(bot))
|
||||
'''
|
||||
|
||||
_cog_extras = """
|
||||
_cog_extras: str = """
|
||||
def cog_unload(self):
|
||||
# clean up logic goes here
|
||||
pass
|
||||
@ -158,7 +159,7 @@ _cog_extras = """
|
||||
# certain file names and directory names are forbidden
|
||||
# see: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
|
||||
# although some of this doesn't apply to Linux, we might as well be consistent
|
||||
_base_table = {
|
||||
_base_table: Dict[str, str] = {
|
||||
"<": "-",
|
||||
">": "-",
|
||||
":": "-",
|
||||
@ -176,7 +177,7 @@ _base_table.update((chr(i), None) for i in range(32))
|
||||
_translation_table = str.maketrans(_base_table)
|
||||
|
||||
|
||||
def to_path(parser, name, *, replace_spaces=False):
|
||||
def to_path(parser: argparse.ArgumentParser, name: str, *, replace_spaces: bool =False) -> Path:
|
||||
if isinstance(name, Path):
|
||||
return name
|
||||
|
||||
@ -208,13 +209,13 @@ def to_path(parser, name, *, replace_spaces=False):
|
||||
if len(name) <= 4 and name.upper() in forbidden:
|
||||
parser.error("invalid directory name given, use a different one")
|
||||
|
||||
name = name.translate(_translation_table)
|
||||
name: str = name.translate(_translation_table)
|
||||
if replace_spaces:
|
||||
name = name.replace(" ", "-")
|
||||
return Path(name)
|
||||
|
||||
|
||||
def newbot(parser, args):
|
||||
def newbot(parser: argparse.ArgumentParser, 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
|
||||
@ -241,7 +242,7 @@ def newbot(parser, args):
|
||||
|
||||
try:
|
||||
with open(str(new_directory / "bot.py"), "w", encoding="utf-8") as fp:
|
||||
base = "Bot" if not args.sharded else "AutoShardedBot"
|
||||
base: str = "Bot" if not args.sharded else "AutoShardedBot"
|
||||
fp.write(_bot_template.format(base=base, prefix=args.prefix))
|
||||
except OSError as exc:
|
||||
parser.error(f"could not create bot file ({exc})")
|
||||
@ -256,7 +257,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)
|
||||
@ -290,7 +291,7 @@ def newcog(parser, args):
|
||||
print("successfully made cog at", directory)
|
||||
|
||||
|
||||
def add_newbot_args(subparser):
|
||||
def add_newbot_args(subparser) -> None:
|
||||
parser = subparser.add_parser("newbot", help="creates a command bot project quickly")
|
||||
parser.set_defaults(func=newbot)
|
||||
|
||||
@ -301,7 +302,7 @@ def add_newbot_args(subparser):
|
||||
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) -> None:
|
||||
parser = subparser.add_parser("newcog", help="creates a new cog template quickly")
|
||||
parser.set_defaults(func=newcog)
|
||||
|
||||
@ -315,8 +316,8 @@ def add_newcog_args(subparser):
|
||||
parser.add_argument("--full", help="add all special methods as well", action="store_true")
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(prog="discord", description="Tools for helping with discord.py")
|
||||
def parse_args() -> None:
|
||||
parser: argparse.ArgumentParser = 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)
|
||||
|
||||
@ -326,7 +327,7 @@ def parse_args():
|
||||
return parser, parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
parser, args = parse_args()
|
||||
args.func(parser, args)
|
||||
|
||||
|
@ -121,7 +121,7 @@ class BaseActivity:
|
||||
|
||||
__slots__ = ("_created_at",)
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
self._created_at: Optional[float] = kwargs.pop("created_at", None)
|
||||
|
||||
@property
|
||||
@ -499,7 +499,7 @@ class Streaming(BaseActivity):
|
||||
return f"<Streaming name={self.name!r}>"
|
||||
|
||||
@property
|
||||
def twitch_name(self):
|
||||
def twitch_name(self) -> Optional[str]:
|
||||
"""Optional[:class:`str`]: If provided, the twitch name of the user streaming.
|
||||
|
||||
This corresponds to the ``large_image`` key of the :attr:`Streaming.assets`
|
||||
|
Loading…
x
Reference in New Issue
Block a user