Skip to content
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

Undeprecate inspect.getfullargspec() #71359

Closed
ryanpetrello mannequin opened this issue Jun 1, 2016 · 37 comments
Closed

Undeprecate inspect.getfullargspec() #71359

ryanpetrello mannequin opened this issue Jun 1, 2016 · 37 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@ryanpetrello
Copy link
Mannequin

ryanpetrello mannequin commented Jun 1, 2016

BPO 27172
Nosy @brettcannon, @ncoghlan, @larryhastings, @ned-deily, @bitdancer, @ericsnowcurrently, @serhiy-storchaka, @1st1, @timgraham
Files
  • signature-from-callable-skip-bound-arg.patch
  • signature-from-callable-skip-bound-arg.patch
  • signature-from-callable-skip-bound-arg.patch
  • signature-from-callable-skip-bound-arg.patch
  • signature-from-callable-skip-bound-arg.patch
  • issue27172_undeprecate_getfullargspec.diff
  • issue27172_undeprecate_getfullargspec_v2.diff
  • 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:

    assignee = None
    closed_at = <Date 2019-05-11.04:32:59.790>
    created_at = <Date 2016-06-01.04:04:50.799>
    labels = ['type-bug', 'docs']
    title = 'Undeprecate inspect.getfullargspec()'
    updated_at = <Date 2019-05-11.04:32:59.789>
    user = 'https://bugs.python.org/ryanpetrello'

    bugs.python.org fields:

    activity = <Date 2019-05-11.04:32:59.789>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-11.04:32:59.790>
    closer = 'ncoghlan'
    components = ['Documentation']
    creation = <Date 2016-06-01.04:04:50.799>
    creator = 'ryan.petrello'
    dependencies = []
    files = ['43078', '43081', '43087', '43089', '43090', '45663', '45723']
    hgrepos = []
    issue_num = 27172
    keywords = ['patch']
    message_count = 37.0
    messages = ['266807', '266813', '266832', '266972', '268932', '269016', '269071', '269081', '269082', '269085', '269129', '269130', '270121', '270122', '281603', '281604', '281605', '281825', '281884', '282174', '282234', '282235', '282256', '282268', '292386', '292408', '292410', '303936', '303960', '303983', '304017', '304018', '307253', '307302', '307303', '307337', '307363']
    nosy_count = 12.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'larry', 'ned.deily', 'r.david.murray', 'SilentGhost', 'python-dev', 'eric.snow', 'serhiy.storchaka', 'yselivanov', 'Tim.Graham', 'ryan.petrello']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27172'
    versions = ['Python 3.6']

    @ryanpetrello ryanpetrello mannequin added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Jun 1, 2016
    @SilentGhost
    Copy link
    Mannequin

    SilentGhost mannequin commented Jun 1, 2016

    This could only go in 3.6, but it needs proper documentation and test(s).

    @ryanpetrello
    Copy link
    Mannequin Author

    ryanpetrello mannequin commented Jun 1, 2016

    Thanks, I'll upload a new patch shortly that addresses these.

    @1st1
    Copy link
    Member

    1st1 commented Jun 1, 2016

    Please don't commit this without my review.

    @SilentGhost SilentGhost mannequin assigned 1st1 Jun 1, 2016
    @1st1
    Copy link
    Member

    1st1 commented Jun 2, 2016

    Nick, what do you think about this one?

    I'm +1 to add this, but I'm not sure about the name for the argument. Do you have better ideas, or "skip_bound_arg" is good enough?

    @ryanpetrello
    Copy link
    Mannequin Author

    ryanpetrello mannequin commented Jun 20, 2016

    Yury/Nick,

    Any word on this? I know it's minor, but I'd love to see this make it into Python3.6.

    @ncoghlan
    Copy link
    Contributor

    Hi Ryan, sorry for the delayed response.

    My own concern with this proposal is that I don't understand the use case for it. We changed the inspect.signature behaviour away from that of inspect.getfullargspec because we considered the latter behaviour to be *wrong*: it reported a parameter the already bound method didn't actually accept when called. The "skip_bound_arg" functionality then remains *within* the inspect module for the sake of providing backwards compatible implementations of getargspec and getfullargspec without duplicating a lot of other callable introspection logic.

    If you can provide more information on the motivating use case, we can better determine if exposing this option directly is a suitable design response, or if there may be better alternatives available.

    @ryanpetrello
    Copy link
    Mannequin Author

    ryanpetrello mannequin commented Jun 22, 2016

    Nick,

    My use case is an issue of backwards compatibility and multiple Python version support for a library that makes prolific use of the legacy argspec (args, varargs, varkw, defaults) namedtuple, *including* the bound self argument behavior. argspec and signature are quite different, and supporting a Py26-Py36 codebase that handles both simultaneously seemed like quite a burden, so I opted to write a compatibility shim that returned an Argspec-like object for all versions of Python; this seemed the simplest approach to bridge the gap in a codebase that supports Python 2.6 through 3.6, and it's the approach that I've seen other major libraries (like Django) take:

    pecan/pecan#61

    I'm using a similar approach to Python 3.5's getfullargspec() implementation:

    https://github.com/python/cpython/blob/3.5/Lib/inspect.py#L1069

    ...but I don't have a long-term public API for doing it (so I have to rely on the private inspect._signature_from_callable call, which seems icky).

    @ncoghlan
    Copy link
    Contributor

    Are you able to use inspect.getfullargspec() on Python 3 rather than inspect.getargspec()?

    While both are technically deprecated, only inspect.getargspec() actually emits a deprecation warning - inspect.getfullargspec() still works without complaint and is in no danger of being removed.

    @ryanpetrello
    Copy link
    Mannequin Author

    ryanpetrello mannequin commented Jun 22, 2016

    Nick,

    My main reasoning for not using it is that it's marked as deprecated in the docstring, and I want to avoid relying on it if disappears in the future :)

    Warnings or not, the shim that I wrote doesn't use any deprecated code, so that's why I took that approach.

    @ncoghlan
    Copy link
    Contributor

    OK, so maybe the right answer here is to officially undeprecate inspect.getfullargspec(), as we definitely *don't* want people feeling obliged to write their own version of that, and potentially introducing inconsistencies between different tools.

    Then the deprecation warning on inspect.getargspec() can be updated to point people towards "inspect.signature() or inspect.getfullargspec()", while the docs for getfullargspec() itself can be updated to say it isn't recommended for use in new code, rather than that it's deprecated.

    @ryanpetrello
    Copy link
    Mannequin Author

    ryanpetrello mannequin commented Jun 23, 2016

    Nick,

    That seems reasonable to me :) I've updated my library to just use inspect.getfullargspec for Py3. Thanks for taking the time to walk through this with me!

    @ncoghlan
    Copy link
    Contributor

    OK, as per the above discussion, I've changed the title of this issue to be to undeprecate inspect.getfullargspec().

    That change involves two pieces:

    • change the documented deprecation of inspect.getfullargspec() to instead be a recommendation to avoid using it in new code, and instead use inspect.signature()
    • update the inspect.getargspec() documentation and programmatic deprecation warning to point to both inspect.signature() and inspect.getfullargspec() as potential replacements

    That gives folks already using inspect.getfullargspec assurance that it isn't going away anytime soon (if ever), while folks using inspect.getargspec get a lower impact migration path to a more Python 3 friendly version of the callable introspection API.

    @ncoghlan ncoghlan changed the title Add skip_bound_arg argument to inspect.Signature.from_callable() Undeprecate inspect.getfullargspec() Jun 23, 2016
    @bitdancer
    Copy link
    Member

    The existing patch no longer addresses the revised decision about the API (or the new title of the issue). So, we need a new patch.

    @bitdancer
    Copy link
    Member

    Also, this is now a documentation issue, though it does require a code change for the getargspec deprecation message.

    @bitdancer bitdancer added docs Documentation in the Doc dir and removed stdlib Python modules in the Lib dir labels Jul 10, 2016
    @bitdancer bitdancer added type-bug An unexpected behavior, bug, or error and removed type-feature A feature request or enhancement labels Jul 10, 2016
    @ncoghlan
    Copy link
    Contributor

    This just came up in a discussion on a urllib3 patch, so I'd like to fix it for 3.6.0rc1.

    Ned, given that the code change here is just deleting the deprecation warning from getfullargspec() and rewording the one for getargspec(), does that seem OK for 3.6.0?

    @ncoghlan ncoghlan self-assigned this Nov 24, 2016
    @ncoghlan
    Copy link
    Contributor

    When we undeprecate this, we should remove and reword the deprecation warnings in the next 3.5 maintenance release as well.

    I'll need to decide on a way to indicate in the docs that some versions of 3.x.y will report a deprecation warning for getfullargspec() though - probably a "Changed in" note.

    @ned-deily
    Copy link
    Member

    Nick, that seems like the right thing to do. Thanks for following up on it.

    @ncoghlan
    Copy link
    Contributor

    Initial patch attached. Key missing pieces:

    • needs a What's New note
    • needs to undeprecate inspect.getcallargs() as well (more on that below)

    As my last couple of comments indicate, I'd forgotten that only getargspec() was programmatically deprecated, which means the sole code change is a rewording of the getargspec() warning to mention both signature() and getfullargspec().

    The documentation changes are a bit more extensive, as I couldn't resist fixing the longstanding terminology error in the docs and docstrings, where these functions are mostly reporting *parameter* names, not argument names (callable arguments have values, not names).

    I think the "versionchanged" note works well for indicating that folks aren't imagining things if they remembered seeing this function marked as deprecated.

    However, I also went back and checked the 3.5 What's New, and that does mention the deprecations, so I'll need to do another version of this patch that includes a 3.6 What's New notice retracting those deprecations.

    That check also reveals some other documented deprecations that should probably be reverted.

    Firstly, getargvalues() and formatargvalues() are *frame* introspection functions, and hence have nothing whatsoever to do with inspect.signature(). The code and docs are just a bit confusing as they're interleaved with the callable introspection functions. I've filed that as a new issue: http://bugs.python.org/issue28814

    Secondly, https://bugs.python.org/issue20438#msg254892 notes that inspect.getcallargs() has some behaviours that differ from Signature.bind in a way that's a bit of a pain to replicate on top of the latter. Similar to getfullargspec(), what do folks think of the idea of reverting that deprecation at least until 2.7 goes EOL?

    (Noting for the record so folks don't wonder if it's an accidental oversight: I think formatargspec should retain its documented deprecation, as folks really are better off writing their own formatting function based on the data returned or switching to the new signature API)

    @1st1
    Copy link
    Member

    1st1 commented Nov 28, 2016

    Nick, your patch LGTM. I'd only add to the getargspec section that getfullargspec is usually a drop-in replacement (I've yet to see code where it's not the case).

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Dec 1, 2016

    Updated patch adding What's New and NEWS entries, and addressing Martin's review comments (mostly by accepting his suggestions).

    I ended up leaving inspect.getcallargs() deprecated, and instead added a comment to bpo-20438 noting how to achieve the bound args -> unbound args conversion in a general way: http://bugs.python.org/issue20438#msg282173

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 2, 2016

    New changeset 14c2d93ffcb3 by Nick Coghlan in branch '3.6':
    Issue bpo-27172: Undeprecate inspect.getfullargspec()
    https://hg.python.org/cpython/rev/14c2d93ffcb3

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Dec 2, 2016

    I've pushed the change for 3.6.0rc1 based on Yury and Martin's review, but wasn't able to forward merge it to default due to merge conflicts on Misc/NEWS that I wasn't sure how to resolve.

    @ned-deily
    Copy link
    Member

    It looks like Serhiy took care of the merge to default in dd4c420b8e66.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Dec 3, 2016

    Thank you Serhiy!

    That leaves this just as a pending update for 3.5.3 to bring it into line with 3.6.0.

    I've reverted the stage to "needs patch" and removed the patch keyword, as the 3.5 patch will be slightly different:

    • using 3.5.3 in the versionchanged notice
    • adjusting the 3.5 What's New to change the list of deprecated inspect module APIs

    @ncoghlan ncoghlan removed the 3.7 (EOL) end of life label Dec 3, 2016
    @timgraham
    Copy link
    Mannequin

    timgraham mannequin commented Apr 27, 2017

    Does it seems likely that getfullargspec() will be deprecated after Python 2 is EOL? Django is currently reimplementing getargspec():
    https://github.com/django/django/blob/8ab7ce8558792f41637d6f87f2a8a117e169dd18/django/utils/inspect.py#L4-L24

    A pull request proposes to modify that implementation to behave as getfullargspec(): django/django#8410

    Django master now supports Python 3.4+, so I guess we would rather use getfullargspec() if it's not going to be deprecated in the future. The only downside is that it would introduce deprecation warnings for Python 3.5 users since those warnings haven't been removed yet. I guess Django could silence them.

    @ncoghlan
    Copy link
    Contributor

    No, there are no plans to ever deprecate getfullargspec() again - it isn't hard to maintain indefinitely as a wrapper around inspect.Signature(), and it doesn't have the significant limitations that affected getargspec().

    @ncoghlan
    Copy link
    Contributor

    Also, note that the programmatic deprecation warning change in the patch is to the warning for getargspec(), so that it recommends getfullargspec() rather than Signature().

    There's no runtime deprecation warning for getfullargspec() in any version of Python 3.x, so projects can use it freely, regardless of target version.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Oct 9, 2017

    Oops, we/I missed the window for also getting the deprecation removed from 3.5.x (as that branch is now in security-fix only mode).

    Marking as resolved for 3.6 accordingly.

    @ncoghlan ncoghlan closed this as completed Oct 9, 2017
    @larryhastings
    Copy link
    Contributor

    Wait, what is all this nonsense?

    inspect.getfullargspec is Python 3 only. It was added to support keyword-only parameters. Python 2 doesn't *have* keyword-only parameters, so it isn't needed there.

    Check for yourself: Python 2 doesn't have inspect.getfullargspec.

    https://docs.python.org/2/library/inspect.html#inspect.getargspec
    

    We might consider un-deprecating inspect.getargspec() for supporting code supporting Py2 and Py3. But there's no point in un-deprecating inspect.getfullargspec() for that reason.

    Nick: please *back out* your pointless, taffy-headed checkin.

    @larryhastings larryhastings reopened this Oct 9, 2017
    @timgraham
    Copy link
    Mannequin

    timgraham mannequin commented Oct 9, 2017

    Perhaps the reason for the undeprecation could use some clarification. In a Python 3 only world (where Django's master branch is), I believe there's still usefulness for inspect.getfullargspec() -- see https://github.com/django/django/search?q=getfullargspec for usages.

    @ncoghlan
    Copy link
    Contributor

    Larry, your personal insult is entirely unwelcome, unnecessary, and unappreciated, especially as it's wrong regarding the Python 2/3 compatibility concerns.

    While getfullargspec() is indeed new in Python 3.x, it's also deliberately designed as a *drop-in replacement* for inspect.getargspec(). This means straddling Python 2 & 3 is straightforward, since you just need to toggle which API function you call (getargspec() on 2.7, getfullargspec() on 3.x).

    This is *not* the case for switching to the inspect.signature() API: doing that for existing code that still supports Python 2.7 also requires switching to one of the third party backports of that API to 2.7, and then changing the way your own code models function signatures.

    This is why getfullargspec() only received a documented deprecation, rather than a programmatic one. However, the combination of that notice in the documentation with the programmatic deprecation warning in getargspec() was enough to make people believe that in order to add Python 3 support, they *also* had to either switch to the inspect.signature() API, or else write and maintain their own version of getfullargspec().

    That wasn't the intended outcome, and we have no plans to actually remove getfullargspec(), so I changed the wording in the getargspec() deprecation warning and the getfullargspec() documentation to better encourage the desired behaviour (i.e. inspect.signature() being used in new code, and existing code being migrated to the inspect.signature() API as developers find value in doing so)

    @ncoghlan
    Copy link
    Contributor

    Note: the minor incompatibility that required getfullargspec() to be a separate API in the first place is the fact that it returns a 7 element named tuple instead of a 4 element one.

    This means that hybrid 2/3 code needs to consistently use name based item access rather than tuple unpacking, but other than that, all code related to the first four fields can be identical across versions. (Code related to handling the extra three fields will naturally only be needed on 3.x)

    @brettcannon
    Copy link
    Member

    Nick pointed me at this issue for the undeprecation of inspect.getfullargspec(). While I'm fine with the un-deprecation for compatibility reasons, I would rather the function not last beyond Python 3 un-deprecated. Nick says, though that:
    """
    ... the undeprecation
    isn't a Python 2/3 issue, it's a "tuples, lists and dicts are really
    handy representations of things, and Python developers often prefer
    them to more structured objects" issue.

    The modern inspect.getfullargspec implementation is a relatively thin
    wrapper around inspect.signature, and the only lossy part of the
    output transformation is that you can't tell the difference between
    positional-only and positional-or-keyword parameters.
    """

    My argument is TOOWDI and as of right now there's 3 for getting parameter information for functions (of which only one is currently deprecated). I would also argue that if people want a "signature to core data structure" translation then that can be covered by a package on PyPI since even now getfullargspec() is lossy and we don't need that kind of pragmatic support in the stdlib for this.

    @ncoghlan
    Copy link
    Contributor

    If there was a documented deprecation that said "Use <this PyPI package> instead", I'd be OK with that.

    The part I wasn't OK with is multiple projects each copying & pasting their own variant of the getfullargspec code and accessing private inspect module APIs in order to get the old behaviour back.

    So in order to move this to PyPI instead, we'd need to offer a completely public API that was equivalent to _signature_from_callable(func, follow_wrapper_chains=False, skip_bound_arg=False, sigcls=Signature)

    inspect.Signature.from_callable(func, follow_wrapped=False) comes very close, but misses a subtlety where "getfullargspec" will mention "self" for bound methods, even though it's implicitly supplied, and doesn't need to be supplied by the caller.

    That aspect could potentially just be deprecated outright though, with the PyPI replacement following inspect.signature's behaviour and reporting the actual call signature of the bound method.

    @ncoghlan
    Copy link
    Contributor

    I'll also note that one possible alternative would be to accept Ryan's original proposal, which was to make "skip_bound_arg=False" part of the public API for inspect.Signature.from_callable.

    Keeping getfullargspec around (but discouraged for new code), seems nicer to me than doing that, though, since the only use case we're aware of for that behaviour toggle is to be able to emulate inspect.getfullargspec() atop inspect.Signature().

    TOOWTDI simply doesn't strike me as a good enough reason to break working code in this case - there *is* an obvious way for new code (inspect.signature), and there are plenty of other standard library APIs that we keep around primarily for backwards compatibility reasons, even though we don't necessarily recommend using them any more.

    @brettcannon
    Copy link
    Member

    I'm not saying we remove getfullargspec() **right now**, just that we don't keep it around long-term as it simply can't evolve to keep up with how rich our parameter support is today and might become in the future. And that's why, for me, the deprecation was enough to signal, "this code is here for now and you can use it, but you really need to be aware of some issues that are leading to it being removed". Now obviously you prefer the doc approach for this, Nick, and that's fine since this is quickly devolving into "agree to disagree" as while "there are plenty of other standard library APIs that we keep around primarily for backwards compatibility reasons", I personally would say we should be prepared to deprecate them for Python 4 (which is what I'm talking about here, not e.g. 3.8).

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Dec 1, 2017

    Independently of what we eventually decide to do for 4.0, there are some changes we could make at the documentation level to more clearly indicate "Even though this isn't deprecated, you still shouldn't use it for new code": https://bugs.python.org/issue32190

    @ncoghlan ncoghlan removed their assignment Jan 28, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants