Introduce __text_signature__ property - #2904
Conversation
__text_signature__ property
|
This pull request seems to resolve #2680 in some part 👀 |
| format!("<built-in function {}>", self.value.name) | ||
| } | ||
| #[pyproperty(magic)] | ||
| fn text_signature(&self, vm: &VirtualMachine) -> PyObjectRef { |
There was a problem hiding this comment.
| fn text_signature(&self, vm: &VirtualMachine) -> PyObjectRef { | |
| fn text_signature(&self, vm: &VirtualMachine) -> Option<String> { |
With this signature, None will turn into Python None and Some(String) will turn into Python str
There was a problem hiding this comment.
I tried to apply it in d12d286 commit. Could you check it again?
|
This is nice! It does introduce an issue though, |
2f06d9b to
d12d286
Compare
Yeah, I missed it to comment. 😅 I made it as an issue #2907. |
youknowone
left a comment
There was a problem hiding this comment.
nice work, I left a few comments about code style.
| fn find_signature(name: &str, doc: &str) -> Option<String> { | ||
| let dot_index = name.rfind('.'); | ||
| let name = match dot_index { | ||
| Some(index) => name[index + 1..].to_owned(), | ||
| _ => name.to_owned(), | ||
| }; | ||
|
|
||
| if !doc.starts_with(&name) { | ||
| return None; | ||
| } | ||
|
|
||
| let doc = doc[name.len()..].to_owned(); | ||
| if !doc.starts_with('(') { | ||
| None | ||
| } else { | ||
| Some(doc) | ||
| } | ||
| } |
There was a problem hiding this comment.
I feel like find_signature should return the first index of string of signature, but this function returns docs starting with signature paranthesis.
My suggestions:
- if this function is just once used by
get_text_signature_from_internal_doc, it doesn't look like to be an independent function. - If this function is planned to be used from somewhere, giving it a better name or comments will be a good idea. For now, I don't have a good name suggestion.
by the way, here is my suggestion for the code itself.
| fn find_signature(name: &str, doc: &str) -> Option<String> { | |
| let dot_index = name.rfind('.'); | |
| let name = match dot_index { | |
| Some(index) => name[index + 1..].to_owned(), | |
| _ => name.to_owned(), | |
| }; | |
| if !doc.starts_with(&name) { | |
| return None; | |
| } | |
| let doc = doc[name.len()..].to_owned(); | |
| if !doc.starts_with('(') { | |
| None | |
| } else { | |
| Some(doc) | |
| } | |
| } | |
| fn find_signature<'a>(name: &str, doc: &'a str) -> Option<&'a str> { | |
| let name = name.rsplit('.').next().unwrap(); // rsplit always have first item | |
| let doc = doc.strip_prefix(name)?; | |
| if !doc.starts_with('(') { | |
| None | |
| } else { | |
| Some(doc) | |
| } | |
| } |
There was a problem hiding this comment.
I feel like
find_signatureshould return the first index of string of signature, but this function returns docs starting with signature paranthesis.My suggestions:
1. if this function is just once used by `get_text_signature_from_internal_doc`, it doesn't look like to be an independent function. 2. If this function is planned to be used from somewhere, giving it a better name or comments will be a good idea. For now, I don't have a good name suggestion.by the way, here is my suggestion for the code itself.
Thanks for suggestions. I looked CPython implementation again and there is the usage of find_signature and skip_signature at _PyType_DocWithoutSignature (see also #2907). Current skip_signature implementation is incorrect and cannot be reused in _PyType_DocWithoutSignature. So I will implement the functions to return index not str like you said your feel 🙏🏻 .
static const char *
_PyType_DocWithoutSignature(const char *name, const char *internal_doc)
{
const char *doc = find_signature(name, internal_doc);
if (doc) {
doc = skip_signature(doc);
if (doc)
return doc;
}
return internal_doc;
}There was a problem hiding this comment.
Oh, I didn't know the name was CPython referenced, I am sorry to confuse you. The CPython code looks like intending Option<&str>. So keep going with it.
I got the feeling from the convention of Rust library, but CPython has its own convention. So never mind about it.
There was a problem hiding this comment.
As a note, I fixed skip_signature implementation but it is not used in this pull request so I removed it. And I implemented get_signature renamed from skip_signature.
| fn skip_signature(doc: String) -> Option<String> { | ||
| doc.find(SIGNATURE_END_MARKER) | ||
| .map(|index| doc[..index + 1].to_owned()) | ||
| } |
There was a problem hiding this comment.
| fn skip_signature(doc: String) -> Option<String> { | |
| doc.find(SIGNATURE_END_MARKER) | |
| .map(|index| doc[..index + 1].to_owned()) | |
| } | |
| fn skip_signature(doc: &str) -> Option<&str> { | |
| doc.find(SIGNATURE_END_MARKER) | |
| .map(|index| &doc[..index + 1]) | |
| } |
this functions doesn't need to copy doc
| let doc_string: Option<PyStrRef> = doc_string.and_then(|o| o.downcast().ok()); | ||
| doc_string.and_then(|doc| { | ||
| get_text_signature_from_internal_doc(self.name().as_str(), doc.as_str()) |
There was a problem hiding this comment.
| let doc_string: Option<PyStrRef> = doc_string.and_then(|o| o.downcast().ok()); | |
| doc_string.and_then(|doc| { | |
| get_text_signature_from_internal_doc(self.name().as_str(), doc.as_str()) | |
| let doc: PyStrRef = doc_string.and_then(|o| o.downcast().ok())?; | |
| get_text_signature_from_internal_doc(self.name().as_str(), doc.as_str()) |
|
|
||
| #[pyproperty(magic)] | ||
| fn text_signature(&self) -> Option<String> { | ||
| let doc_string = self.get_attr("__doc__"); |
There was a problem hiding this comment.
If you are interested in, I think tp_doc which should be added in PyTypeSlots is related to improvement of this line. It can be a topic for next patch.
There was a problem hiding this comment.
I tried to apply your suggestion in 78ff936. Could you check that it was you said?
Co-Authored-By: Jeong YunWon <youknowone@users.noreply.github.com>
34d5215 to
1931b9f
Compare
1931b9f to
78ff936
Compare
|
it looks great, thank you for contributing! |
This pull request tries to resolve #2410
This pull request does:
__text_signature__property toPyType,PyBuiltinMethod,PyBuiltinFunction. 40ab1c4Code to verify compatibility
References
__text_signature__logic: https://github.com/python/cpython/blob/fa919fdf2583bdfead1df00e842f24f30b2a34bf/Objects/typeobject.c#L183-L203