Skip to content

Commit

Permalink
Breaking out module aliases as distinct from import guessing
Browse files Browse the repository at this point in the history
- moduleToPypiPackageOverride is now exclusively for guessing and
  search overrides.
- moduleToPypiPackageAliases is used only for guessing. Installing by
  way of search will then subsequently dereference the package in
  moduleToPypiPackageOverride, but without burdening the user's search
  experience.
  • Loading branch information
blast-hardcheese committed Jan 2, 2024
1 parent f0a570d commit 9755ebf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions internal/backends/python/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ func filterImports(ctx context.Context, foundPkgs map[string]bool) (map[string][
break
}

// test aliases
overrides, ok = moduleToPypiPackageAliases[testModName]
if ok {
break
}

// test pypi
pkg, ok = pypiMap.ModuleToPackage(testModName)
if ok {
Expand Down
20 changes: 12 additions & 8 deletions internal/backends/python/pypi_map.override.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ var moduleToPypiPackageOverride = map[string][]string{
"faiss": {"faiss-cpu"}, // faiss is offered as precompiled wheels, faiss-cpu and faiss-gpu.
"graphics": {"graphics.py"}, // this package is popular, but the module doesn't match the package name https://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html#a-graphics-introduction
"replit.ai": {"replit-ai"}, // Replit's AI package
/* Proxy packages
*
* These are packages that provide helpful aliases, but otherwise provide no functionality.
* We should prefer the real version.
*/
"discord": {"discord.py"},
"bs4": {"beautifulsoup4"},
"glm": {"PyGLM", "glm"},
"glm": {"PyGLM", "glm"}, // Both of these packages are valid, but PyGLM is a library, glm is an executable.
}

/* Proxy packages
*
* These are packages that provide helpful aliases, but otherwise provide no functionality.
* We should prefer the real version.
*/
var moduleToPypiPackageAliases = map[string][]string{
"bs4": {"beautifulsoup4"},
"discord": {"discord.py"},
"psycopg2": {"psycopg2-binary"}, // psycopg2 is a source package, psycopg2-binary is the dist wheel
}
10 changes: 10 additions & 0 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func add(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName stri
cmd := []string{"poetry", "add"}
for name, spec := range pkgs {
name := string(name)
if found, ok := moduleToPypiPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found[0]
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
}
spec := string(spec)

// NB: this doesn't work if spec has
Expand Down Expand Up @@ -375,6 +380,11 @@ func makePythonPipBackend(python string) api.LanguageBackend {
for name, spec := range pkgs {
name := string(name)
spec := string(spec)
if found, ok := moduleToPypiPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found[0]
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
}

cmd = append(cmd, name+spec)
}
Expand Down

0 comments on commit 9755ebf

Please sign in to comment.