pyresolve

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 3 Imported by: 0

Documentation

Overview

Package pyresolve answers, from Python source bytes alone, whether an import statement that begins on a given line imports a given dotted module. It backs the `reference-resolves` falsifier's Python arm, mirroring what goImportsAtLine (cmd/corvint/prove.go) does for Go with go/parser.

Tokenization reuses the repository's closed-subset Python lexer (internal/pythongrammar.LexPython312), which already handles comments, prefixed and triple-quoted strings, and backslash line continuation. The statement- and import-grammar walk above the token stream is this package's own: pythongrammar.Token's line/column fields are unexported (so line numbers here are derived independently from byte offsets), and the grammar's own from-import fact emits only the imported module, not each member's fully-qualified name, which this package needs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImportsAtLine

func ImportsAtLine(source []byte, imported string, line int) bool

ImportsAtLine reports whether a Python import statement that begins on `line` (1-based) imports the dotted module `imported`. For a relative import it compares literally against the dotted-with-leading-dots spelling Imports produces (e.g. ".a.b") — it has no package context to resolve a relative import to an absolute name, so it returns false unless `imported` already matches that literal relative form or the resolved form a caller computed with ResolveRelative. It returns false if the source does not tokenize.

func ResolveRelative

func ResolveRelative(importingPackage string, dots int, module string) string

ResolveRelative resolves a relative import's dots and optional dotted suffix into an absolute dotted module name, given the dotted package the importing file resolves relative imports from (its own dotted name for an __init__.py, otherwise its enclosing package). dots is the number of leading dots from the relative import (>=1) and module is the optional dotted suffix after them (empty for `from . import x`). It returns "" if there are not enough package components to climb past.

Types

type Import

type Import struct {
	Line  int
	Names []string
}

Import is one import statement: the 1-based line it begins on, and every dotted name it binds. `from a.b import c` yields "a.b.c" and "a.b"; `import a.b` yields "a.b" only (no prefix packages); `from a import *` yields "a"; a relative import yields names starting with one or more dots, e.g. ".a.b" and ".a" for `from .a import b`, and ".c" and "." for `from . import c`.

func Imports

func Imports(source []byte) ([]Import, error)

Imports lists every import binding in the file in source order. It returns an error if the source does not tokenize (including an unterminated string); a statement that tokenizes but is not a well-formed import is silently skipped rather than treated as a file-wide failure, since this package does not attempt full Python parsing.

Jump to

Keyboard shortcuts

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