Skip to content

Python Requirements Ignoring Extra Packages #2209

Description

@yckao

Hello there! I’m working with Cog and specifying a requirement like transformers[torch]==0.49.0. However, I’ve noticed that the extra package (i.e., torch) is currently being ignored.

Expected Behavior

Transformers should be installed with the specified extra packages.

Description

After some research, I found this pull request:
#2160

In versions prior to this PR, using transformers[torch]==0.49.0 would raise an error in SplitPinnedPythonRequirement:

func SplitPinnedPythonRequirement(requirement string) (name string, version string, findLinks []string, extraIndexURLs []string, err error) {
pinnedPackageRe := regexp.MustCompile(`(?:([a-zA-Z0-9\-_]+)==([^ ]+)|--find-links=([^\s]+)|-f\s+([^\s]+)|--extra-index-url=([^\s]+))`)
matches := pinnedPackageRe.FindAllStringSubmatch(requirement, -1)
if matches == nil {
return "", "", nil, nil, fmt.Errorf("Package %s is not in the expected format", requirement)
}

Meanwhile, pythonPackageForArch treated it as an unpinned dependency, which unintentionally but worked:

cog/pkg/config/config.go

Lines 397 to 401 in 2a0763a

name, version, findLinksList, extraIndexURLs, err := SplitPinnedPythonRequirement(pkg)
if err != nil {
// It's not pinned, so just return the line verbatim
return pkg, []string{}, []string{}, nil
}

After version 0.14.0, SplitPinnedPythonRequirement no longer raises an error, but the extra package is completely ignored:

// SplitPinnedPythonRequirement returns the name, version, findLinks, and extraIndexURLs from a requirements.txt line
// in the form name[extras]==version [--find-links=<findLink>] [-f <findLink>] [--extra-index-url=<extraIndexURL>]
func SplitPinnedPythonRequirement(requirement string) (name string, version string, findLinks []string, extraIndexURLs []string, err error) {
pinnedPackageRe := regexp.MustCompile(`(?:([a-zA-Z0-9\-_]+)(\[[a-zA-Z0-9\-_,]+])?==([^ ]+)|--find-links=([^\s]+)|-f\s+([^\s]+)|--extra-index-url=([^\s]+))`)
matches := pinnedPackageRe.FindAllStringSubmatch(requirement, -1)
if matches == nil {
return "", "", nil, nil, fmt.Errorf("Package %s is not in the expected format", requirement)
}
nameFound := false
versionFound := false
for _, match := range matches {
if match[1] != "" {
name = match[1]
nameFound = true
}
if match[3] != "" {
version = match[3]
versionFound = true
}
if match[4] != "" {
findLinks = append(findLinks, match[4])
}
if match[5] != "" {
findLinks = append(findLinks, match[5])
}
if match[6] != "" {
extraIndexURLs = append(extraIndexURLs, match[6])
}
}
if !nameFound || !versionFound {
return "", "", nil, nil, fmt.Errorf("Package name or version is missing in %s", requirement)
}
return name, version, findLinks, extraIndexURLs, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    goPull requests that update Go codepythonPull requests that update Python codetype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions