-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Added support for reading .pyi files. #4824
Conversation
What is .pyi file? Is it standardly used? |
You guys have an open issue that references .pyi files in #2755. |
Thank you for description. I understand. I know stub files for mypy (typeshed). But I've never know PyCharm using it as external type hinting. +0 for this for following reasons:
I think this is temporarily needed feature. So it would be nice if we can implement this as an extension. I'll think about it later. |
Thanks for the consideration. I was leaning towards an extension myself. I'll have to get back to it when I have some time to figure how best to implement it. Also just one more use case for this (though it is pretty narrow) is an object whose methods defined until run time. As I said this is why I needed to experiment with this. Basically, the methods are decided using the strategy pattern when the object is instantiated. The stub files allowed me to define the signature of the methods and add docstrings for the default strategy. |
I feel your case is not usual. So I can't determine to add this feature to sphinx-core. As a workaround, you can override
I know this is very hacky. But I believe this will work without changing sphinx-core. |
@shimizukawa any comments? |
|
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.
We need a general case of reading the pyi file in the same directory and doing autodoc.
@shimizukawa Thank you for comment. @alliefitter I markded this as "wontfix". I think there are no proper way. But we can hack it with workaround. So please use it for your extension. |
I just stumbled across this issue by googling and have exactly the same problem as OP. I also created some methods dynamically and documented them using .pyi files. I was very much assuming that Sphinx would use them to generate documentation, as many typing-related things are also supported.
For dynamically generated types, functions and methods, you really don't have any other way of annotating them properly. |
Could you let me know about "many typing-related things"? I'd like to know this is commonly used way or not. Is there any specification?
Read my comment above please: #4824 (comment) |
I apologize for not responding sooner. I get a flood of Github notifications from work everyday and missed these notifications. @JosXa glad that some else has a need for this besides me. @tk0miya I agree that this doesn't belong in With @tk0miya 's workaround, I think I can get this done over the weekend. Thanks, guys! |
Oh, and @JosXa, just a heads up. If you're using the Google Docstring extension, you have to edit that too in order for it work properly. |
Sorry, I'm not very experienced with Sphinx as this is the first project I'm making public docs for, so I'm not exactly sure. But all the normal argument type hinting with @tk0miya Ya so I got a custom extension rolling with your guideline, thanks for that. However, it has issues with the non-standard syntax of .pyi stubs, for instance class attributes defined as
instead of
I suspect that there might be more instances where the syntax is not quite parseable by a normal python interpreter. Do you not type hint your attributes and properties the same way, @alliefitter? Or have you found a solution to that? I guess it should be fairly simple to script just this one part, but it makes me think that the support for this should come from a more holistic solution than just a workaround/hack.
I think the idea stems from the mypi project in order to be able to write and share type hints for third-party libraries that you do not have access to or in order to separate type hints from the source code (PYTHONPATH or |
Interesting docs:
Note: |
@JosXa are you using the autodoc typehints extension? I haven't had any issues with the autodoc reading my type annotations. |
Trying it now, and thanks for the link - I included it. Update: The issue remains:
and
@alliefitter this is what I'm trying to do: https://github.com/JosXa/tgintegration/blob/freeze/tgintegration/botintegrationclient.pyi#L15 Do you do it differently? I'm pretty sure it's valid syntax, so something is wrong. Perhaps a too old Python version (edit: no I thinnk 3.5 should be good, right?) or actually an issue with the Sphinx parsing... |
Yes, it should be fine with 3.5+. I just checked and I'm able to type attributes using that syntax. It could be that you aren't initializing them? Maybe try giving them a default value. |
Thanks! This is what I'd wanted.
It means this is not PyCharm specific behavior. +1 to add this to sphinx-core. @shimizukawa what do you think? |
Found an alternative: https://github.com/readthedocs/sphinx-autoapi |
It'd be great if we have a built-in support :-) |
Any progress here please? this feature is really useful for packages that are heavily written in Cython |
I'd like to add to the chorus. It would help add type info for Pybind11 projects. |
If I were to provide a new PR that addresses the issues raised on this PR (and updates it). Would it be something y'all devs would willing to merge? (@tk0miya) |
@arthurp please can you provide a high level (bullets fine) overview of your updated proposal? I wouldn't want you to put in work needlessly. Personally I'd want to see a switch for A |
Has there been any update on the inclusion of an option to pull types from stub files? |
Not yet but I plan to work on that next year. I can't promise anything yet but this is indeed an important thing to support imo. |
I'll add that we've run into the same issue for many of our projects, too, where the
This comes from We treat warnings as errors, so, we have to have these files excluded for now. |
For @tk0miya's workaround, I wasn't able to get it to work. I tried a few things for the
It seems that |
Closing in favour of #13253. A |
Subject: Added support for reading .pyi files
Feature or Bugfix
Purpose
I created a class that dynamically builds its methods at runtime and wanted to create some docs for it.
So, I made a .pyi file with the dynamic methods defined and with docstrings. PyCharm was able to read this file and provide code completion and documentation, but sphinx required a little changing. The change in this commit is more of a proof of concept than anything. I REALLY don't know this codebase or how best to incorporate this--as a change to sphinx or as an extension. I'd really appreciate any thoughts or guidance on what I can do to expose this functionality.
Detail
os.path.isfile('{}i'.format(module.__file__))
.importlib
.