pypi

package
v1.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package pypi provides an HTTP client for the Python Package Index API.

Overview

This package fetches package metadata from PyPI (https://pypi.org), the official repository for Python packages.

Usage

client := pypi.NewClient(backend, 24*time.Hour, "3.12")

pkg, err := client.FetchPackage(ctx, "fastapi", false) // false = use cache
if err != nil {
    log.Fatal(err)
}

fmt.Println(pkg.Name, pkg.Version)
fmt.Println("Dependencies:", pkg.Dependencies)

PackageInfo

[FetchPackage] returns a PackageInfo containing:

  • Name, Version: Package identity
  • Dependencies: Direct runtime dependencies (extras/dev filtered out)
  • Summary: Package description
  • License, Author: Package metadata
  • ProjectURLs, HomePage: Links for enrichment

Caching

Responses are cached to reduce load on PyPI and speed up repeated requests. The cache TTL is set when creating the client. Pass refresh=true to [FetchPackage] to bypass the cache.

Dependency Filtering

Dependencies are extracted from requires_dist, filtering out:

  • Optional extras (extra markers)
  • Development dependencies (dev markers)
  • Test dependencies (test markers)

Package names are normalized following PEP 503.

Index

Constants

View Source
const DefaultPythonVersion = "3.11"

DefaultPythonVersion is the default assumed Python version for marker evaluation. Dependencies with markers like `python_version < "3.11"` are skipped.

Variables

This section is empty.

Functions

func CheckPythonVersion

func CheckPythonVersion(pythonVersion, requiresPython string) bool

CheckPythonVersion checks if a Python version satisfies a requires_python constraint. This is a convenience wrapper around constraints.CheckVersionConstraint.

Types

type Client

type Client struct {
	*integrations.Client
	// contains filtered or unexported fields
}

Client provides access to the PyPI package registry API. It handles HTTP requests with caching and automatic retries.

All methods are safe for concurrent use by multiple goroutines.

func NewClient

func NewClient(backend cache.Cache, cacheTTL time.Duration, pythonVersion string) *Client

NewClient creates a PyPI client with the given cache backend.

Parameters:

  • backend: Cache backend for HTTP response caching (use storage.NullBackend{} for no caching)
  • cacheTTL: How long responses are cached (typical: 1-24 hours)
  • pythonVersion: Target Python version for marker evaluation (e.g., "3.11"); empty uses default

The returned Client is safe for concurrent use.

func (*Client) FetchPackage

func (c *Client) FetchPackage(ctx context.Context, pkg string, refresh bool) (*PackageInfo, error)

FetchPackage retrieves metadata for a Python package from PyPI (latest version).

The pkg parameter is normalized automatically (case-insensitive, underscores→hyphens). Package name cannot be empty; an empty string will result in an API error.

If refresh is true, the cache is bypassed and a fresh API call is made. If refresh is false, cached data is returned if available and not expired.

Returns:

The returned PackageInfo pointer is never nil if err is nil. This method is safe for concurrent use.

func (*Client) FetchPackageVersion

func (c *Client) FetchPackageVersion(ctx context.Context, pkg, version string, refresh bool) (*PackageInfo, error)

FetchPackageVersion retrieves metadata for a specific version of a Python package.

The pkg parameter is normalized automatically. The version must be an exact version string (e.g., "2.31.0").

If refresh is true, the cache is bypassed and a fresh API call is made.

Returns:

This method is safe for concurrent use.

func (*Client) ListVersions

func (c *Client) ListVersions(ctx context.Context, pkg string, refresh bool) ([]string, error)

ListVersions returns all available versions for a package. Versions are sorted lexicographically (not by PEP 440), so "1.10.0" sorts before "1.2.0". Pre-release versions are included in the list. Callers that need proper version ordering should sort the result with a PEP 440 comparator.

func (*Client) ListVersionsWithConstraints

func (c *Client) ListVersionsWithConstraints(ctx context.Context, pkg string, refresh bool) (map[string]string, error)

ListVersionsWithConstraints returns all versions and their runtime constraints in a single API call. This is more efficient than calling FetchPackageVersion for each version individually. Returns a map of version -> requires_python constraint (empty string if not specified).

type Dependency

type Dependency struct {
	Name       string // Normalized package name
	Constraint string // Version constraint (e.g., ">=1.0,<2.0"), empty for no constraint
}

Dependency represents a package dependency with version constraint.

type PackageInfo

type PackageInfo struct {
	Name           string            // Normalized package name (e.g., "fastapi", never empty in valid info)
	Version        string            // Version string (e.g., "0.104.1", never empty in valid info)
	RequiresPython string            // Python version constraint (e.g., ">=3.8", may be empty)
	Dependencies   []Dependency      // Direct runtime dependencies with constraints (nil or empty if none)
	ProjectURLs    map[string]string // Project URLs from metadata (e.g., "Homepage", "Repository", may be nil)
	HomePage       string            // Homepage URL (may be empty)
	Summary        string            // Short package description (may be empty)
	License        string            // License name or expression (may be empty)
	LicenseText    string            // Full raw license text for custom/proprietary licenses (may be empty)
	Author         string            // Author name (may be empty)
}

PackageInfo holds metadata for a Python package from PyPI.

Package names are normalized following PEP 503 (lowercase, underscores→hyphens). Dependencies list only runtime dependencies; extras, dev, and test deps are excluded.

Zero values: All string fields are empty, Dependencies is nil. A nil Dependencies slice is valid and indicates no dependencies or failed dependency fetch. This struct is safe for concurrent reads after construction.

func (*PackageInfo) IsCompatibleWith

func (p *PackageInfo) IsCompatibleWith(pythonVersion string) bool

IsCompatibleWith checks if this package is compatible with the given Python version. Returns true if compatible (or if requires_python is not specified), false otherwise.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL