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

Bugfix: Fixes #3763, #3830, and #4453 #5010

Closed
wants to merge 1 commit into from

Conversation

simnalamburt
Copy link

@simnalamburt simnalamburt commented Feb 1, 2018

Prevent --install-option and --global-option from leaking into the next dependencies in the requirements.txt file.

You should not mutate the parameters provided by the caller unless you are aware of what you're doing.

# BAD! Mutating the parameter!
def bad(param):
    param += ['a']

# OK. Binding a new object to the name.
def good(param):
    param = param + ['a']

Fixes #3763, #3830, and #4453.

Reference

@simnalamburt simnalamburt changed the title Fixes #4453 Fixes #3763, #3830, and #4453 Feb 1, 2018
Prevent '--install-option' and '--global-option' from leaking into the
next dependencies in the 'requirements.txt' file.

You should not mutate the parameters provided by the caller unless you
are aware of what you're doing.

    # BAD! Mutating the parameter!
    def bad(param):
        param += ['a']

    # OK. Binding a new object to the name.
    def good(param):
        param = param + ['a']

Fixes pypa#3763, pypa#3830, and pypa#4453.

Reference:
  https://github.com/simnalamburt/snippets/blob/master/python/pip-4453.py
@simnalamburt simnalamburt changed the title Fixes #3763, #3830, and #4453 Bugfix: Fixes #3763, #3830, and #4453 Feb 18, 2018
@pradyunsg pradyunsg mentioned this pull request Mar 1, 2018
@pradyunsg pradyunsg added this to the 10.0 milestone Mar 1, 2018
global_options = \
global_options + self.options.get('global_options', [])
install_options = \
install_options + self.options.get('install_options', [])
Copy link
Member

Choose a reason for hiding this comment

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

It's not at all clear why this makes a difference (I'm assuming it's something to do with in-place modification vs creating a new list?) so at a minimum, I'd want a comment explaining what's going on here. Better still would be a code change that didn't result in the reader having a huge temptation to switch back to the += form 😄

Copy link
Member

Choose a reason for hiding this comment

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

Reading the conversation thread clarifies (must read things in the right order in future :-)) but I'd still like a comment in the code.

Maybe better would be

# Take a copy so we don't mutate the value supplied by the caller
global_options = list(global_options)
global_options += self.options.get('global_options', [])

as that makes the point explicit in the code as well as in the comment.

Copy link
Member

Choose a reason for hiding this comment

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

+1

Having a comment would definitely be helpful here. Nice catch btw.

Choose a reason for hiding this comment

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

Hello @simnalamburt! Thanks for spotting this. 😄

If I may pitch in... If we are to take a copy, I would suggest to put it in another variable.
Currently, the global_options and install_options first refer to the arguments of the method, then refer to a modified copy of them. It can get confusing.

I would suggest something along:

        package_global_options = \
            global_options + self.options.get('global_options', [])
        package_install_options = \
            install_options + self.options.get('install_options', [])

Of course renaming in the next usages as well would be required.

I don't know if the comment should be put in that case, but I believe it is at least much less tempting to refactor into the += form with different names.

Copy link
Author

Choose a reason for hiding this comment

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

If I may pitch in... If we are to take a copy, I would suggest to put it in another variable.

It seems to be the best of the ideas so far. I'll update the PR.

@BrownTruck
Copy link
Contributor

Hello!

I am an automated bot and I have noticed that this pull request is not currently able to be merged. If you are able to either merge the master branch into this pull request or rebase this pull request against master then it will eligible for code review and hopefully merging!

@BrownTruck BrownTruck added the needs rebase or merge PR has conflicts with current master label Mar 21, 2018
@pradyunsg
Copy link
Member

Closing in favour of #5090.

@pradyunsg pradyunsg closed this Mar 21, 2018
@pradyunsg pradyunsg removed this from the 10.0 milestone Mar 21, 2018
@simnalamburt simnalamburt deleted the issue-4453 branch March 27, 2018 08:39
@simnalamburt simnalamburt restored the issue-4453 branch March 27, 2018 08:41
@simnalamburt simnalamburt deleted the issue-4453 branch May 21, 2018 19:36
@lock
Copy link

lock bot commented Jun 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the auto-locked Outdated issues that have been locked by automation label Jun 2, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation needs rebase or merge PR has conflicts with current master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Install option not attributed correctly
5 participants