Skip to content

Provide decorator for replaced parameters - #5208

Closed
evertrol wants to merge 1 commit into
astropy:masterfrom
evertrol:replaced_parameter-decorator
Closed

evertrol wants to merge 1 commit into
astropy:masterfrom
evertrol:replaced_parameter-decorator

Conversation

@evertrol

Copy link
Copy Markdown
Contributor

The replaced_parameter decorator allows for the old use of a
function or method where a parameter has been deprecated and replaced
by one with a different name. The new parameter should otherwise
function the same.

@evertrol

Copy link
Copy Markdown
Contributor Author

This PR derives from #5171, where the discussion lead to a decorator to handle renamed keyword parameters. I felt that changes were large enough that the decorator part should be its own PR, since it has little to do with the replacement of the clobber keyword parameter.
If people deem otherwise, this PR can simply be closed and I'll move the decorator to #5171.
@MSeifert04 and @pllim may be interested in this PR, as they were involved in #5171. With credits to MSeifert04, who came up with an initial implementation and may have more to add.

NB: naming and wording may want some change. E.g., "renamed" may be better than "replaced".

The ``replaced_parameter`` decorator allows for the old use of a
function or method where a parameter has been deprecated and replaced
by one with a different name. The new parameter should otherwise
function the same.
Comment thread CHANGES.rst

- New decorator: replaced_argument. This can be used for functions
that have renamed a keyword, but still want to allow for use of
the older keyword.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to attach this PR number to the entry. See other entries for example.

@pllim pllim added the utils label Jul 29, 2016
@pllim pllim added this to the v1.3.0 milestone Jul 29, 2016
@pllim

pllim commented Jul 29, 2016

Copy link
Copy Markdown
Member

@evertrol , separate PR is fine, as long as we merge this first, and then update #5171 to use it. Thanks!

@bsipocz

bsipocz commented Jul 29, 2016

Copy link
Copy Markdown
Member

NB: naming and wording may want some change. E.g., "renamed" may be better than "replaced".

I agree, rename sounds better. For replace I think about changing the order rather than the name itself.

def test_replaced_parameter():
@replaced_parameter('clobber', 'overwrite', since='1.3')
def test(a, overwrite=False, c=10):
"""A simple test function

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper functions for tests generally don't need docstrings if they are just dummies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring is to test whether the "..versionchanged" part gets appended properly (see a few lines below). I'll shorten it though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I totally forgot that the versionchanged is only appended if there is already a docstring. Sorry for the noise.

@MSeifert04

MSeifert04 commented Jul 30, 2016 •

Copy link
Copy Markdown
Contributor

@evertrol I also had a commit for the decorator (I was about to make a PR when I realized you already opened one). If you want you can have a look: https://github.com/MSeifert04/astropy/commit/b0d65c6bff58f5a38aaf570cfc7200345b4e24c6

@evertrol

evertrol commented Aug 1, 2016

Copy link
Copy Markdown
Contributor Author

@MSeifert04 If you like, I'm happy to withdraw this PR in place of yours, since you came up with the decorator in the first place. Otherwise, I'll incorporate parts of your PR into mine; the arg_in_kwargs seems useful. Or, as suggested before, you can create a PR against this PR.
I'm not really in favour of a relax option though: I can't think of a case where it's useful to allow both the new and deprecated argument to be used. Mostly where a user updates the function call, but forgets to remove the old argument; in which case it seems more logical to inform the user by raising a TypeError, similar to calling the function with an incorrect argument.

@MSeifert04

Copy link
Copy Markdown
Contributor

@evertrol I know at least one place where relax would be fine: https://github.com/astropy/astropy/blob/master/astropy/stats/sigma_clipping.py#L22 (which is essentially the decorator with relax). I agree that it's not useful in most of the cases but at least it offers the freedom.

I'll don't know if it's easy to make a PR against your branch because they now differ in some aspects. I will take a look later.

@pllim

pllim commented Aug 1, 2016

Copy link
Copy Markdown
Member

@evertrol , @MSeifert04 -- Whichever of you end up with the final PR, you can still credit each other by manually changing the commit author via git commit --amend --author "New Author Name <email>"

@MSeifert04

Copy link
Copy Markdown
Contributor

@pllim Where do you always get these handy informations? I never heard of manually changing the author of the commit 😮 But wouldn't it completly replace the person doing the commit?

I actually don't know what the best way to proceed would be. The decorators behave similar but each has some unique features which would be nice to have in the other one.

@pllim

pllim commented Aug 1, 2016

Copy link
Copy Markdown
Member

@MSeifert04 , I did that in #3094 . If you look at the commits there, you will see some "such-and-such commited with such-and-such" entries.

@bsipocz

bsipocz commented Aug 1, 2016

Copy link
Copy Markdown
Member

@MSeifert04:

Where do you always get these handy informations? I never heard of manually changing the author of the commit 😮 But wouldn't it completly replace the person doing the commit?

It's usually described in git tutorials under "Rewrite history" or similar sections. However you're right, doing what @pllim suggests above completely changes the author, and that's shows up in the git log, etc. However there is also another field: commiter that becomes the person who actually makes the commit (that's why there are two names in those commits in #3094, but only one is the author).
Same happens when you cherry-pick, and think it's clearer and preferable that way, rather than adding names to commits that actually wasn't made by said person.

Needless to say git and git history are thus not suitable for fair credit sharing for e.g. pair coding or team brainstorming, etc.

@evertrol

evertrol commented Aug 1, 2016

Copy link
Copy Markdown
Contributor Author

@MSeifert04 The sigma_clip example is probably where I would prefer to raise a TypeError, so it's really about how backward compatible you'd like to be there (how much user base currently relies on this particular behaviour, using both keywords and ignoring the warning?).
A PR against my branch will require some copy-pasting, as it indeed is unlikely to merge properly with your commit. But I'd be totally fine with having this PR closed and using yours instead. (After all, I'm just after renaming clobber to overwrite.)

@evertrol evertrol closed this Aug 2, 2016
@evertrol
evertrol deleted the replaced_parameter-decorator branch December 1, 2016 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants