-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Operator methods include a type signature of the additional overload #2277
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,6 +257,7 @@ class cpp_function : public function { | |
/* Generate a proper function signature */ | ||
std::string signature; | ||
size_t type_index = 0, arg_index = 0; | ||
std::string self_type("object"); | ||
for (auto *pc = text; *pc != '\0'; ++pc) { | ||
const auto c = *pc; | ||
|
||
|
@@ -293,9 +294,13 @@ class cpp_function : public function { | |
pybind11_fail("Internal error while parsing type signature (1)"); | ||
if (auto tinfo = detail::get_type_info(*t)) { | ||
handle th((PyObject *) tinfo->type); | ||
signature += | ||
std::string tname = | ||
th.attr("__module__").cast<std::string>() + "." + | ||
th.attr("__qualname__").cast<std::string>(); // Python 3.3+, but we backport it to earlier versions | ||
signature += tname; | ||
if (arg_index == 0 && rec->is_method) { | ||
self_type = tname; | ||
} | ||
} else if (rec->is_new_style_constructor && arg_index == 0) { | ||
// A new-style `__init__` takes `self` as `value_and_holder`. | ||
// Rewrite it to the proper class type. | ||
|
@@ -306,6 +311,9 @@ class cpp_function : public function { | |
std::string tname(t->name()); | ||
detail::clean_type_id(tname); | ||
signature += tname; | ||
if (arg_index == 0 && rec->is_method) { | ||
self_type = tname; | ||
} | ||
} | ||
} else { | ||
signature += c; | ||
|
@@ -404,9 +412,10 @@ class cpp_function : public function { | |
|
||
std::string signatures; | ||
int index = 0; | ||
const bool has_overloads = (chain || rec->is_operator); | ||
/* Create a nice pydoc rec including all signatures and | ||
docstrings of the functions in the overload chain */ | ||
if (chain && options::show_function_signatures()) { | ||
if (has_overloads && options::show_function_signatures()) { | ||
// First a generic signature | ||
signatures += rec->name; | ||
signatures += "(*args, **kwargs)\n"; | ||
|
@@ -417,7 +426,7 @@ class cpp_function : public function { | |
for (auto it = chain_start; it != nullptr; it = it->next) { | ||
if (options::show_function_signatures()) { | ||
if (index > 0) signatures += "\n"; | ||
if (chain) | ||
if (has_overloads) | ||
signatures += std::to_string(++index) + ". "; | ||
signatures += rec->name; | ||
signatures += it->signature; | ||
|
@@ -435,6 +444,16 @@ class cpp_function : public function { | |
if (options::show_function_signatures()) signatures += "\n"; | ||
} | ||
} | ||
if (rec->is_operator) { | ||
signatures += "\n"; | ||
signatures += std::to_string(++index) + ". "; | ||
signatures += rec->name; | ||
signatures += "("; | ||
if (rec->is_method) { | ||
signatures += "self: " + self_type + ", "; | ||
} | ||
signatures += "*args, **kwargs) -> NotImplemented\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scrutinizing this again: Also, is this the way it's done? Can you give me a reference of type annotations in pure Python code, perhaps? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I had incorrectly assumed that being a singleton, that |
||
} | ||
|
||
/* Install docstring */ | ||
auto *func = (PyCFunctionObject *) m_ptr; | ||
|
This comment was marked as outdated.
Sorry, something went wrong.