Type-Hint some files that were not type-hinted #101
@@ -23,23 +23,25 @@ DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import platform
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
import aiohttp
|
||||
import pkg_resources
|
||||
|
||||
import discord
|
||||
import pkg_resources
|
||||
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 +51,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 +82,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
|
||||
@@ -110,7 +112,7 @@ var/
|
||||
config.py
|
||||
"""
|
||||
|
||||
_cog_template = '''from discord.ext import commands
|
||||
_cog_template: str = '''from discord.ext import commands
|
||||
import discord
|
||||
|
||||
class {name}(commands.Cog{attrs}):
|
||||
@@ -123,7 +125,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 +160,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 +178,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 +210,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 +243,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 +258,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 +292,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 +303,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 +317,10 @@ 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 +330,7 @@ def parse_args():
|
||||
return parser, parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
parser, args = parse_args()
|
||||
args.func(parser, args)
|
||||
|
||||
|
||||
@@ -703,7 +703,14 @@ class GuildChannel:
|
||||
) -> None:
|
||||
...
|
||||
|
||||
async def set_permissions(self, target, *, overwrite=_undefined, reason=None, **permissions):
|
||||
async def set_permissions(
|
||||
self,
|
||||
target: Union[Member, User],
|
||||
*,
|
||||
overwrite: PermissionOverwrite = _undefined,
|
||||
reason: Optional[str] = None,
|
||||
**permissions: bool,
|
||||
):
|
||||
r"""|coro|
|
||||
|
||||
Sets the channel specific permission overwrites for a target in the
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -87,7 +87,7 @@ __all__ = ("Client",)
|
||||
Coro = TypeVar("Coro", bound=Callable[..., Coroutine[Any, Any, Any]])
|
||||
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
_log: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _cancel_tasks(loop: asyncio.AbstractEventLoop) -> None:
|
||||
|
||||
@@ -61,7 +61,7 @@ from .gateway import DiscordClientWebSocketResponse
|
||||
from . import __version__, utils
|
||||
from .utils import MISSING
|
||||
|
||||
_log = logging.getLogger(__name__)
|
||||
_log: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .file import File
|
||||
|
||||
@@ -170,7 +170,7 @@ class CachedSlotProperty(Generic[T, T_co]):
|
||||
|
||||
class classproperty(Generic[T_co]):
|
||||
def __init__(self, fget: Callable[[Any], T_co]) -> None:
|
||||
self.fget = fget
|
||||
self.fget: Callable[[Any], T_co] = fget
|
||||
|
||||
def __get__(self, instance: Optional[Any], owner: Type[Any]) -> T_co:
|
||||
return self.fget(owner)
|
||||
@@ -207,7 +207,7 @@ class SequenceProxy(Generic[T_co], collections.abc.Sequence):
|
||||
def __reversed__(self) -> Iterator[T_co]:
|
||||
return reversed(self.__proxied)
|
||||
|
||||
def index(self, value: Any, *args, **kwargs) -> int:
|
||||
def index(self, value: Any, *args: Any, **kwargs: Any) -> int:
|
||||
return self.__proxied.index(value, *args, **kwargs)
|
||||
|
||||
def count(self, value: Any) -> int:
|
||||
@@ -507,15 +507,15 @@ def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
|
||||
return float(reset_after)
|
||||
|
||||
|
||||
async def maybe_coroutine(f, *args, **kwargs):
|
||||
value = f(*args, **kwargs)
|
||||
async def maybe_coroutine(f: Callable[..., Any], *args: Any, **kwargs: Any) -> Callable[..., Any]:
|
||||
value: Any = f(*args, **kwargs)
|
||||
if _isawaitable(value):
|
||||
return await value
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
async def async_all(gen, *, check=_isawaitable):
|
||||
async def async_all(gen, *, check=_isawaitable) -> bool:
|
||||
for elem in gen:
|
||||
if check(elem):
|
||||
elem = await elem
|
||||
|
||||
Reference in New Issue
Block a user