Skip to content

Commit

Permalink
Use Requirement.parse to populate extra reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattoberle committed Jun 7, 2022
1 parent 248e653 commit f0faa97
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
17 changes: 3 additions & 14 deletions python/pip_install/extract_wheels/lib/requirements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from typing import Dict, Optional, Set, Tuple

import pkg_resources
Expand Down Expand Up @@ -31,17 +30,7 @@ def _parse_requirement_for_extra(
"""Given a requirement string, returns the requirement name and set of extras, if extras specified.
Else, returns (None, None)
"""

# https://www.python.org/dev/peps/pep-0508/#grammar
extras_pattern = re.compile(
r"^\s*([0-9A-Za-z][0-9A-Za-z_.\-]*)\s*\[\s*([0-9A-Za-z][0-9A-Za-z_.\-]*(?:\s*,\s*[0-9A-Za-z][0-9A-Za-z_.\-]*)*)\s*\]"
)

matches = extras_pattern.match(requirement)
if matches:
return (
pkg_resources.Requirement.parse(matches.group(1)).key,
{extra.strip() for extra in matches.group(2).split(",")},
)

req = pkg_resources.Requirement.parse(requirement)
if req.extras:
return (req.key, set(req.extras))
return None, None
4 changes: 3 additions & 1 deletion python/pip_install/extract_wheels/lib/requirements_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class TestRequirementExtrasParsing(unittest.TestCase):
def test_parses_requirement_for_extra(self) -> None:
cases = [
("name[foo]", ("name", frozenset(["foo"]))),
("name[ Foo123 ]", ("name", frozenset(["Foo123"]))),
("name[ Foo123 ]", ("name", frozenset(["foo123"]))),
(" name1[ foo ] ", ("name1", frozenset(["foo"]))),
("Name[foo]", ("name", frozenset(["foo"]))),
("Name_Foo[bar]", ("name-foo", frozenset(["bar"]))),
(
"name [fred,bar] @ http://foo.com ; python_version=='2.7'",
("name", frozenset(["fred", "bar"])),
Expand Down

0 comments on commit f0faa97

Please sign in to comment.