Repository: pypa/pip

Issue:
pip uninstall with req's file fails with --commands at top of file
If you add --find-links, --allow-all-external, --allow-unverified lines at the top of a requirements file, you can no longer use it with

```
pip uninstall -r <filename> 
```

as it thinks there is a syntax error.

The following fixes this... but it may not be the best fix?

Closes #1719



Discussion:
The alternative is to add a bunch of 'and finder' on to parse_requirements

This looks great, can you write a test that fails without this change but passes with it?

Updated the existing test case. If it would be better as a separate test, let me know.

Nope that's fine. Thanks!


Resolving patch:
diff --git a/pip/req/req_file.py b/pip/req/req_file.py
--- a/pip/req/req_file.py
+++ b/pip/req/req_file.py
@@ -85,14 +85,18 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
             # Default in 1.6
             pass
         elif line.startswith('--no-use-wheel'):
-            finder.use_wheel = False
+            if finder:
+                finder.use_wheel = False
         elif line.startswith('--no-index'):
-            finder.index_urls = []
+            if finder:
+                finder.index_urls = []
         elif line.startswith("--allow-external"):
             line = line[len("--allow-external"):].strip().lstrip("=")
-            finder.allow_external |= set([normalize_name(line).lower()])
+            if finder:
+                finder.allow_external |= set([normalize_name(line).lower()])
         elif line.startswith("--allow-all-external"):
-            finder.allow_all_external = True
+            if finder:
+                finder.allow_all_external = True
         # Remove in 1.7
         elif line.startswith("--no-allow-external"):
             pass
@@ -102,10 +106,12 @@ def parse_requirements(filename, finder=None, comes_from=None, options=None,
         # Remove after 1.7
         elif line.startswith("--allow-insecure"):
             line = line[len("--allow-insecure"):].strip().lstrip("=")
-            finder.allow_unverified |= set([normalize_name(line).lower()])
+            if finder:
+                finder.allow_unverified |= set([normalize_name(line).lower()])
         elif line.startswith("--allow-unverified"):
             line = line[len("--allow-unverified"):].strip().lstrip("=")
-            finder.allow_unverified |= set([normalize_name(line).lower()])
+            if finder:
+                finder.allow_unverified |= set([normalize_name(line).lower()])
         else:
             comes_from = '-r %s (line %s)' % (filename, line_number)
             if line.startswith('-e') or line.startswith('--editable'):


Test patch:
diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py
--- a/tests/unit/test_req.py
+++ b/tests/unit/test_req.py
@@ -257,3 +257,23 @@ def test_req_file_parse_egginfo_end_of_line_with_url(tmpdir):
 
     assert len(reqs) == 1
     assert reqs[0].name == "wat"
+
+
+def test_req_file_no_finder(tmpdir):
+    """
+    Test parsing a requirements file without a finder
+    """
+    with open(tmpdir.join("req.txt"), "w") as fp:
+        fp.write("""
+--find-links https://example.com/
+--index-url https://example.com/
+--extra-index-url https://two.example.com/
+--no-use-wheel
+--no-index
+--allow-external foo
+--allow-all-external
+--allow-insecure foo
+--allow-unverified foo
+        """)
+
+    parse_requirements(tmpdir.join("req.txt"), session=PipSession())