-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add type stub generation to cython #3818
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
base: master
Are you sure you want to change the base?
Conversation
Ah, cool, thanks for volunteering to work on this. |
I was considering to run it right after type analysis, but I now think that it would really be best to run it at the very end. Multiple code checks are executed shortly before the C code generation phase, and I don't know if users would expect a stub file to be generated (or overwritten) if the translation run fails in the end. We might even consider doing it after the C code generation, because that is the point where we know that everything really went smoothly.
Those are added somewhat early in the pipeline, so you wouldn't find a place to run the
Not sure if it's really the parser. Probably some later transform, such as
Oh, yes. Tricky one. You might start by adding only assignments to the visitor and tracking names imported from the
Sounds more like an option to me, but should be handled in the same way as
Sure. You can base that on the |
I ended up having to solve a very similar problem for my dataclasses PR. My approach was |
Also: added support for async functions and limited decorator support
Thank you for the feedback and support. The first draft was more of a prototype to convey the ideas behind the stub generator. I am glad you agree with the based architecture, so I will just go on until I have a working prototype 😄 |
Well, most of this should still reuse the existing |
Well, there is definitely some overlap between writing @da-woods: Thank you for the suggestions. Regarding tests, I think I will try to rig something up using round trips via the python |
"__releasebuffer__", | ||
"__getreadbuffer__", | ||
"__getwritebuffer__", | ||
"___getsegcount__", |
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.
___
– typo or intended?
Since the |
What's the status of this PR? Automatically generating typing stubs would be a very nice feature! |
I think the original author stopped working on it. Help is welcome. Look through my previous comments to see what needs to be changed. |
@scoder I like this change a lot and I think I could resume this branch and fix that typo if you want me to even though it's been almost two full years since the author abandoned the project so some other fixes that might be needed such as variable name changes and other things. My reasons on finishing this are simple. I'm a VS Code user, I found it very annoying to have to write large amounts of stub files for things. I think that this will be beneficial to projects such as PyImgui and Pyducktape2 which lack stub files. Having these tools available may help those projects out tremendously. But hey, this isn't my first rodeo making a large solution to a problem and it won't be my last either. |
I'd be happy to see this resumed. Several problems that existed at the time have been solved now, for features like the imported pure Python mode or dataclasses. The Cython code writer was also definitely improved since then. I haven't looked into this PR for years, so I can't judge the details right now, but it should be much easier to get something working these days.
I'm still very much against the code duplication that this PR added at the time. That is even more redundant now and needs to be cleaned up.
In general, we should try to output what Cython itself uses/accepts as annotations and decorators. See Shadow.py and the pure Python docs on this. We have a stub file for that, so analysers should understand it.
|
I agree with that as well. having conversions from this example I wrote below is what I've always wanted. # myexample.pyx
cpdef int add(int a, int b):
return a + b # myexample.pyi
def add(a: int , b: int ) -> int:.. I also just took a look at My only question is this, should I try and make a new fork off of the old branch or make an entirely new fork and bring in some of the things this author worked on as a template? |
Compilation of PYX (cython) -> PYI (stub) file converters.
I should have done a more complete search earlier. I would have considered using the cython compiler as the parser. |
On Sat, Aug 5, 2023 at 9:47 PM Raub Camaioni ***@***.***> wrote:
Compilation of PYX (cython) -> PYI (stub) file converters.
- CyStub <https://github.com/Vizonex/CyStub>: uses cython compiler to
generate PYX -> PYI
- cythonbuilder <https://github.com/mike-huls/cythonbuilder>: custom
parser PYX -> PYI
- CythonPeg <https://github.com/RaubCamaioni/CythonPEG>: pyparsing
custom definitions PYX -> PYI
- stubgen <https://mypy.readthedocs.io/en/stable/stubgen.html>: uses
python runtime objects to generate PYI files
I should have done a more complete search earlier. I would have considered
using the cython compiler as the parser.
I started a conversation in cython-users
<https://groups.google.com/g/cython-users> mailing list to compile
additional converter options (if it gets approved).
If you know about additional PYX -> PYI code/methods post it there.
—
Reply to this email directly, view it on GitHub
<#3818 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A3K7GGRC5IYA65YNYMH2IG3XT4AVNANCNFSM4Q65BJHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Sure thing I will do that as the maintainer of cystub my experimentation
with the nodes has died off since 3.0 since there is no easy way to get
them immediate but I would love to resume what I had going for it. I just
wanted to make sure the compilation was perfect such as type hinting the
return values or other things even if the user didn’t have it in place.
There are many ideas I had on improving this but it’s just been difficult
to extract all the information needed from them such as conversions from C
type to python object type and keeping initialized classes for later
annotations and what not.
|
Just from glancing at the linked pages, it seems that CyStub by @Vizonex is the most complete solution at the moment, right? However, it does not work anymore with Cython 3... Stubgen is a mostly stable solution, but is unable to infer most types from Cython code. (They just become As the author of this pull request stopped working on it, maybe it should be closed? |
On Sun, Sep 3, 2023 at 4:31 AM Simon-Martin Schröder < ***@***.***> wrote:
Just from glancing at the linked pages, it seems that CyStub by @Vizonex
<https://github.com/Vizonex> is the most complete solution at the moment,
right? However, it does not work anymore with Cython 3...
Stubgen is a mostly stable solution, but is unable to infer most types
from Cython code. (They just become Any which is not very helpful.)
As the author of this pull request stopped working on it, maybe it should
be closed?
—
Reply to this email directly, view it on GitHub
<#3818 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A3K7GGSDBBJXEQ6CGZPGF73XYRE6TANCNFSM4Q65BJHQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
I can resume this project but I need to find a clean solution to extract
the node-tree nobody tells me anything including big changes like this one.
But if someone could just provide me the steps I could resume what I
currently have.
|
@moi90 I think the only reason why I have been currently stuck on writing something good for my current .pyx to .pyi compiler and doing a pull request is because I'm trying to implement a feature similar to what PyLance is able to do where it has the ability to resolve return type annotations even when the programmer doesn't have anything put in. The tricky thing about that is that Pyrex Types like to hide certain variable types that are things such as built-in types which would commonly return with |
That sounds totally reasonable! :) (I'm sorry, I'm not of much help. I have very limited time and no prior knowledge on this issue. I just would like to use it ^^) |
Update: my |
Hi! I just started looking into this topic and want to add another item to the list of @RaubCamaioni. The vscode cython extension has an experimental feature to generate stub files: https://github.com/ktnrg45/vs-code-cython Unfortunatly I do not have the skills to check if it is based one one of the items listed. Maybe it is of help for this feature. |
This was brought up in #3150 by @scoder:
I have written a very simple prototype to generate stub files, based on a
TreeVisitor
. Based on this input:it generates the following stub file:
The following problems need to be resolved:
cpdef
correctly: I am using a combination of thepy_type_name
of thePyrexType
and thetype.name
of the node method so far. Unfortunately, for anint
, thepy_type_name
returns'(int, long)'
see here. What is more, anobject
type is probably a little useless...@property
__cinit__
and friends.Declarations such asglobal_typed: int
without a value are swallowed by the parser, this probably needs to be rectified.T = TypeVar("T")
).pyi
file accompanying the compiled extension library (rather than just printing it out)I will continue to work on the PR, tracking the changes. I would be happy about any feedback...