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

Ignore comments from pip freeze when running pipenv clean #2230

Closed
wants to merge 2 commits into from

Conversation

nelfin
Copy link
Contributor

@nelfin nelfin commented May 21, 2018

Addresses #2229

nelfin added 2 commits May 21, 2018 17:05
requirements.txt files (and by extension pipfreeze) can contain comment
lines, but the stdout of pip freeze is used in a number of places with
the assumption that every line contains a dependency declaration. This
change adds an integration test that could trigger one of these cases
that was the cause of the behaviour seen in issue pypa#2229.
Ref pypa#2229. This change only fixes the issue seen when calling `pipenv
clean`. A future refactor should consolidate these behaviours (or use
the actual parsing of lines from pip, i.e. FrozenRequirement, if we can
rely on this API).
@uranusjr
Copy link
Member

While I agree with the logic, it is probably an overkill to create a generator function just for this purpose. It would be much readable to merge this into the previous filter call.

@nelfin
Copy link
Contributor Author

nelfin commented May 22, 2018

HI @uranusjr, thanks for taking the time to review but I disagree that creating a separate function for this is overkill, as it has the advantage that the intent of the action is explicit: the reader can simply choose to trust that the function skips commented lines without having to know the implementation. On the other hand, extending the filter predicate might look something like this:

    installed_packages = filter(
        lambda line: line and not line.lstrip().startswith('#'),
        delegator.run('{0} freeze'.format(which_pip())).out.strip().split(
            '\n'
        ),
    )

Here there reader is presented with the "how", and needs to figure out the "what".

This pull request is actually cut down to a minimal change from a larger set of changes where pip freeze output is treated in a more consistent manner which would justify having this in one place to prevent re-use. As a first-time contributor to this project however, I wasn't going to push up a pull request that touched many parts of the core (at a glance it looks like lock, purge, clean, and outdated all use the output of pip freeze but only purge handles this, see #1268) without first becoming more familiar with the testing and the codebase so that we could all be confident that such a refactor wouldn't break anything existing.

@uranusjr
Copy link
Member

@nelfin There is a middle ground between filtering + iterator and a lambda:

def is_requirement_line_useful(line):
    line = line.strip()
    if not line:
        return False
    if not line.startswith('#'):
        return False
    return True

installed_packages = filter(
    is_requirement_line_useful,
    delegator.run('{0} freeze'.format(which_pip())).out.split('\n'),
)

(Personally I’d prefer a generator to filter in this case, but that’s only my preference.)

@kennethreitz
Copy link
Contributor

+1 to generator, filter is hard to understand/maintain

@uranusjr uranusjr mentioned this pull request May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants