-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Make pydoc consistent about bound methods #64909
Comments
inspect has a bug:
If I fix that, that exposes a bug in pydoc:
The only reason pydoc assumed the __func__ attribute was so it could use that instead of the bound function when getting the signature. I don't know why it cared, but: when it does so, that means it displays "self" for bound methods implemented in Python. However, since it doesn't do that for bound methods implemented in C, now the behavior is inconsistent. Should it display self or not? The consensus was, it should not. This would make pydoc consistent with inspect.signature. This patch therefore changes a third thing:
I propose to merge this for 3.4.0rc2. |
Larry, I think you can use undocumented and private (but still heavily tested) 'inspect._signature_internal(skip_bound_arg=False)', instead of signature. That will give you a Signature object with 'self' parameter always included. And in 3.5 we'll probably make this option (renamed, hopefully), public, and switch pydoc to use new API again. What do you think? |
(The thing with my proposal, is that pydoc will handle everything that 'inspect.signature' supports + it's behaviour won't change at all) |
Whoops, the test suite hadn't finished when I posted that, and it had an error. Here's an updated patch that fixes the tests. Also, there's one more minor fix in the patch. pydoc has cut-and-pasted code for HTML versus text rendering, and they had fallen out of sync. This patch brings them into sync. |
I don't think we should touch 'inspect.isbuiltin' at this stage. |
We discussed it in python-dev: And the overwhelming majority voted +1 for "don't show self for bound methods". So I'd like to change it for 3.4. (I meant to get it in earlier but lacked the time.) |
I'm not proposing to modify isbuiltin. Rather, I'm proposing to fix a bug in ismethod. |
But please hold on, I think I found a bug in inspect.signature. |
Indeed, here it is: bpo-20711 |
So why is it necessary (and a release blocker) to fix a bug in inspect._signature_fromstr, but a terrible idea to fix a bug in inspect.ismethod? |
I was actually writing a comment, when I received this on my email ;) Let me explain my point of view.
Sorry, I didn't know about it. I lost track of the relevant issue, and thought that we still need to keep the old behaviour in tact.
I'm +1 in principle, the only thing that bugs me, is that 'ismethod' is probably 100x more popular API then signature, and event getfullargspec. I'm just not sure what are the consequences. Maybe, it's possible to fix pydoc without touching 'ismethod'? My 2 cents. Now, about the newly discovered bug in getfullargspec() [and _signature_fromstr]. I put a "release blocker" on that issue because it's a new change, that 'getfullargspec' now uses 'inspect.signature', and I don't think we should ship this new feature broken. But certainly, it's up to you to let this in 3.4.0. |
Okay, that's a fair point. I checked, and the documentation specifically says that ismethod only returns true on bound methods implemented in Python. I think that's a bad API, it should be agnostic about the implementation language. But I'll remove the change to ismethod and rework the patch to pydoc around it. |
Agree. Re bpo-20711: Please take a look at the patch I wrote. It's your code I modified, you know it and the __text_signature__ quirks better than anyone. |
Here's a revised patch that doesn't modify inspect. |
A slight tweak to the patch. Previously I was just using truth testing on the value I got from "__self__", but that's wrong if the object is considered false (e.g. ''.zfill). (Yury got this right in bpo-20711, and I copied from him!) |
An even slighter tweak to the patch, just using De Morgan's law to make the code easier to read. |
Updated with tests. And I built without docstrings and it still passed. And if I revert the change to Lib/pydoc.py the tests fail. |
I went ahead and added tests for os.stat, and the unbound versions of the two class methods. Total of five new tests now. |
You're right, that was the exact same test ;-) So what I did was: move the other tests down into that test class, as that's a better fit, and keep the new version of the test. |
Looks good to me. Those unit tests give some confidence ;) |
New changeset 5e73bb72662e by Larry Hastings in branch 'default': |
With this patch 'help' now correctly renders classmethods, yay |
New changeset b2ee3fe195e2 by Larry Hastings in branch '3.4': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: