Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

87.2 KB
Raw Blame
//! Python: packages, imports, risks, the PyPI-to-crate table, and exported functions and
//! classes with type hints mapped into the TS-like type dialect.

use std::collections::{BTreeSet, HashMap};
use std::path::Path;

use anyhow::Result;
use tree_sitter::Node;

use super::syntax::{self, named_children, text};
use crate::risks::{Risk, regex_needs_fancy};
use crate::ts::deps::CrateMatch;
use crate::ts::discover::{FileKind, Package, SourceFile, Workspace};
use crate::ts::exports::{ExportedClass, ExportedFn, Param};
use crate::ts::scan::{FileFacts, Import};

fn language() -> tree_sitter::Language {
    tree_sitter_python::LANGUAGE.into()
}

pub(crate) const SKIP_DIRS: &[&str] = &[
    "node_modules",
    "venv",
    ".venv",
    "env",
    "__pycache__",
    "build",
    "dist",
    "site-packages",
    ".tox",
    ".nox",
    "target",
    "fixtures",
    "examples",
    "docs",
];

// ---- discovery -------------------------------------------------------------------------------

pub fn discover(root: &Path) -> Result<Workspace> {
    let mut manifests = Vec::new();
    let mut files = Vec::new();
    let walker = crate::walk::filtered(root, |e| {
        !(e.file_type().is_some_and(|t| t.is_dir())
            && SKIP_DIRS.contains(&e.file_name().to_string_lossy().as_ref()))
    })
    .hidden(true)
    .require_git(false)
    .build();
    for entry in walker.flatten() {
        if !entry.file_type().is_some_and(|t| t.is_file()) {
            continue;
        }
        let Ok(rel) = entry.path().strip_prefix(root) else {
            continue;
        };
        let rel = rel.to_string_lossy().replace('\\', "/");
        let name = rel.rsplit('/').next().unwrap_or(&rel);
        if matches!(name, "pyproject.toml" | "setup.py" | "setup.cfg") {
            let dir = rel
                .rsplit_once('/')
                .map_or(String::new(), |(d, _)| d.to_string());
            if !manifests.contains(&dir) {
                manifests.push(dir);
            }
        } else if name.ends_with(".py") && name != "setup.py" && name != "conftest.py" {
            let is_test = name.starts_with("test_")
                || name.ends_with("_test.py")
                || rel
                    .split('/')
                    .any(|p| matches!(p, "tests" | "test" | "testing"));
            files.push((
                rel,
                if is_test {
                    FileKind::Test
                } else {
                    FileKind::Source
                },
            ));
        }
    }
    let mut packages: Vec<Package> = manifests
        .iter()
        .map(|dir| read_package(root, dir))
        .collect();
    if !packages.iter().any(|p| p.dir.is_empty()) {
        packages.push(read_package(root, ""));
    }
    packages.sort_by(|a, b| a.dir.cmp(&b.dir));
    files.sort_by(|a, b| a.0.cmp(&b.0));
    let files = files
        .into_iter()
        .map(|(path, kind)| {
            let package = packages
                .iter()
                .enumerate()
                .filter(|(_, p)| p.dir.is_empty() || path.starts_with(&format!("{}/", p.dir)))
                .max_by_key(|(_, p)| p.dir.len())
                .map_or(0, |(i, _)| i);
            SourceFile {
                path,
                package,
                kind,
            }
        })
        .collect();
    Ok(Workspace { packages, files })
}

fn read_package(root: &Path, dir: &str) -> Package {
    let join = |name: &str| {
        if dir.is_empty() {
            name.to_string()
        } else {
            format!("{dir}/{name}")
        }
    };
    let mut name = None;
    let mut deps = Vec::new();
    if let Ok(text) = std::fs::read_to_string(root.join(join("pyproject.toml")))
        && let Ok(doc) = text.parse::<toml_edit::DocumentMut>()
    {
        name = doc
            .get("project")
            .and_then(|p| p.get("name"))
            .and_then(|n| n.as_str())
            .map(normalized_name);
        if let Some(list) = doc
            .get("project")
            .and_then(|p| p.get("dependencies"))
            .and_then(|d| d.as_array())
        {
            deps.extend(
                list.iter()
                    .filter_map(|d| d.as_str())
                    .filter_map(requirement_name),
            );
        }
        if let Some(t) = doc
            .get("tool")
            .and_then(|t| t.get("poetry"))
            .and_then(|p| p.get("dependencies"))
            .and_then(|d| d.as_table_like())
        {
            deps.extend(
                t.iter()
                    .map(|(k, _)| normalized_name(k))
                    .filter(|k| k != "python"),
            );
        }
    }
    if let Ok(text) = std::fs::read_to_string(root.join(join("setup.cfg"))) {
        deps.extend(setup_cfg_requirements(&text));
    }
    if let Ok(text) = std::fs::read_to_string(root.join(join("setup.py"))) {
        deps.extend(setup_py_requirements(&text));
    }
    let root_canon = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
    requirements_from(
        &root_canon,
        &root.join(join("requirements.txt")),
        &mut deps,
        &mut BTreeSet::new(),
    );
    let src = join("src");
    Package {
        name: name.unwrap_or_else(|| {
            if dir.is_empty() {
                root.file_name()
                    .map_or_else(|| "root".into(), |n| n.to_string_lossy().into_owned())
            } else {
                dir.to_string()
            }
        }),
        src_root: if root.join(&src).is_dir() {
            src
        } else {
            dir.to_string()
        },
        dir: dir.to_string(),
        deps,
        dev_deps: Vec::new(),
        namespaces: Vec::new(),
    }
}

/// A string literal, implicitly joined literals or either in parentheses: the left side of `%`
/// formatting.
fn is_string_literal(node: Node) -> bool {
    match node.kind() {
        "string" | "concatenated_string" => true,
        "parenthesized_expression" => {
            // Comments and `\` line continuations are extras in tree-sitter's Python grammar:
            // `("%s"  # note\n) % x`.
            let mut cursor = node.walk();
            let inner: Vec<Node> = node
                .named_children(&mut cursor)
                .filter(|c| !c.is_extra())
                .collect();
            matches!(inner.as_slice(), [only] if is_string_literal(*only))
        }
        _ => false,
    }
}

/// `requests>=2.0; python_version>"3"` -> `requests`.
/// `None` when the line points at a VCS checkout, an archive URL or a local file. Those are not
/// packages a registry serves: reading the first run of letters names `git+https://...` as `git`
/// and `./vendor/foo.whl` as `.`.
fn requirement_name(spec: &str) -> Option<String> {
    let spec = spec.trim();
    let name: String = spec
        .chars()
        .take_while(|c| c.is_alphanumeric() || "-_.".contains(*c))
        .collect();
    if !name.starts_with(|c: char| c.is_ascii_alphanumeric()) {
        return None;
    }
    let rest = spec[name.len()..].trim_start();
    // A trailing `# comment` after the name leaves the name intact.
    let carried = rest.is_empty()
        || rest.starts_with(['[', '(', '=', '<', '>', '!', '~', ';', ',', '@', '#']);
    carried.then(|| normalized_name(&name))
}

/// `install_requires` under `[options]` in a `setup.cfg`: a single value on the `=` line,
/// indented continuation lines, or both. Comments (`#` or `;`) and blank lines inside the block
/// do not end it; `[options.extras_require]` and other sections are ignored.
fn setup_cfg_requirements(text: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut in_options = false;
    let mut in_install_requires = false;
    for raw in text.lines() {
        let trimmed = raw.trim();
        if trimmed.is_empty() || trimmed.starts_with(['#', ';']) {
            continue;
        }
        let indented = raw.starts_with([' ', '\t']);
        if !indented
            && trimmed.starts_with('[')
            && let Some(end) = trimmed.find(']')
        {
            in_options = trimmed[1..end].trim() == "options";
            in_install_requires = false;
            continue;
        }
        if !in_options {
            continue;
        }
        if indented {
            if in_install_requires {
                out.extend(requirement_name(trimmed));
            }
            continue;
        }
        if let Some((key, value)) = raw.split_once(['=', ':'])
            && key.trim() == "install_requires"
        {
            in_install_requires = true;
            let value = value.trim();
            if !value.is_empty() && !value.starts_with(['#', ';']) {
                out.extend(requirement_name(value));
            }
        } else {
            in_install_requires = false;
        }
    }
    out
}

/// String literals inside `install_requires=[...]` of a `setup(...)` call in `setup.py`.
fn setup_py_requirements(raw: &str) -> Vec<String> {
    let source = raw.strip_prefix('\u{feff}').unwrap_or(raw);
    let Some(tree) = syntax::parse(&language(), source) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    syntax::walk(tree.root_node(), |node| {
        if node.kind() != "call" {
            return;
        }
        let is_setup = node.child_by_field_name("function").is_some_and(|f| {
            let name = text(f, source);
            name == "setup" || name.ends_with(".setup")
        });
        if !is_setup {
            return;
        }
        let Some(args) = node.child_by_field_name("arguments") else {
            return;
        };
        for arg in named_children(args) {
            if arg.kind() != "keyword_argument" {
                continue;
            }
            let Some(kname) = arg.child_by_field_name("name") else {
                continue;
            };
            if text(kname, source) != "install_requires" {
                continue;
            }
            let Some(val) = arg.child_by_field_name("value") else {
                continue;
            };
            if !matches!(val.kind(), "list" | "tuple") {
                continue;
            }
            for item in named_children(val) {
                if let Some(s) = plain_string_literal(item, source) {
                    out.extend(requirement_name(&s));
                }
            }
        }
    });
    out
}

/// The packages a requirements file names, following `-r` / `--requirement` includes: a file that
/// is reached twice is read once, and a cycle stops instead of recursing. `pip` resolves an include
/// against the directory of the file that names it, not the project root.
fn requirements_from(
    root_canon: &Path,
    file: &Path,
    out: &mut Vec<String>,
    seen: &mut BTreeSet<std::path::PathBuf>,
) {
    let Ok(key) = std::fs::canonicalize(file) else {
        return;
    };
    if !key.starts_with(root_canon) || !seen.insert(key.clone()) {
        return;
    }
    let Ok(text) = std::fs::read_to_string(&key) else {
        return;
    };
    let dir = key.parent().unwrap_or(Path::new(""));
    for line in text.lines() {
        let line = line.trim();
        if let Some(rest) = line
            .strip_prefix("--requirement")
            .or_else(|| line.strip_prefix("-r"))
        {
            let included = rest
                .trim_start_matches(['=', ' ', '\t'])
                .split('#')
                .next()
                .unwrap_or("")
                .trim();
            if !included.is_empty() && !Path::new(included).is_absolute() {
                requirements_from(root_canon, &dir.join(included), out, seen);
            }
            continue;
        }
        if line.is_empty() || line.starts_with(['#', '-']) {
            continue;
        }
        out.extend(requirement_name(line));
    }
}

/// A distribution name as pip compares it (PEP 503): `Core_Lib.x` -> `core-lib-x`.
fn normalized_name(name: &str) -> String {
    name.split(['-', '_', '.'])
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>()
        .join("-")
        .to_lowercase()
}

// ---- scanning --------------------------------------------------------------------------------

/// Local names of imported modules and functions, to the dotted name they have in their module:
/// `import re as regex` gives `regex` -> `re`; `from re import sub as s` gives `s` -> `re.sub`.
fn import_aliases(root: Node, source: &str) -> HashMap<String, String> {
    let mut out = HashMap::new();
    syntax::walk(root, |node| match node.kind() {
        "import_statement" => {
            for child in named_children(node) {
                if child.kind() == "aliased_import"
                    && let (Some(name), Some(alias)) = (
                        child.child_by_field_name("name"),
                        child.child_by_field_name("alias"),
                    )
                {
                    out.insert(
                        text(alias, source).to_string(),
                        text(name, source).to_string(),
                    );
                }
            }
        }
        "import_from_statement" => {
            let Some(module) = node.child_by_field_name("module_name") else {
                return;
            };
            let module = text(module, source);
            if module.starts_with('.') {
                return;
            }
            let mut cursor = node.walk();
            for name in node.children_by_field_name("name", &mut cursor) {
                let (orig, local) = match name.kind() {
                    "aliased_import" => (
                        name.child_by_field_name("name"),
                        name.child_by_field_name("alias"),
                    ),
                    _ => (Some(name), Some(name)),
                };
                if let (Some(orig), Some(local)) = (orig, local) {
                    out.insert(
                        text(local, source).to_string(),
                        format!("{module}.{}", text(orig, source)),
                    );
                }
            }
        }
        _ => {}
    });
    out
}

/// `callee` with its first segment replaced by what that name was imported as.
fn resolve_alias(callee: &str, aliases: &HashMap<String, String>) -> String {
    let (first, rest) = callee.split_once('.').map_or((callee, ""), |(f, r)| (f, r));
    match aliases.get(first) {
        Some(full) if rest.is_empty() => full.clone(),
        Some(full) => format!("{full}.{rest}"),
        None => callee.to_string(),
    }
}

/// A set written as the last operand of `in` or `not in` is gone once it has answered, so its order
/// is never seen. In `x in {'a'} in y` the set goes on to `y`, and a set held by a name may be
/// iterated anywhere, so both stay a risk.
fn only_tested(set: Node) -> bool {
    let mut node = set;
    while let Some(parent) = node.parent()
        && parent.kind() == "parenthesized_expression"
    {
        node = parent;
    }
    // Comments and line continuations are siblings too.
    let code = |n: &Node| !n.is_extra();
    node.parent()
        .is_some_and(|p| p.kind() == "comparison_operator")
        && std::iter::successors(node.next_sibling(), Node::next_sibling)
            .find(code)
            .is_none()
        && std::iter::successors(node.prev_sibling(), Node::prev_sibling)
            .find(code)
            .is_some_and(|op| matches!(op.kind(), "in" | "not in"))
}

pub fn scan(source: &str) -> FileFacts {
    let mut facts = FileFacts {
        loc: syntax::loc(source, crate::lang::Lang::Python),
        ..FileFacts::default()
    };
    let Some(tree) = syntax::parse(&language(), source) else {
        return facts;
    };
    let root = tree.root_node();
    facts.parse_errors = usize::from(root.has_error());
    let add = |facts: &mut FileFacts, risk: Risk, node: Node| {
        facts
            .risks
            .entry(risk)
            .or_default()
            .push(syntax::line(node));
    };
    let aliases = import_aliases(root, source);
    syntax::walk(root, |node| match node.kind() {
        "import_statement" => {
            for child in named_children(node) {
                let name = child.child_by_field_name("name").unwrap_or(child);
                facts.imports.push(Import {
                    specifier: text(name, source).to_string(),
                    type_only: false,
                    dynamic: false,
                });
            }
        }
        "import_from_statement" => {
            let Some(module) = node.child_by_field_name("module_name") else {
                return;
            };
            let module = text(module, source).to_string();
            facts.imports.push(Import {
                specifier: module.clone(),
                type_only: false,
                dynamic: false,
            });
            // `from pkg import sub` may name a submodule.
            let mut cursor = node.walk();
            for name in node.children_by_field_name("name", &mut cursor) {
                let name = name.child_by_field_name("name").unwrap_or(name);
                let sep = if module.ends_with('.') { "" } else { "." };
                facts.imports.push(Import {
                    specifier: format!("{module}{sep}{}", text(name, source)),
                    type_only: true,
                    dynamic: false,
                });
            }
        }
        "call" => {
            let Some(function) = node.child_by_field_name("function") else {
                return;
            };
            // `from re import sub`, `import re as regex`: the scan judges the name the module has.
            let resolved = resolve_alias(text(function, source), &aliases);
            let callee = resolved.as_str();
            match callee {
                "eval" | "exec" | "compile" | "__import__" | "importlib.import_module" => {
                    add(&mut facts, Risk::DynamicCode, node)
                }
                "getattr" | "setattr" | "hasattr" | "delattr" | "isinstance" | "type" => {
                    add(&mut facts, Risk::DynamicTyping, node);
                }
                "globals" | "locals" | "vars" => add(&mut facts, Risk::Reflection, node),
                "time.time"
                | "time.time_ns"
                | "time.localtime"
                | "time.gmtime"
                | "time.strftime"
                | "time.ctime"
                | "time.asctime"
                | "datetime.now"
                | "datetime.utcnow"
                | "datetime.today"
                | "date.today"
                | "datetime.datetime.now"
                | "datetime.datetime.utcnow"
                | "datetime.datetime.today"
                | "datetime.date.today"
                | "uuid.uuid4"
                | "os.urandom" => add(&mut facts, Risk::ClockAndRandom, node),
                _ if callee.starts_with("random.") || callee.starts_with("secrets.") => {
                    add(&mut facts, Risk::ClockAndRandom, node);
                }
                "set" | "frozenset" if !only_tested(node) => {
                    add(&mut facts, Risk::UnorderedIteration, node)
                }
                "threading.Thread"
                | "Thread"
                | "ThreadPoolExecutor"
                | "ProcessPoolExecutor"
                | "asyncio.gather" => {
                    add(&mut facts, Risk::Concurrency, node);
                }
                "re.compile" | "re.match" | "re.search" | "re.fullmatch" | "re.sub" | "re.subn"
                | "re.split" | "re.findall" | "re.finditer" => {
                    let pattern = node
                        .child_by_field_name("arguments")
                        .and_then(|a| named_children(a).into_iter().next())
                        // An f-string builds its pattern at runtime.
                        .filter(|a| {
                            a.kind() == "string"
                                && !named_children(*a)
                                    .iter()
                                    .any(|c| c.kind() == "interpolation")
                        });
                    match pattern {
                        Some(p)
                            if regex_needs_fancy(text(p, source), "")
                                || crate::risks::regex_classes_differ(text(p, source)) =>
                        {
                            add(&mut facts, Risk::RegexSyntax, node)
                        }
                        Some(_) => {}
                        None => add(&mut facts, Risk::DynamicRegex, node),
                    }
                }
                "os.getenv" | "os.environ.get" | "os.getcwd" | "getenv" => {
                    add(&mut facts, Risk::SharedState, node)
                }
                _ if callee.ends_with(".find")
                    || callee.ends_with(".index")
                    || callee.ends_with(".rfind") =>
                {
                    add(&mut facts, Risk::StringOffsets, node);
                }
                _ => {}
            }
        }
        "subscript" if named_children(node).iter().any(|c| c.kind() == "slice") => {
            add(&mut facts, Risk::StringOffsets, node)
        }
        "binary_operator" | "augmented_assignment" => {
            let op = node
                .child_by_field_name("operator")
                .map_or("", |o| text(o, source));
            match op {
                // `"..." % args` formats a string; only a number's `%` is modulo.
                "%" if node
                    .child_by_field_name("left")
                    .is_some_and(is_string_literal) => {}
                "//" | "%" | "//=" | "%=" => add(&mut facts, Risk::DivisionSemantics, node),
                "**" | "**=" | "<<" | "<<=" => add(&mut facts, Risk::IntegerSemantics, node),
                "&" | "|" | "^" | ">>" | "&=" | "|=" | "^=" | ">>=" => {
                    add(&mut facts, Risk::NumberSemantics, node)
                }
                _ => {}
            }
        }
        "set" | "set_comprehension" if !only_tested(node) => {
            add(&mut facts, Risk::UnorderedIteration, node)
        }
        "decorator" if crate::risks::compiled_by(text(node, source)).is_some() => {
            add(&mut facts, Risk::AlreadyCompiled, node)
        }
        "typed_parameter" | "typed_default_parameter"
            if node.child_by_field_name("type").is_some_and(|t| {
                let t = text(t, source);
                t.contains("ndarray") || t.contains("NDArray")
            }) =>
        {
            add(&mut facts, Risk::ArrayParameters, node)
        }
        "global_statement" => add(&mut facts, Risk::SharedState, node),
        "raise_statement" | "try_statement" => add(&mut facts, Risk::Exceptions, node),
        "yield" => add(&mut facts, Risk::Generators, node),
        "await" => add(&mut facts, Risk::Async, node),
        "for_statement"
        | "while_statement"
        | "list_comprehension"
        | "dictionary_comprehension"
        | "generator_expression" => {
            facts.loops += 1;
        }
        "class_definition" if node.child_by_field_name("superclasses").is_some() => {
            add(&mut facts, Risk::ClassInheritance, node)
        }
        "function_definition" => {
            if text(node, source).starts_with("async") {
                add(&mut facts, Risk::Async, node);
            }
            if node.child_by_field_name("parameters").is_some_and(|p| {
                named_children(p).iter().any(|c| {
                    c.kind() == "identifier"
                        && text(*c, source) != "self"
                        && text(*c, source) != "cls"
                })
            }) {
                add(&mut facts, Risk::DynamicTyping, node);
            }
        }
        "assignment" => {
            // `Class.method = ...`, `module.attr = ...`: rewriting code at runtime.
            let left = node
                .child_by_field_name("left")
                .map_or("", |l| text(l, source));
            if left.contains('.')
                && !left.starts_with("self.")
                && left.chars().next().is_some_and(char::is_uppercase)
            {
                add(&mut facts, Risk::Monkeypatching, node);
            }
        }
        _ => {}
    });
    facts
}

/// Where a Python import points.
pub enum Resolution {
    File(usize),
    External(String),
    Stdlib,
    Unresolved,
}

/// What [`resolve`] asks of the set of file paths, worked out once for a run: without it every
/// import that is neither a stdlib module nor a library the crate table knows scans every path
/// of the workspace, so a repository of thousands of files takes minutes.
pub struct PathIndex<'a> {
    /// The package source roots and the workspace root (`""`), sorted, without repeats.
    roots: Vec<&'a str>,
    /// Every folder name on a path (all of its components but the file name).
    dir_names: std::collections::HashSet<&'a str>,
    /// The `<module>.py` files by module name: the path and its place in the index.
    modules: HashMap<&'a str, Vec<(&'a str, usize)>>,
}

impl<'a> PathIndex<'a> {
    pub fn new(index: &HashMap<&'a str, usize>, ws: &'a Workspace) -> Self {
        let roots: BTreeSet<&str> = ws
            .packages
            .iter()
            .map(|p| p.src_root.as_str())
            .chain([""])
            .collect();
        let mut dir_names = std::collections::HashSet::new();
        let mut modules: HashMap<&str, Vec<(&str, usize)>> = HashMap::new();
        for (&path, &i) in index {
            let mut parts: Vec<&str> = path.split('/').collect();
            let file = parts.pop().unwrap_or_default();
            dir_names.extend(parts);
            if let Some(module) = file.strip_suffix(".py") {
                modules.entry(module).or_default().push((path, i));
            }
        }
        Self {
            roots: roots.into_iter().collect(),
            dir_names,
            modules,
        }
    }
}

/// Resolves `pkg.mod`, `.mod` and `..mod` against the files of the workspace.
pub fn resolve(from: &str, spec: &str, index: &HashMap<&str, usize>, ws: &Workspace) -> Resolution {
    resolve_with(from, spec, index, &PathIndex::new(index, ws))
}

/// [`resolve`] with the lookups of `paths`, which a run builds once and shares.
pub fn resolve_with(
    from: &str,
    spec: &str,
    index: &HashMap<&str, usize>,
    paths: &PathIndex,
) -> Resolution {
    let try_module = |base: &str| -> Option<usize> {
        let base = base.trim_matches('/');
        [format!("{base}.py"), format!("{base}/__init__.py")]
            .iter()
            .find_map(|c| index.get(c.as_str()).copied())
    };
    if spec.starts_with('.') {
        let dots = spec.chars().take_while(|c| *c == '.').count();
        let mut dir: Vec<&str> = from.split('/').collect();
        dir.pop();
        for _ in 1..dots {
            dir.pop();
        }
        let rest = spec[dots..].replace('.', "/");
        let base = if rest.is_empty() {
            dir.join("/")
        } else {
            format!("{}/{rest}", dir.join("/"))
        };
        return try_module(&base).map_or(Resolution::Unresolved, Resolution::File);
    }
    let path = spec.replace('.', "/");
    for &root in &paths.roots {
        let base = if root.is_empty() {
            path.clone()
        } else {
            format!("{root}/{path}")
        };
        if let Some(i) = try_module(&base) {
            return Resolution::File(i);
        }
    }
    let segments: Vec<&str> = spec.split('.').collect();
    // A script's own folder is first on `sys.path`: a module beside the importing file is the
    // workspace's, whatever the package roots are. A module compiled into the interpreter is
    // found before any path is searched, so a sibling `sys.py` never answers `import sys`.
    if !BUILT_IN.contains(&segments[0])
        && let Some((dir, _)) = from.rsplit_once('/')
        && !in_package(dir, index)
        && let Some(i) = try_module(&format!("{dir}/{path}"))
    {
        return Resolution::File(i);
    }
    if let Some(known) = (2..=segments.len())
        .rev()
        .map(|k| segments[..k].join("."))
        .find(|name| lookup(name).is_some())
    {
        return Resolution::External(known);
    }
    let top = segments[0];
    if STDLIB.contains(&top) {
        Resolution::Stdlib
    } else if has_folder(top, index, paths) {
        // A local package imported by a shorter path than its files: still local.
        Resolution::Unresolved
    } else if let Some(module) = module_elsewhere(top, index, paths) {
        // A module in a folder the code puts on `sys.path` itself.
        module
    } else {
        Resolution::External(top.to_lowercase().replace('_', "-"))
    }
}

/// Modules compiled into CPython on Linux, macOS and Windows (`sys.builtin_module_names`): the
/// import system finds them before it looks at `sys.path`, so no file can stand in for them.
/// Other standard-library modules are files, and a script's sibling of the same name wins.
const BUILT_IN: &[&str] = &[
    "sys",
    "builtins",
    "marshal",
    "gc",
    "time",
    "itertools",
    "errno",
    "atexit",
    "_thread",
];

fn in_package(dir: &str, index: &HashMap<&str, usize>) -> bool {
    index.contains_key(format!("{dir}/__init__.py").as_str())
}

/// The workspace's own `<top>.py` in some other folder, for an import no root and no sibling
/// answered: the file when there is one, unresolved when several share the name. A name the
/// crate table knows stays the library.
fn module_elsewhere(
    top: &str,
    index: &HashMap<&str, usize>,
    paths: &PathIndex,
) -> Option<Resolution> {
    if lookup(top).is_some() {
        return None;
    }
    let outside_a_package = |path: &str| {
        !path
            .rsplit_once('/')
            .is_some_and(|(dir, _)| in_package(dir, index))
    };
    let mut found: Vec<usize> = if top.contains('/') {
        // Not a module name, so not in the table by name: the scan the table replaces.
        let file = format!("{top}.py");
        let nested = format!("/{file}");
        index
            .iter()
            .filter(|(path, _)| **path == file || path.ends_with(&nested))
            .filter(|(path, _)| outside_a_package(path))
            .map(|(_, i)| *i)
            .collect()
    } else {
        paths.modules.get(top).map_or_else(Vec::new, |files| {
            files
                .iter()
                .filter(|(path, _)| outside_a_package(path))
                .map(|(_, i)| *i)
                .collect()
        })
    };
    // One file is the module, several make it ambiguous; the order they were found in is not
    // part of the answer.
    let first = found.pop()?;
    Some(if found.is_empty() {
        Resolution::File(first)
    } else {
        Resolution::Unresolved
    })
}

/// Whether some path of the workspace has a folder called `top`: a local package imported by a
/// shorter path than its files.
fn has_folder(top: &str, index: &HashMap<&str, usize>, paths: &PathIndex) -> bool {
    if top.is_empty() || top.contains('/') {
        // Not a folder name, so not in the set by name: the scan the set replaces.
        return index
            .keys()
            .any(|p| p.starts_with(&format!("{top}/")) || p.contains(&format!("/{top}/")));
    }
    paths.dir_names.contains(top)
}

// The version-agnostic list includes Python 3.12 modules removed in 3.13;
// imports of their backports can therefore also resolve as stdlib.
const STDLIB: &[&str] = &[
    "__future__",
    "__main__",
    "_abc",
    "_aix_support",
    "_ast",
    "_asyncio",
    "_bisect",
    "_blake2",
    "_bz2",
    "_codecs",
    "_codecs_cn",
    "_codecs_hk",
    "_codecs_iso2022",
    "_codecs_jp",
    "_codecs_kr",
    "_codecs_tw",
    "_collections",
    "_collections_abc",
    "_compat_pickle",
    "_compression",
    "_contextvars",
    "_crypt",
    "_csv",
    "_ctypes",
    "_curses",
    "_curses_panel",
    "_datetime",
    "_dbm",
    "_decimal",
    "_elementtree",
    "_frozen_importlib",
    "_frozen_importlib_external",
    "_functools",
    "_gdbm",
    "_hashlib",
    "_heapq",
    "_imp",
    "_io",
    "_json",
    "_locale",
    "_lsprof",
    "_lzma",
    "_markupbase",
    "_md5",
    "_msi",
    "_multibytecodec",
    "_multiprocessing",
    "_opcode",
    "_operator",
    "_osx_support",
    "_overlapped",
    "_pickle",
    "_posixshmem",
    "_posixsubprocess",
    "_py_abc",
    "_pydatetime",
    "_pydecimal",
    "_pyio",
    "_pylong",
    "_queue",
    "_random",
    "_scproxy",
    "_sha1",
    "_sha2",
    "_sha3",
    "_signal",
    "_sitebuiltins",
    "_socket",
    "_sqlite3",
    "_sre",
    "_ssl",
    "_stat",
    "_statistics",
    "_string",
    "_strptime",
    "_struct",
    "_symtable",
    "_thread",
    "_threading_local",
    "_tkinter",
    "_tokenize",
    "_tracemalloc",
    "_typing",
    "_uuid",
    "_warnings",
    "_weakref",
    "_weakrefset",
    "_winapi",
    "_wmi",
    "_zoneinfo",
    "abc",
    "aifc",
    "antigravity",
    "argparse",
    "array",
    "ast",
    "asyncio",
    "atexit",
    "audioop",
    "base64",
    "bdb",
    "binascii",
    "bisect",
    "builtins",
    "bz2",
    "calendar",
    "cgi",
    "cgitb",
    "chunk",
    "cmath",
    "cmd",
    "code",
    "codecs",
    "codeop",
    "collections",
    "colorsys",
    "compileall",
    "concurrent",
    "configparser",
    "contextlib",
    "contextvars",
    "copy",
    "copyreg",
    "cProfile",
    "crypt",
    "csv",
    "ctypes",
    "curses",
    "dataclasses",
    "datetime",
    "dbm",
    "decimal",
    "difflib",
    "dis",
    "doctest",
    "email",
    "encodings",
    "ensurepip",
    "enum",
    "errno",
    "faulthandler",
    "fcntl",
    "filecmp",
    "fileinput",
    "fnmatch",
    "fractions",
    "ftplib",
    "functools",
    "gc",
    "genericpath",
    "getopt",
    "getpass",
    "gettext",
    "glob",
    "graphlib",
    "grp",
    "gzip",
    "hashlib",
    "heapq",
    "hmac",
    "html",
    "http",
    "idlelib",
    "imaplib",
    "imghdr",
    "importlib",
    "inspect",
    "io",
    "ipaddress",
    "itertools",
    "json",
    "keyword",
    "lib2to3",
    "linecache",
    "locale",
    "logging",
    "lzma",
    "mailbox",
    "mailcap",
    "marshal",
    "math",
    "mimetypes",
    "mmap",
    "modulefinder",
    "msilib",
    "msvcrt",
    "multiprocessing",
    "netrc",
    "nis",
    "nntplib",
    "nt",
    "ntpath",
    "nturl2path",
    "numbers",
    "opcode",
    "operator",
    "optparse",
    "os",
    "ossaudiodev",
    "pathlib",
    "pdb",
    "pickle",
    "pickletools",
    "pipes",
    "pkgutil",
    "platform",
    "plistlib",
    "poplib",
    "posix",
    "posixpath",
    "pprint",
    "profile",
    "pstats",
    "pty",
    "pwd",
    "py_compile",
    "pyclbr",
    "pydoc",
    "pydoc_data",
    "pyexpat",
    "queue",
    "quopri",
    "random",
    "re",
    "readline",
    "reprlib",
    "resource",
    "rlcompleter",
    "runpy",
    "sched",
    "secrets",
    "select",
    "selectors",
    "shelve",
    "shlex",
    "shutil",
    "signal",
    "site",
    "smtplib",
    "sndhdr",
    "socket",
    "socketserver",
    "spwd",
    "sqlite3",
    "sre_compile",
    "sre_constants",
    "sre_parse",
    "ssl",
    "stat",
    "statistics",
    "string",
    "stringprep",
    "struct",
    "subprocess",
    "sunau",
    "symtable",
    "sys",
    "sysconfig",
    "syslog",
    "tabnanny",
    "tarfile",
    "telnetlib",
    "tempfile",
    "termios",
    "test",
    "textwrap",
    "this",
    "threading",
    "time",
    "timeit",
    "tkinter",
    "token",
    "tokenize",
    "tomllib",
    "trace",
    "traceback",
    "tracemalloc",
    "tty",
    "turtle",
    "turtledemo",
    "types",
    "typing",
    "typing_extensions",
    "unicodedata",
    "unittest",
    "urllib",
    "uu",
    "uuid",
    "venv",
    "warnings",
    "wave",
    "weakref",
    "webbrowser",
    "winreg",
    "winsound",
    "wsgiref",
    "xdrlib",
    "xml",
    "xmlrpc",
    "zipapp",
    "zipfile",
    "zipimport",
    "zlib",
    "zoneinfo",
];

// ---- dependencies ----------------------------------------------------------------------------

/// The Rust counterpart of a PyPI package (or import name).
pub fn lookup(package: &str) -> Option<CrateMatch> {
    crate::crates::find("python", package)
}

// ---- exports ---------------------------------------------------------------------------------

/// Decorators that keep a function's behavior as seen by callers.
const SAFE_DECORATORS: &[&str] = &[
    "cache",
    "lru_cache",
    "functools.cache",
    "functools.lru_cache",
    "staticmethod",
];

/// Public module-level functions and classes, honoring `__all__`.
pub fn exports(path: &str, source: &str) -> (Vec<ExportedFn>, Vec<ExportedClass>) {
    let Some(tree) = syntax::parse(&language(), source) else {
        return (Vec::new(), Vec::new());
    };
    let root = tree.root_node();
    let all = dunder_all(root, source);
    let public = |name: &str| {
        all.as_ref().map_or(!name.starts_with('_'), |names| {
            names.iter().any(|n| n == name)
        })
    };
    let entry = entry_calls(root, source, path.ends_with("__main__.py"));
    let mut functions = Vec::new();
    let mut classes = Vec::new();
    for item in named_children(root) {
        let (decorators, definition) = unwrap_decorated(item, source);
        match definition.kind() {
            "function_definition" => {
                let Some(mut f) = function(path, definition, source, &decorators, false) else {
                    continue;
                };
                let parser = definition
                    .child_by_field_name("return_type")
                    .is_some_and(|t| text(t, source).ends_with("ArgumentParser"));
                if parser || entry.iter().any(|n| *n == f.name) {
                    f.blocker = Some(
                        "a command-line entry point: it stays in Python, and `app front-door` \
                         takes the command line over when the whole app moves"
                            .into(),
                    );
                }
                if public(&f.name) {
                    functions.push(f);
                }
            }
            "class_definition" => {
                let Some(c) = class(path, definition, source, &decorators) else {
                    continue;
                };
                if public(&c.name) {
                    classes.push(c);
                }
            }
            _ => {}
        }
    }
    (functions, classes)
}

/// Dataclasses declared at the top level of a file, as types `record` can build values of:
/// `{ $object: "Name"; attrs: { field: type; defaulted?: type } }`, which the Python driver
/// turns into an instance of the class.
pub fn dataclass_types(source: &str) -> Vec<crate::ts::exports::LocalType> {
    let Some(tree) = syntax::parse(&language(), source) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for item in named_children(tree.root_node()) {
        let (decorators, definition) = unwrap_decorated(item, source);
        if definition.kind() != "class_definition"
            || !decorators.iter().any(|d| d.contains("dataclass"))
        {
            continue;
        }
        let Some(name) = definition.child_by_field_name("name") else {
            continue;
        };
        let mut fields = Vec::new();
        if let Some(body) = definition.child_by_field_name("body") {
            for statement in named_children(body) {
                let Some(assignment) = named_children(statement)
                    .into_iter()
                    .find(|n| n.kind() == "assignment")
                else {
                    continue;
                };
                let (Some(left), Some(ty)) = (
                    assignment.child_by_field_name("left"),
                    assignment.child_by_field_name("type"),
                ) else {
                    continue;
                };
                let hint = text(ty, source);
                if hint.starts_with("ClassVar") || hint.starts_with("typing.ClassVar") {
                    continue;
                }
                let optional = if assignment.child_by_field_name("right").is_some() {
                    "?"
                } else {
                    ""
                };
                fields.push(format!(
                    "{}{optional}: {}",
                    text(left, source),
                    to_ts_type(hint)
                ));
            }
        }
        let name = text(name, source).to_string();
        out.push(crate::ts::exports::LocalType {
            text: format!(
                "{{ $object: {name:?}; attrs: {{ {} }} }}",
                fields.join("; ")
            ),
            name,
            alias: true,
        });
    }
    out
}

/// Functions the file runs as a program: the calls inside `if __name__ == "__main__":`, or
/// every top-level call in a package's `__main__.py`.
fn entry_calls<'s>(root: Node, source: &'s str, main_module: bool) -> Vec<&'s str> {
    let mut out = Vec::new();
    let mut collect = |node: Node| {
        syntax::walk(node, |n| {
            if n.kind() == "call"
                && let Some(f) = n.child_by_field_name("function")
                && f.kind() == "identifier"
            {
                out.push(text(f, source));
            }
        });
    };
    for statement in named_children(root) {
        let guard = statement.kind() == "if_statement"
            && statement.child_by_field_name("condition").is_some_and(|c| {
                let c = text(c, source).replace(' ', "").replace('\'', "\"");
                c == "__name__==\"__main__\"" || c == "\"__main__\"==__name__"
            });
        if guard {
            if let Some(body) = statement.child_by_field_name("consequence") {
                collect(body);
            }
        } else if main_module && statement.kind() == "expression_statement" {
            collect(statement);
        }
    }
    out
}

/// The names a module's `__all__` lists, when its top level sets it from string literals only:
/// `__all__ = [...]` (with or without a type; the last one wins) and `__all__ += [...]`. `None`
/// when there is no `__all__`, or when it is built or changed any other way (`+ other.__all__`,
/// `sorted(...)`, `__all__.extend(...)`, inside an `if` or `try` block), so the caller falls
/// back to the names without a leading underscore instead of trusting a partial list.
pub(crate) fn dunder_all(root: Node, source: &str) -> Option<Vec<String>> {
    let mut names: Option<Vec<String>> = None;
    for statement in named_children(root) {
        match statement.kind() {
            "expression_statement" => {}
            // Their bodies are not the module's top level.
            "function_definition" | "class_definition" | "decorated_definition" => continue,
            // A change inside a block (`if sys.platform == ...: __all__ += [...]`) cannot be
            // followed.
            _ if mentions_all(statement, source) => return None,
            _ => continue,
        }
        for node in named_children(statement) {
            let left = node.child_by_field_name("left").map(|l| text(l, source));
            match node.kind() {
                "assignment" | "augmented_assignment" if left == Some("__all__") => {
                    // `__all__: list[str]` alone declares it without a value.
                    let Some(right) = node.child_by_field_name("right") else {
                        continue;
                    };
                    let listed = string_literals(right, source)?;
                    match node
                        .child_by_field_name("operator")
                        .map(|o| text(o, source))
                    {
                        None => names = Some(listed),
                        Some("+=") => names.get_or_insert_with(Vec::new).extend(listed),
                        Some(_) => return None,
                    }
                }
                _ if mentions_all(node, source) => return None,
                _ => {}
            }
        }
    }
    names
}

fn plain_string_literal(item: Node, source: &str) -> Option<String> {
    if item.kind() != "string" {
        return None;
    }
    let mut content = String::new();
    for part in named_children(item) {
        match part.kind() {
            // Bytes are not names.
            "string_start" if text(part, source).contains(['b', 'B']) => return None,
            "string_start" | "string_end" => {}
            // Escape sequences and f-string interpolations would need evaluation.
            "string_content" if part.named_child_count() > 0 => return None,
            "string_content" => content.push_str(text(part, source)),
            _ => return None,
        }
    }
    Some(content)
}

/// The values of a list or tuple made only of plain string literals (comments allowed).
fn string_literals(node: Node, source: &str) -> Option<Vec<String>> {
    if !matches!(node.kind(), "list" | "tuple" | "expression_list") {
        return None;
    }
    let mut out = Vec::new();
    for item in named_children(node) {
        match item.kind() {
            "comment" => {}
            "string" => out.push(plain_string_literal(item, source)?),
            _ => return None,
        }
    }
    Some(out)
}

/// Whether the module's own `__all__` appears inside `node`; `other.__all__` and a keyword
/// argument named `__all__` do not count. Below 256 levels the answer is "yes": a tree that deep
/// cannot be read to the end, and the caller then falls back to the names without an underscore.
pub(crate) fn mentions_all(node: Node, source: &str) -> bool {
    mentions_all_checked(node, source).unwrap_or(true)
}

/// Like `mentions_all`, but `None` when the tree is too deep to be read to the end: a caller
/// that wants an exact reason tells this case apart from a truly built `__all__`.
pub(crate) fn mentions_all_checked(node: Node, source: &str) -> Option<bool> {
    mentions_all_at(node, source, 0)
}

fn mentions_all_at(node: Node, source: &str, depth: usize) -> Option<bool> {
    if depth >= 256 {
        return None;
    }
    match node.kind() {
        "identifier" => Some(text(node, source) == "__all__"),
        "attribute" => match node.child_by_field_name("object") {
            Some(object) => mentions_all_at(object, source, depth + 1),
            None => Some(false),
        },
        "keyword_argument" => match node.child_by_field_name("value") {
            Some(value) => mentions_all_at(value, source, depth + 1),
            None => Some(false),
        },
        _ => {
            let mut deep = false;
            for child in named_children(node) {
                match mentions_all_at(child, source, depth + 1) {
                    Some(true) => return Some(true),
                    Some(false) => {}
                    None => deep = true,
                }
            }
            if deep { None } else { Some(false) }
        }
    }
}

fn unwrap_decorated<'t>(node: Node<'t>, source: &str) -> (Vec<String>, Node<'t>) {
    if node.kind() != "decorated_definition" {
        return (Vec::new(), node);
    }
    let decorators = named_children(node)
        .into_iter()
        .filter(|n| n.kind() == "decorator")
        .map(|d| {
            let t = text(d, source).trim_start_matches('@').trim();
            t.split('(').next().unwrap_or(t).to_string()
        })
        .collect();
    (
        decorators,
        node.child_by_field_name("definition").unwrap_or(node),
    )
}

/// Unannotated parameters the body uses as numbers (`r - g`, `x * 0.5`, `max(r, g)`, `x < 1`)
/// become `number`: generated inputs then fit what the code expects. `int(s)`, `float(s)` and
/// `s * n` also take strings, so they say nothing: a parameter typed `number` is never recorded
/// with a string.
pub fn infer_numbers(params: &mut [Param], body: Node, source: &str) {
    let unknown: Vec<String> = params
        .iter()
        .filter(|p| p.ts_type == "unknown" && p.default.is_none())
        .map(|p| p.name.clone())
        .collect();
    if unknown.is_empty() {
        return;
    }
    let mut numeric: Vec<String> = Vec::new();
    let is_number = |n: Node| matches!(n.kind(), "integer" | "float");
    syntax::walk(body, |n| {
        let operands: Vec<Node> = match n.kind() {
            "binary_operator" => {
                let op = n
                    .child_by_field_name("operator")
                    .map_or("", |o| text(o, source));
                let (Some(l), Some(r)) = (
                    n.child_by_field_name("left"),
                    n.child_by_field_name("right"),
                ) else {
                    return;
                };
                match op {
                    "-" | "/" | "//" | "**" => vec![l, r],
                    // `s * n` repeats a string: only a float literal says the other side is a number.
                    "*" if l.kind() == "float" || r.kind() == "float" => vec![l, r],
                    "+" if is_number(l) || is_number(r) => vec![l, r],
                    "%" if is_number(l) => vec![l, r],
                    _ => return,
                }
            }
            "comparison_operator" => {
                let parts = named_children(n);
                if !parts.iter().any(|p| is_number(*p)) {
                    return;
                }
                parts
            }
            "call" => {
                let f = n
                    .child_by_field_name("function")
                    .map_or("", |f| text(f, source));
                if !matches!(f, "min" | "max" | "abs" | "round" | "divmod")
                    && !f.starts_with("math.")
                {
                    return;
                }
                n.child_by_field_name("arguments")
                    .map(named_children)
                    .unwrap_or_default()
            }
            _ => return,
        };
        for o in operands {
            if o.kind() == "identifier" {
                let name = text(o, source).to_string();
                if unknown.contains(&name) && !numeric.contains(&name) {
                    numeric.push(name);
                }
            }
        }
    });
    for p in params.iter_mut().filter(|p| numeric.contains(&p.name)) {
        p.ts_type = "number".into();
    }
}

fn function(
    path: &str,
    node: Node,
    source: &str,
    decorators: &[String],
    method: bool,
) -> Option<ExportedFn> {
    let name = text(node.child_by_field_name("name")?, source).to_string();
    let (mut params, mut blocker) =
        parameters(node.child_by_field_name("parameters")?, source, method);
    let body = node.child_by_field_name("body");
    if let Some(body) = body {
        infer_numbers(&mut params, body, source);
    }
    let mut generator = false;
    if let Some(body) = body {
        syntax::walk(body, |n| {
            if n.kind() == "yield" {
                generator = true;
            }
        });
    }
    // An untyped parameter that the body calls is a callable: it cannot cross a bridge.
    if let Some(body) = body {
        let names: Vec<&str> = params.iter().map(|p| p.name.as_str()).collect();
        syntax::walk(body, |n| {
            if n.kind() == "call"
                && let Some(f) = n.child_by_field_name("function")
                && f.kind() == "identifier"
                && names.contains(&text(f, source))
                && blocker.is_none()
            {
                blocker = Some(format!(
                    "calls its parameter `{}`: a callable",
                    text(f, source)
                ));
            }
        });
    }
    if let Some(d) = decorators
        .iter()
        .find(|d| !SAFE_DECORATORS.contains(&d.as_str()))
    {
        blocker = blocker.or_else(|| Some(format!("decorated with `@{d}`")));
    }
    let mut returns: String = node
        .child_by_field_name("return_type")
        .map_or_else(|| "unknown".into(), |r| to_ts_type(text(r, source)));
    // The annotation says `None`, or there is none and the body never returns a value.
    if !generator
        && returns == "unknown"
        && let Some(body) = body
        && !super::effects::has_value_return(super::Lang::Python, text(body, source))
    {
        returns = "undefined".into();
    }
    if blocker.is_none() {
        let names: Vec<&str> = params.iter().map(|p| p.name.as_str()).collect();
        // A method's effects stay unseen (the calls after it show the instance, not the
        // arguments); a top-level function's are recorded.
        blocker = if method {
            super::effects::reason(super::Lang::Python, &names, text(node, source))
        } else {
            super::effects::function_reason(super::Lang::Python, &names, text(node, source))
        };
    }
    let (mutates, prints) = if method {
        (Vec::new(), false)
    } else {
        crate::ts::exports::observed_effects(super::Lang::Python, &params, text(node, source))
    };
    Some(ExportedFn {
        mutates,
        prints,
        name,
        file: path.to_string(),
        line: syntax::line(node),
        params,
        returns,
        is_async: text(node, source).starts_with("async"),
        is_generator: generator,
        blocker,
        ..Default::default()
    })
}

/// Parameters in the type dialect, and a reason when the signature cannot cross a bridge.
fn parameters(node: Node, source: &str, method: bool) -> (Vec<Param>, Option<String>) {
    let mut params = Vec::new();
    let mut blocker = None;
    let mut keyword_only = false;
    for (i, p) in named_children(node).into_iter().enumerate() {
        let name = |n: Option<Node>| n.map(|n| text(n, source).to_string()).unwrap_or_default();
        let param = match p.kind() {
            "identifier" => Param {
                name: text(p, source).into(),
                ts_type: "unknown".into(),
                ..Default::default()
            },
            "typed_parameter" => {
                let inner = named_children(p).into_iter().next();
                let ty = p
                    .child_by_field_name("type")
                    .map_or_else(|| "unknown".into(), |t| to_ts_type(text(t, source)));
                match inner.map(|n| n.kind()) {
                    Some("list_splat_pattern") => {
                        blocker = Some("takes `*args`".into());
                        continue;
                    }
                    Some("dictionary_splat_pattern") => {
                        blocker = Some("takes `**kwargs`".into());
                        continue;
                    }
                    _ => Param {
                        name: name(inner),
                        ts_type: ty,
                        ..Default::default()
                    },
                }
            }
            "default_parameter" | "typed_default_parameter" => {
                let value = p.child_by_field_name("value");
                let default = value.map(|v| text(v, source).to_string());
                let ty = match p.child_by_field_name("type") {
                    Some(t) => to_ts_type(text(t, source)),
                    None => match value.map(|v| v.kind()) {
                        Some("integer") => "int".into(),
                        Some("float") => "number".into(),
                        Some("string") => "string".into(),
                        Some("true" | "false") => "boolean".into(),
                        _ => "unknown".into(),
                    },
                };
                Param {
                    name: name(p.child_by_field_name("name")),
                    ts_type: ty,
                    optional: true,
                    default,
                    ..Default::default()
                }
            }
            "list_splat_pattern" => {
                blocker = Some("takes `*args`".into());
                continue;
            }
            "dictionary_splat_pattern" => {
                blocker = Some("takes `**kwargs`".into());
                continue;
            }
            "keyword_separator" => {
                keyword_only = true;
                continue;
            }
            _ => continue,
        };
        if method && i == 0 && (param.name == "self" || param.name == "cls") {
            continue;
        }
        params.push(Param {
            keyword_only,
            ..param
        });
    }
    (params, blocker)
}

fn class(path: &str, node: Node, source: &str, decorators: &[String]) -> Option<ExportedClass> {
    let name = text(node.child_by_field_name("name")?, source).to_string();
    let mut blockers = Vec::new();
    if let Some(bases) = node.child_by_field_name("superclasses") {
        let bases: Vec<&str> = named_children(bases)
            .into_iter()
            .map(|b| text(b, source))
            .filter(|b| *b != "object")
            .collect();
        if !bases.is_empty() {
            blockers.push(format!("inherits from {}", bases.join(", ")));
        }
    }
    if let Some(d) = decorators.first() {
        blockers.push(format!("decorated with `@{d}`"));
    }
    let mut constructor = Vec::new();
    let mut methods = Vec::new();
    for item in named_children(node.child_by_field_name("body")?) {
        let (decorators, definition) = unwrap_decorated(item, source);
        match definition.kind() {
            "function_definition" => {
                let method_name = definition
                    .child_by_field_name("name")
                    .map_or("", |n| text(n, source));
                if method_name == "__init__" {
                    let (params, blocker) =
                        parameters(definition.child_by_field_name("parameters")?, source, true);
                    constructor = params;
                    blockers.extend(blocker);
                    // `self.name = ...` with a public name: callers can read the attribute.
                    if let Some(body) = definition.child_by_field_name("body") {
                        syntax::walk(body, |n| {
                            if n.kind() == "assignment"
                                && let Some(left) = n.child_by_field_name("left")
                                && let Some(attr) = text(left, source).strip_prefix("self.")
                                && !attr.starts_with('_')
                                && !attr.contains('.')
                            {
                                blockers.push(format!("public attribute `{attr}`"));
                            }
                        });
                    }
                } else if method_name.starts_with("__") && method_name.ends_with("__") {
                    blockers.push(format!("special method `{method_name}`"));
                } else if method_name.starts_with('_') {
                } else if let Some(d) = decorators.first() {
                    blockers.push(format!("`@{d}` method `{method_name}`"));
                } else if let Some(f) = function(path, definition, source, &[], true) {
                    if f.is_async || f.is_generator {
                        blockers.push(format!("async or generator method `{}`", f.name));
                    }
                    if f.returns
                        .split(|c: char| !(c.is_alphanumeric() || c == '_'))
                        .any(|w| w == name || w == "Self")
                    {
                        blockers.push(format!("method `{}` returns an instance", f.name));
                    }
                    methods.push(f);
                }
            }
            "expression_statement" => {
                let assignment = named_children(definition)
                    .into_iter()
                    .find(|n| n.kind() == "assignment");
                if let Some(left) = assignment.and_then(|a| a.child_by_field_name("left")) {
                    let attr = text(left, source);
                    if !attr.starts_with('_') {
                        blockers.push(format!("public class attribute `{attr}`"));
                    }
                }
            }
            _ => {}
        }
    }
    blockers.sort();
    blockers.dedup();
    Some(ExportedClass {
        name,
        file: path.to_string(),
        line: syntax::line(node),
        constructor,
        methods,
        blocker: (!blockers.is_empty()).then(|| blockers.join(", ")),
    })
}

/// A Python type hint in the TS-like dialect: `list[str]` -> `string[]`, `Optional[int]` ->
/// `int | undefined`, `Callable[...]` -> a function type (which keeps the export in Python).
pub fn to_ts_type(hint: &str) -> String {
    let hint = hint.trim().trim_matches(|c| c == '"' || c == '\'');
    let hint = hint.strip_prefix("typing.").unwrap_or(hint);
    let top = split_top(hint, '|');
    if top.len() > 1 {
        return top
            .iter()
            .map(|p| to_ts_type(p))
            .collect::<Vec<_>>()
            .join(" | ");
    }
    let (head, args) = match hint.find('[') {
        Some(i) if hint.ends_with(']') => {
            (&hint[..i], split_top(&hint[i + 1..hint.len() - 1], ','))
        }
        _ => (hint, Vec::new()),
    };
    let head = head.trim().strip_prefix("typing.").unwrap_or(head.trim());
    let arg = |i: usize| {
        args.get(i)
            .map_or_else(|| "unknown".to_string(), |a| to_ts_type(a))
    };
    match head {
        "str" => "string".into(),
        "int" => "int".into(),
        "float" => "number".into(),
        "bool" => "boolean".into(),
        "bytes" | "bytearray" | "memoryview" => "pybytes".into(),
        // A path object, not a string: code calls `.stem` and `.is_dir()` on it. The classes
        // that name a flavour (`PureWindowsPath`, `PosixPath`, ...) stay named classes: a value
        // rebuilt as `pathlib.Path` would follow this platform's rules, not theirs.
        "Path" | "PurePath" | "PathLike" | "pathlib.Path" | "pathlib.PurePath" | "os.PathLike" => {
            "pypath".into()
        }
        "None" | "NoneType" => "undefined".into(),
        "list" | "List" | "Sequence" | "Iterable" | "MutableSequence" | "Collection" => {
            format!("{}[]", wrap(&arg(0)))
        }
        "Optional" => format!("{} | undefined", arg(0)),
        "Union" => args
            .iter()
            .map(|a| to_ts_type(a))
            .collect::<Vec<_>>()
            .join(" | "),
        "dict" | "Dict" | "Mapping" | "MutableMapping" if arg(0) == "string" => {
            format!("Record<string, {}>", arg(1))
        }
        "Literal" => args
            .iter()
            .map(|a| a.trim().replace('"', "'"))
            .collect::<Vec<_>>()
            .join(" | "),
        "Callable" => "(...args: unknown[]) => unknown".into(),
        "Any" | "object" => "unknown".into(),
        // Tuples, sets, other dicts and named classes cross as JSON.
        other => other.to_string(),
    }
}

fn wrap(ts: &str) -> String {
    if ts.contains('|') {
        format!("({ts})")
    } else {
        ts.to_string()
    }
}

fn split_top(text: &str, sep: char) -> Vec<String> {
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut current = String::new();
    for c in text.chars() {
        match c {
            '[' | '(' => depth += 1,
            ']' | ')' => depth -= 1,
            _ => {}
        }
        if c == sep && depth == 0 {
            out.push(current.trim().to_string());
            current.clear();
        } else {
            current.push(c);
        }
    }
    if !current.trim().is_empty() {
        out.push(current.trim().to_string());
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn maps_type_hints() {
        assert_eq!(to_ts_type("list[str]"), "string[]");
        assert_eq!(to_ts_type("Optional[int]"), "int | undefined");
        assert_eq!(to_ts_type("str | None"), "string | undefined");
        assert_eq!(
            to_ts_type("dict[str, list[float]]"),
            "Record<string, number[]>"
        );
        assert_eq!(to_ts_type("Literal['a', \"b\"]"), "'a' | 'b'");
        assert!(to_ts_type("Callable[[int], str]").contains("=>"));
        assert_eq!(to_ts_type("tuple[int, int]"), "tuple");
    }

    #[test]
    fn requirement_name_reads_the_package_of_a_requirements_line() {
        assert_eq!(requirement_name("requests").as_deref(), Some("requests"));
        assert_eq!(
            requirement_name("requests>=2.0").as_deref(),
            Some("requests")
        );
        assert_eq!(
            requirement_name("requests >= 2").as_deref(),
            Some("requests")
        );
        assert_eq!(
            requirement_name("uvicorn[standard]==0.30").as_deref(),
            Some("uvicorn")
        );
        assert_eq!(
            requirement_name("requests>=2.0; python_version>\"3\"").as_deref(),
            Some("requests")
        );
        assert_eq!(
            requirement_name("requests # pinned").as_deref(),
            Some("requests")
        );
        assert_eq!(requirement_name("git+https://example.com/repo.git"), None);
        assert_eq!(requirement_name("./local.whl"), None);
    }

    #[test]
    fn normalized_name_is_the_pep_503_form() {
        assert_eq!(normalized_name("Core_Lib.x"), "core-lib-x");
        assert_eq!(normalized_name("Requests"), "requests");
        assert_eq!(normalized_name("my_package"), "my-package");
        assert_eq!(normalized_name("my.package"), "my-package");
        assert_eq!(normalized_name("a__b..c"), "a-b-c");
    }

    #[test]
    fn unix_stdlib_is_not_external() {
        // `import fcntl` in a pure-Python package must not surface as an unknown
        // (npm) dependency: it is Unix stdlib with no PyPI counterpart to look up.
        for module in [
            "fcntl", "termios", "resource", "grp", "pwd", "posix", "syslog",
        ] {
            assert!(
                STDLIB.contains(&module),
                "{module} should resolve as stdlib"
            );
        }
    }

    #[test]
    fn standard_library_imports_resolve_without_dependencies() {
        let ws = Workspace {
            packages: vec![],
            files: vec![],
        };
        let index = HashMap::new();
        for module in [
            "faulthandler",
            "site",
            "bdb",
            "graphlib",
            "_thread",
            "telnetlib",
            "cgi",
        ] {
            assert!(
                matches!(resolve("main.py", module, &index, &ws), Resolution::Stdlib),
                "{module}"
            );
        }
        assert!(matches!(
            resolve("main.py", "requests", &index, &ws),
            Resolution::External(_)
        ));
        let local = HashMap::from([("site.py", 0)]);
        assert!(matches!(
            resolve("main.py", "site", &local, &ws),
            Resolution::File(0)
        ));
    }

    #[test]
    fn a_module_beside_the_script_or_on_its_own_sys_path_is_local() {
        let ws = Workspace {
            packages: vec![],
            files: vec![],
        };
        let index = HashMap::from([
            ("hooks/_kit.py", 0),
            ("hooks/stop_guard.py", 1),
            ("bin/gates.py", 2),
            ("a/util.py", 3),
            ("b/util.py", 4),
            ("tools/yaml.py", 5),
            ("hooks/sys.py", 6),
            ("hooks/json.py", 7),
            ("app/__init__.py", 8),
            ("app/redis.py", 9),
            ("app/core.py", 10),
            ("app/boltons.py", 11),
        ]);
        match resolve("app/core.py", "redis", &index, &ws) {
            Resolution::External(name) => assert_eq!(name, "redis"),
            _ => panic!("redis in a package: not external"),
        }
        match resolve("hooks/stop_guard.py", "boltons", &index, &ws) {
            Resolution::External(name) => assert_eq!(name, "boltons"),
            _ => panic!("boltons: not external"),
        }
        // A module compiled into the interpreter is found before `sys.path` is searched; any
        // other standard-library module is a file, and the script's sibling comes first.
        assert!(matches!(
            resolve("hooks/stop_guard.py", "sys", &index, &ws),
            Resolution::Stdlib
        ));
        assert!(matches!(
            resolve("hooks/stop_guard.py", "json", &index, &ws),
            Resolution::File(7)
        ));
        // `import _kit` from a script in the same folder: the script's folder is on `sys.path`.
        assert!(matches!(
            resolve("hooks/stop_guard.py", "_kit", &index, &ws),
            Resolution::File(0)
        ));
        // `import gates` after `sys.path.insert(0, ".../bin")`: the only `gates.py` there is.
        assert!(matches!(
            resolve("hooks/stop_guard.py", "gates", &index, &ws),
            Resolution::File(2)
        ));
        // The sibling wins over a module of the same name elsewhere.
        assert!(matches!(
            resolve("b/main.py", "util", &index, &ws),
            Resolution::File(4)
        ));
        // Two candidates and no sibling: local, but not one file.
        assert!(matches!(
            resolve("hooks/stop_guard.py", "util", &index, &ws),
            Resolution::Unresolved
        ));
        // A library the crate table knows is not hidden by a file of its name elsewhere.
        match resolve("hooks/stop_guard.py", "yaml", &index, &ws) {
            Resolution::External(name) => assert_eq!(name, "yaml"),
            _ => panic!("yaml: not external"),
        }
    }

    /// The scans `has_folder` and `module_elsewhere` replaced, as they were.
    fn folder_by_scan(top: &str, index: &HashMap<&str, usize>) -> bool {
        index
            .keys()
            .any(|p| p.starts_with(&format!("{top}/")) || p.contains(&format!("/{top}/")))
    }

    fn module_by_scan(top: &str, index: &HashMap<&str, usize>) -> Option<Resolution> {
        if lookup(top).is_some() {
            return None;
        }
        let file = format!("{top}.py");
        let nested = format!("/{file}");
        let mut found = index
            .iter()
            .filter(|(path, _)| **path == file || path.ends_with(&nested))
            .filter(|(path, _)| {
                !path
                    .rsplit_once('/')
                    .is_some_and(|(dir, _)| in_package(dir, index))
            })
            .map(|(_, i)| *i);
        let first = found.next()?;
        Some(match found.next() {
            None => Resolution::File(first),
            Some(_) => Resolution::Unresolved,
        })
    }

    fn shown(r: &Option<Resolution>) -> String {
        match r {
            None => "none".into(),
            Some(Resolution::File(i)) => format!("file {i}"),
            Some(Resolution::External(n)) => format!("external {n}"),
            Some(Resolution::Stdlib) => "stdlib".into(),
            Some(Resolution::Unresolved) => "unresolved".into(),
        }
    }

    #[test]
    fn the_path_index_answers_like_the_scans_it_replaces() {
        let ws = Workspace {
            packages: vec![],
            files: vec![],
        };
        let index = HashMap::from([
            ("pkg/__init__.py", 0),
            ("pkg/core.py", 1),
            ("pkg/sub/leaf.py", 2),
            ("tools/helper.py", 3),
            ("scripts/helper.py", 4),
            ("scripts/solo.py", 5),
            ("top.py", 6),
            ("a/top/inner.py", 7),
            ("a/file_named_dir", 8),
            ("notes.txt", 9),
            ("pkg/yaml.py", 10),
        ]);
        let paths = PathIndex::new(&index, &ws);
        for top in [
            "pkg",
            "sub",
            "tools",
            "scripts",
            "top",
            "a",
            "inner",
            "leaf",
            "core",
            "helper",
            "solo",
            "notes",
            "file_named_dir",
            "yaml",
            "nothing",
            "",
            "a/b",
            "pkg/sub",
        ] {
            assert_eq!(
                has_folder(top, &index, &paths),
                folder_by_scan(top, &index),
                "folder `{top}`"
            );
            assert_eq!(
                shown(&module_elsewhere(top, &index, &paths)),
                shown(&module_by_scan(top, &index)),
                "module `{top}`"
            );
        }
    }

    #[test]
    fn resolving_many_imports_of_a_big_workspace_stays_fast() {
        // 10,000 files and 10,000 imports of libraries the crate table does not know: each one
        // used to scan every path twice, with an allocation per path, about 10^8 allocations.
        let paths_owned: Vec<String> = (0..10_000)
            .map(|i| format!("src/pkg{}/mod{i}.py", i % 100))
            .collect();
        let index: HashMap<&str, usize> = paths_owned
            .iter()
            .enumerate()
            .map(|(i, p)| (p.as_str(), i))
            .collect();
        let ws = Workspace {
            packages: vec![],
            files: vec![],
        };
        let shared = PathIndex::new(&index, &ws);
        let started = std::time::Instant::now();
        for i in 0..10_000 {
            let spec = format!("unknownlib{i}.thing");
            assert!(matches!(
                resolve_with("src/pkg1/mod1.py", &spec, &index, &shared),
                Resolution::External(_)
            ));
        }
        assert!(
            started.elapsed() < std::time::Duration::from_secs(5),
            "took {:?}",
            started.elapsed()
        );
    }

    #[test]
    fn a_dotted_import_the_table_knows_keeps_its_dotted_name() {
        let ws = Workspace {
            packages: vec![],
            files: vec![],
        };
        let index = HashMap::new();
        for spec in ["google.protobuf", "google.protobuf.json_format"] {
            match resolve("main.py", spec, &index, &ws) {
                Resolution::External(name) => assert_eq!(name, "google.protobuf", "{spec}"),
                _ => panic!("{spec}: not external"),
            }
        }
        match resolve("main.py", "yaml.loader", &index, &ws) {
            Resolution::External(name) => assert_eq!(name, "yaml"),
            _ => panic!("yaml.loader: not external"),
        }
    }

    #[test]
    fn reads_exports() {
        let source = "__all__ = ['slug', 'Counter']\n\n\
            def slug(text: str, sep: str = '-', *, lower=True) -> str:\n    return text\n\n\
            def hidden(x): pass\n\n\
            @lru_cache\ndef cached(n: int) -> int:\n    return n\n\n\
            def gen(n: int):\n    yield n\n\n\
            class Counter:\n    def __init__(self, start: int = 0):\n        self._n = start\n    def add(self, k: int) -> int:\n        return k\n    def _p(self): pass\n\n\
            class Point:\n    def __init__(self, x):\n        self.x = x\n";
        let (functions, classes) = exports("m.py", source);
        assert_eq!(
            functions
                .iter()
                .map(|f| f.name.as_str())
                .collect::<Vec<_>>(),
            ["slug"]
        );
        let slug = &functions[0];
        assert_eq!(slug.params[1].default.as_deref(), Some("'-'"));
        assert!(slug.params[2].keyword_only && slug.params[2].ts_type == "boolean");
        assert_eq!(slug.returns, "string");
        assert_eq!(classes.len(), 1);
        assert!(classes[0].blocker.is_none());
        assert_eq!(classes[0].methods.len(), 1);

        let (all, classes) = exports(
            "n.py",
            &source.replace("__all__ = ['slug', 'Counter']\n", ""),
        );
        let names: Vec<&str> = all.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, ["slug", "hidden", "cached", "gen"]);
        assert!(all[3].is_generator);
        assert!(
            classes[1]
                .blocker
                .as_deref()
                .unwrap()
                .contains("public attribute `x`")
        );
    }

    #[test]
    fn reads_all_after_a_docstring_and_imports() {
        let source = "\"\"\"Text helpers.\"\"\"\n\
            from os import path\n\
            import re\n\n\
            __all__ = ['slug', '_kept']\n\n\
            def slug(text: str) -> str:\n    return text\n\n\
            def helper(x: int) -> int:\n    return x\n\n\
            def _kept(x: int) -> int:\n    return x\n";
        let (functions, _) = exports("m.py", source);
        let names: Vec<&str> = functions.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, ["slug", "_kept"]);
    }

    fn exported(source: &str) -> Vec<String> {
        let defs = "\ndef a(x: int) -> int:\n    return x\n\n\
            def b(x: int) -> int:\n    return x\n\n\
            def _c(x: int) -> int:\n    return x\n";
        exports("m.py", &format!("{source}{defs}"))
            .0
            .into_iter()
            .map(|f| f.name)
            .collect()
    }

    #[test]
    fn later_changes_to_all_are_followed() {
        assert_eq!(
            exported("__all__ = ['a']\n__all__ += ['_c']\n"),
            ["a", "_c"]
        );
        assert_eq!(exported("__all__ = ['a']\n__all__ = ['b']\n"), ["b"]);
    }

    #[test]
    fn annotated_prefixed_and_commented_all_is_read() {
        let source =
            "__all__: list[str]\n__all__: list[str] = [\n    r'a',  # first\n    \"_c\",\n]\n";
        assert_eq!(exported(source), ["a", "_c"]);
    }

    #[test]
    fn an_all_built_any_other_way_falls_back_to_public_names() {
        for source in [
            "import other\n__all__ = ['a'] + other.__all__\n",
            "__all__ = sorted(['a'])\n",
            "__all__ = ['a']\n__all__.extend(['_c'])\n",
            "__all__ = [f'{1}']\n",
            "import sys\n__all__ = ['a']\nif sys.platform == 'win32':\n    __all__ += ['_c']\n",
            "__all__ = ['\\x61']\n",
            "__all__ = [b'a']\n",
        ] {
            assert_eq!(exported(source), ["a", "b"], "{source}");
        }
    }

    #[test]
    fn another_module_all_and_reads_in_functions_keep_the_list() {
        let source = "import other\n__all__ = ['a']\nnames = other.__all__\n\n\
            def __dir__():\n    return __all__\n";
        assert_eq!(exported(source), ["a"]);
    }

    #[test]
    fn scans_risks_and_imports() {
        let facts = scan(
            "import os\nfrom .util import helper\nimport re\nx = 7 // 2\nx &= 0xff\nx |= 1\nx ^= 2\nx >>= 1\nre.compile(r'(?<=a)b')\nfor i in range(3): pass\n",
        );
        let specs: Vec<&str> = facts.imports.iter().map(|i| i.specifier.as_str()).collect();
        assert!(
            specs.contains(&"os") && specs.contains(&".util") && specs.contains(&".util.helper")
        );
        assert!(facts.risks.contains_key(&Risk::DivisionSemantics));
        assert_eq!(
            facts.risks.get(&Risk::NumberSemantics).map(Vec::as_slice),
            Some(&[5, 6, 7, 8][..])
        );
        assert!(facts.risks.contains_key(&Risk::RegexSyntax));
        assert_eq!(facts.loops, 1);
    }

    #[test]
    fn percent_formatting_of_a_string_literal_is_not_modulo() {
        let facts = scan(
            "a = 'x=%s' % x\nb = ('x=%s, '\n     'y=%s' % (x, y))\nc = (\"%d\" \"%d\") % (1, 2)\n\
             d = b'%d' % 3\ne = 7 % n\nf = name % n\nt %= 4\ng = ('%s'  # note\n     ) % x\nh = ('%s' \\\n     ) % x\n",
        );
        assert_eq!(
            facts.risks.get(&Risk::DivisionSemantics).map(Vec::as_slice),
            Some(&[6, 7, 8][..])
        );
    }

    #[test]
    fn a_set_tested_where_it_is_written_has_no_order_to_keep() {
        let risk = |source: &str| scan(source).risks.get(&Risk::UnorderedIteration).cloned();
        for source in [
            "x = c in {'a', 'b'}\n",
            "x = c not in {'a'}\n",
            "x = c in ({'a'})\n",
            "x = c in set('ab') or c in frozenset(s)\n",
            "x = c in {v for v in 'ab'}\n",
            "x = (c in  # why\n     {'a'})\n",
            "x = c in \\\n    {'a'}\n",
            "x = (c in {'a'}  # why\n)\n",
        ] {
            assert_eq!(risk(source), None, "{source}");
        }
        for source in [
            "s = {'a'}\nx = c in s\n",
            "x = [c for c in {'a', 'b'}]\n",
            "for c in set('ab'):\n    pass\n",
            "x = {'a'} in groups\n",
            "x = c in {'a'} in groups\n",
            "x = c == {'a'}\n",
            "x = c in list({'a'})\n",
        ] {
            assert!(risk(source).is_some(), "{source}");
        }
    }

    #[test]
    fn infers_numbers_from_arithmetic() {
        let src = "def rgb_to_hsv(r, g, b):\n    maxc = max(r, g, b)\n    return maxc - r\n\n\
                   def greet(name):\n    return 'hi ' + name\n";
        let (fns, _) = exports("colorsys.py", src);
        let types: Vec<&str> = fns[0].params.iter().map(|p| p.ts_type.as_str()).collect();
        assert_eq!(types, ["number", "number", "number"]);
        assert_eq!(fns[1].params[0].ts_type, "unknown");
    }

    #[test]
    fn risks_are_found_however_the_module_was_imported() {
        // Each risk written four ways: plain, aliased module, from-import, aliased from-import.
        let cases: &[(&str, Risk)] = &[
            (
                "import re\ndef f(s):\n    return re.sub(r'(?<=a)b', '', s)\n",
                Risk::RegexSyntax,
            ),
            (
                "import re as regex\ndef f(s):\n    return regex.sub(r'(?<=a)b', '', s)\n",
                Risk::RegexSyntax,
            ),
            (
                "from re import sub\ndef f(s):\n    return sub(r'(?<=a)b', '', s)\n",
                Risk::RegexSyntax,
            ),
            (
                "from re import compile as rc\ndef f(s):\n    return rc(r'(a)\\1')\n",
                Risk::RegexSyntax,
            ),
            (
                "import re\ndef f(s, p):\n    return re.compile(f'{p}x')\n",
                Risk::DynamicRegex,
            ),
            (
                "import random\ndef f():\n    return random.random()\n",
                Risk::ClockAndRandom,
            ),
            (
                "import random as r\ndef f():\n    return r.random()\n",
                Risk::ClockAndRandom,
            ),
            (
                "from random import randint\ndef f():\n    return randint(1, 2)\n",
                Risk::ClockAndRandom,
            ),
            (
                "from time import time\ndef f():\n    return time()\n",
                Risk::ClockAndRandom,
            ),
            (
                "from datetime import datetime\ndef f():\n    return datetime.now()\n",
                Risk::ClockAndRandom,
            ),
            (
                "import datetime as dt\ndef f():\n    return dt.datetime.now()\n",
                Risk::ClockAndRandom,
            ),
            (
                "from datetime import datetime\ndef f():\n    return datetime.today()\n",
                Risk::ClockAndRandom,
            ),
            (
                "import time\ndef f():\n    return time.localtime().tm_year\n",
                Risk::ClockAndRandom,
            ),
            (
                "import os\ndef f():\n    return os.getenv('USER')\n",
                Risk::SharedState,
            ),
            (
                "counter = 0\ndef f():\n    global counter\n    counter += 1\n    return counter\n",
                Risk::SharedState,
            ),
        ];
        for (code, risk) in cases {
            let facts = scan(code);
            assert!(facts.risks.contains_key(risk), "{code}: {:?}", facts.risks);
        }
        // `re.escape` and the method on a result are not patterns.
        let facts = scan(
            "import re\ndef f(s, x):\n    return re.sub('a', 'b', s).strip() + re.escape(x)\n",
        );
        assert!(
            !facts.risks.contains_key(&Risk::DynamicRegex),
            "{:?}",
            facts.risks
        );
    }

    #[test]
    fn a_format_string_is_not_typed_as_a_number() {
        let src = "def show(fmt):\n    return fmt % 3\n\n\
                   def rem(n):\n    return 10 % n\n";
        let (fns, _) = exports("m.py", src);
        assert_eq!(fns[0].params[0].ts_type, "unknown");
        assert_eq!(fns[1].params[0].ts_type, "number");
    }

    #[test]
    fn a_parameter_that_may_be_a_string_is_not_typed_as_a_number() {
        // `int(s)` parses a string, `s * n` repeats one: the recording must be free to pass strings.
        let src = "def parse(s):\n    return int(s) + 1\n\n\
                   def rep(s, n):\n    return s * n\n\n\
                   def halve(x):\n    return x * 0.5\n";
        let (fns, _) = exports("m.py", src);
        assert_eq!(fns[0].params[0].ts_type, "unknown");
        assert_eq!(fns[1].params[0].ts_type, "unknown");
        assert_eq!(fns[1].params[1].ts_type, "unknown");
        assert_eq!(fns[2].params[0].ts_type, "number");
    }

    #[test]
    fn maps_bytes_hints_to_bytes_dialect_type() {
        assert_eq!(to_ts_type("bytes"), "pybytes");
        assert_eq!(to_ts_type("bytearray"), "pybytes");
        assert_eq!(to_ts_type("memoryview"), "pybytes");
        assert_eq!(to_ts_type("Optional[bytes]"), "pybytes | undefined");
        assert_eq!(to_ts_type("list[bytes]"), "pybytes[]");
        assert_eq!(to_ts_type("bytes | None"), "pybytes | undefined");
        for path in ["Path", "pathlib.Path", "PurePath", "os.PathLike"] {
            assert_eq!(to_ts_type(path), "pypath", "{path}");
        }
        // A class that names its flavour is not rebuilt as this platform's `Path`.
        for flavour in ["PureWindowsPath", "PosixPath", "pathlib.PurePosixPath"] {
            assert_eq!(to_ts_type(flavour), flavour);
        }
        assert_eq!(to_ts_type("os.PathLike[str]"), "pypath");
        assert_eq!(to_ts_type("Path | None"), "pypath | undefined");
        assert_eq!(to_ts_type("Optional[Path]"), "pypath | undefined");
        assert_eq!(to_ts_type("list[Path]"), "pypath[]");
        // A project's own class that only ends in `Path` is still a named class.
        assert_eq!(to_ts_type("DataPath"), "DataPath");
    }

    #[test]
    fn reads_install_requires_from_setup_cfg() {
        let dir = std::env::temp_dir().join(format!(
            "rust-anything-python-setup-cfg-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("setup.cfg"),
            concat!(
                "[metadata]\n",
                "name = demo\n\n",
                "[options]\n",
                "packages = find:\n",
                "install_requires =\n",
                "    requests>=2.28\n",
                "    # a comment inside the list\n",
                "    ; another comment\n\n",
                "    PyYAML >= 6.0 ; python_version >= \"3.9\"\n",
                "python_requires = >=3.9\n\n",
                "[options.extras_require]\n",
                "dev =\n",
                "    pytest>=7\n",
            ),
        )
        .unwrap();
        let pkg = read_package(&dir, "");
        assert_eq!(pkg.deps, ["requests", "pyyaml"]);

        // Single-line value on the `=` line, and mixed same-line + continuation.
        assert_eq!(
            setup_cfg_requirements("[options]\ninstall_requires = httpx>=0.25\n"),
            ["httpx"]
        );
        assert_eq!(
            setup_cfg_requirements("[options]\ninstall_requires = httpx>=0.25\n    click>=8.0\n"),
            ["httpx", "click"]
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn reads_install_requires_from_setup_py() {
        let dir = std::env::temp_dir().join(format!(
            "rust-anything-python-setup-py-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("setup.py"),
            concat!(
                "from setuptools import setup\n",
                "setup(\n",
                "    name=\"demo\",\n",
                "    install_requires=[\n",
                "        \"requests>=2\",\n",
                "        # comment inside list\n",
                "        'click>=8.0',\n",
                "        f\"{pkg}\",\n",
                "    ],\n",
                "    extras_require={\"dev\": [\"pytest\"]},\n",
                ")\n",
            ),
        )
        .unwrap();
        let pkg = read_package(&dir, "");
        let _ = std::fs::remove_dir_all(&dir);
        assert_eq!(pkg.deps, ["requests", "click"]);
        assert_eq!(
            setup_py_requirements(
                "import setuptools\nsetuptools.setup(install_requires=(\"httpx==0.27\", 'pyyaml'))\n"
            ),
            ["httpx", "pyyaml"]
        );
    }
}