-
-
Notifications
You must be signed in to change notification settings - Fork 247
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 Python stubs #130
Comments
I export the functions that need to be publicly available and python-language servers provide autocompletion for them. Did you face an issue where the lsp was not recognizing anything? |
I guess my thoughts were more about the types, but I see that the only one that matters on this issue would be the "request" dictionary. 🤔 I didn't know it was a dict, but https://docs.python.org/3/library/typing.html#typing.TypedDict can be used to define the keys it holds, and the LSP will be able to autocomplete the keys as well, jfyk. |
Ah, I see. I am not a big fan of typed languages. So, I never really bothered to learn more about types in python. But I can definitely have a look if you folks use them. :D |
Supplying types would help immensely with verifying type-correctness with checkers like mypy, so I would really recommend bundling and maintaining pyi files with robyn. Type annotations in python has been getting a boost and wider recognition lately, so I don’t think it’s wrong to focus on this as well. If you pass user functions any object that’s only defined in native code, or expose functions defined the same, then it is common practice to either at least document it, and often to then provide type stubs as described here. |
@ShadowJonathan @Kludex , I started reading about Could you folks point me to a codebase that uses them, so that I could take some inspiration from it? |
I'm on my phone, but look for "blacksheep python" |
Hi @Kludex @ShadowJonathan , I have been thinking about adding some types to the public methods atleast. Do you folks have any strong opinions about |
|
Ah okay. And do you think that mypy has gradual adoption or is it very enforcing? |
You can progressively add support. See: |
Thank you @Kludex. I will have a look the PRs :D |
Feel free to ping me as reviewer. |
@sansyrox Pyright is more feature rich and much faster than mypy. Feature requests / bug fixes also get implemented considerably faster. The only feature that mypy has over pyright in my opinion is plugin support which is currently missing in pyright. With that said, mypy is currently more popular than pyright for people's choice of type checker although pyright is still fairly popular and can also be used as a language server with support for VSCode, Sublime Text, vim etc. From a library maintainer's perspective it is best for the public API to support both although I highly recommend pyright for any internal usage if you need to use a type checker.
Pyright also supports incremental adoption, you can explicitly list the files to check or exclude files from being type checked through the config file, see https://github.com/microsoft/pyright/blob/main/docs/configuration.md#main-pyright-config-options. Also you only need to add stub files for the Rust -> Python interface, all other standard python files you can add inline type annotations. Feel free to ping me for review or if you need help as well :) For reference, I maintain https://github.com/RobertCraigie/prisma-client-py which makes heavy use of type annotations. |
Hi @RobertCraigie , Firstly, thank you so much for the detailed explanation. :D I checked out
So, then just adding and including Python stubs in the repo should be enough. Right?
Actually, I was thinking of adding stub files for everything. As I am not a big fan of inline types.(I know. I am sorry :p) Would that bring any issue(s) ?
Thank you so much. This feature is the next thing that I plan to implement. So, hopefully soon 🤞 |
Thank you :D
Yes, by supporting both I mean only using features that both type checkers support. For example, pyright has support for
No worries everyone has their own personal preferences, I'm just glad you're willing to add type stubs for the people that do want to use types. I know of other libraries that are completely against adding types. So thank you :)
I'm not familiar with packaging type stubs as all of the projects I've worked on have used inline types. The only issues I can see arising from using type stubs is that different type checkers use different semantics to resolve stub files and I'm not aware of the nuances between them. Adding the |
@RobertCraigie , got it. Thank you! :D Also, If it's not that hard, I will try to make some integrations this week. |
Thank you :D It should be fairly easy to make a crud app example. Here's a minimal example using Prisma with Robyn. from robyn import Robyn
from prisma import Prisma
from prisma.models import User
app = Robyn(__file__)
prisma = Prisma(auto_register=True)
@app.startup_handler
async def startup_handler() -> None:
await prisma.connect()
@app.shutdown_handler
async def shutdown_handler() -> None:
if prisma.is_connected():
await prisma.disconnect()
@app.get("/")
async def h():
user = await User.prisma().create(
data={
"name": "Robert",
},
)
return user.json(indent=2)
app.start(port=5000) Using this Prisma Schema: datasource db {
provider = "sqlite"
url = "file:dev.db"
}
generator py {
provider = "prisma-client-py"
}
model User {
id String @id @default(cuid())
name String
} Something I also forgot to mention is that pyright has support for automatically creating stub files which can help you save a lot of time but you'll still have to edit the auto generated stubs. You can try it out by running |
@RobertCraigie , wow! Thank you for both of them. :D |
I'm very noob in the Rust->Python binding stuff, but I guess we need
.pyi
files?The text was updated successfully, but these errors were encountered: