-
Notifications
You must be signed in to change notification settings - Fork 769
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
[Feature Request] add support for type annotation hints in #[text_signature]
macro.
#1112
Comments
I'm not sure that |
Is there any update on this issue? |
Afraid not; proposals for design and implementation are always welcomed. |
Hello @davidhewitt, would it be possible to plan for a call to discuss what has already been explored and what is yet to be attempted? |
Type annotations in pyo3 would sure be very helpful. But is there some hack around it for now? I am creating a python module in rust and I really do need editor auto-completion support. |
@CaffeineDuck What I've done in the past is unpack the essentially: ./.venv/bin/wheel unpack foo-0.1.0.whl
touch foo/py.typed
cp -R ./foo foo-0.1.0
./.venv/bin/wheel pack foo-0.1.0 where there are some |
@sbdchd Dats big brain, thanks. |
Note that if you're using |
@LucaCappelletti94 if you want you can reach out to me on gitter and we can try to arrange something. TBH the short answer is that I'd really love to have type annotations supported better in PyO3, however I haven't thought about them much myself and there's other things I'm working on at the moment. Also I prefer to share information on GitHub issues rather than calls because then it's available for everyone to read it! |
@davidhewitt yeah I absolutely concur that the information must be shared on GitHub issues, we could just keep a small record of the important points, if any, discussed during the proposed call. The call would be only a possibly shorter attempt to sync up and avoid trying stuff that you have already attempted. I'll reach to you on Gitter! |
Logging here what was briefly discussed on Gitter that I believe could be of help to other users:
|
For multiple reasons, using .pyi files is inconvenient for me at the moment ( Until it's possible for PyO3 to generate the type annotations automatically, would it be possible to have something similar to /// Documentation here
#[pyo3(text_signature = "(a, /)", type = "(int) -> Optional[int]")]
pub fn foo(a: u64) -> Option<u64> {
todo!()
} which would generate: def foo(a):
# type: (int) -> Optional[int]
"""Documentation here"""
pass This syntax (https://peps.python.org/pep-0484/) is supported by MyPy and most IDEs. PyO3 doesn't even need to check that it's correct. It's compatible with all versions of Python. Would this be doable? I can submit a PR if someone guides me through what needs to be changed. |
The problem isn't the PyO3 syntax but rather how to transfer this information from the Rust source through to Python. The only space for metadata that the Python C-API gives us to communicate information is the I've been considering reaching out to upstream CPython to request the |
Ah, I see. Would it be possible to integrate such a tool with PyO3 so it can take advantage of the information PyO3 already possesses? (e.g. as an optional cargo feature). I guess if it's integrated well enough with PyO3 and Maturin it can work just as well as if the information was included in the objects directly. The only downside I see is that from my experimentation, |
I don't think it would integrate with |
The .pyi file generation itself should be quite simple (all the information is already available to PyO3 if we add the |
In theory you might be able to use |
I attempted to use the macros to generate the info, but they don't have access to type information so it's a bit limited. I'm currently looking at |
👍 that might save us having to do the parsing too, good idea. If you find you need to add extra sentences to the docs for |
I'll update you on what I manage to do. I'll need you so Maturin can start that tool anyway :) |
I forgot to link it here, and I see that many people follow this issue. I'm prototyping a way to generate python stubs (pyi), hopefully without needing any annotations. Please take a look and share your feedback here: #2379 |
Hi, is there any updates? Thanks |
In PyO3 0.18 we will autogenerate |
xref: python/cpython#101872 |
Unfortunately annotations in https://discuss.python.org/t/type-signatures-for-extension-modules-pep-draft/43914 |
Hi there, would you consider adding some note about type annotations and workarounds into the docs? I was surprised to find that my compiled statically-typed pyo3 library didn't come with type annotations and even more surprised to find that after searching around the only discussion I could find about it was this closed issue... |
@dbarnett There is a section about it in the doc: https://pyo3.rs/v0.22.2/python-typing-hints.html Sadly CPython does not allow modules written in C to add annotations so there is nothing we can do to add them directly. However, there is some work to auto-generate type hints: #510 #2454 |
Would it be possible to extend the
text_signature
macro to be able to support python type annotation.Example
Expected documentation:
->
type
int
instead of rust'si64
for example)int
for the annotation ofa
andb
even if Rust expect anusize
since python doesn't have unsized integersstr
and notPyResult
Note
After reading #310 (comment) I think that it's not possible to use the same machinery than type annotation in python code (which, from what I understand, would allow IDE to give better auto-completion hints). While waiting for the cpython bug to be fixed, it should still be possible that the text returned by
help(sum_as_string)
contains the type annotation even if__annotations__
isn't populated correctly (yet).The text was updated successfully, but these errors were encountered: