-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add typings #1497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typings #1497
Conversation
I understand the usage and reasons for However, I do not think they apply to this right? Shouldn't the goal be to apply these typings to the main functions inside the library instead of using stubs? |
There are a few advantages to using
As I said, I initially started out typing the entire library (including the function bodies), but after discussing with you in PM on Discord you mentioned that you didn't feel strongly about having the library code typed. I thought this was a good compromise. |
The bugfixes should probably be split up into their own commit (or maybe pull request). They don't really have anything to do with adding typings to the lib. |
Good call. I'll separate them out. |
I've separated the fixes out into their own PRs and removed them from this one as well as rebasing on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BotBase type hints could use the Dict[str, Any]
for cogs, extra_events and extensions.
Also, any call to discord.ext.commands going through such syntax complains about ext not being in the __init__.pyi
.
Added
As long as you |
If you do I never implied it is a proper way to use it, but it exists, and typings should cover for it too, instead of pulling up a warn. |
I just tried the following: import discord
from discord.ext import commands
Bot = discord.ext.commands.Bot
reveal_type(Bot) I don't get a warning from |
7466693
to
1e56826
Compare
It's been a while, as I still am in middle of transitioning my project to Type Hints, but I caught that |
Since |
Well, given that any object typed as GuildChannel - i.e list returned by |
Or, perhaps the other way around, possibly the This would merely be escaping from the single-case issue here. It raises a question of whether anything should be typed with an abstract, and not with the actual functional classes. |
Just a note about the fact recent library update might require this getting a lil update. |
97d81e8
to
f6936de
Compare
Is this going to be merged or should someone change it to be in code (like using Sphinx doc strings allowing it to be explicit on the docs too)? I've wanted typing for the last year so would love a solution |
I myself have been using this on my own for a while, and I have to say it has been of great assist, but given that using the type hints in python, at least now, afaik isn't really _too mainstream_, and can be confusing as to the usecase. I'd nevertheless welcome the addition of this at least as an optional extension.
|
I think most of the attributes missing types have a doc string anyway, so although it seems a bit long, the be might be using |
Any news on when this will be added to discord.py? Typing is very useful. |
I don't have a news on that, but now there's a discord.py-stubs package from the author of this PR, which you could use if you need d.py to be typed. |
After a few months of inactivity here, I've decided that it will be easier for me to maintain the stubs in their own package on pypi named discord.py-stubs. This does add some complexity in using discord.py with mypy, namely using generics from the stubs when they aren't generic at runtime. For instance, to create a from typing import TYPE_CHECKING
from discord.ext import commands
if TYPE_CHECKING:
Cog = commands.Cog[commands.Context]
else:
Cog = commands.Cog
class MyCog(Cog):
... To alleviate this issue, I have also created discord-ext-typed-commands which mirrors the from discord.ext import typed_commands
class MyCog(typed_commands.Cog[commands.Context]):
... Thanks to @Rapptz for the help here as well as everyone who reviewed this PR (especially @NCPlayz and @Werseter )! |
I've added
pyi
files as siblings to all of the library files:typing.Generic
in order to be subscriptable and not throw errors when using generic subscripts in python code.mypy_extensions
andtyping_extensions
forTypedDict
andProtocol
respectively. These will not be pulled in unless specified by the consumer.