{"record":{"id":"1f33f5c28e98256c","repo":"astral-sh/ruff","slug":"can-t-use-built-in-builtin-constructor","errorCode":null,"errorMessage":"Can't use built-in `{builtin}` constructor","messagePattern":"Can't use built-in `(.+?)` constructor","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs","lineNumber":294,"sourceCode":"                // ```\n                try_construct_call(name, iter, func, Name::new_static(\"set\"), checker)\n            }\n        }\n    }\n}\n\n/// Try constructing the call to `itertools.starmap` and wrapping it with the given builtin.\nfn try_construct_call(\n    name: Name,\n    iter: &Expr,\n    func: &Expr,\n    builtin: Name,\n    checker: &Checker,\n) -> Result<String> {\n    // We can only do our fix if `builtin` identifier is still bound to\n    // the built-in type.\n    if !checker.semantic().has_builtin_binding(&builtin) {\n        bail!(\"Can't use built-in `{builtin}` constructor\")\n    }\n\n    // In general, we replace:\n    // ```python\n    // foo(...) for ... in iter\n    // ```\n    //\n    // with:\n    // ```python\n    // builtin(itertools.starmap(foo, iter))\n    // ```\n    // where `builtin` is a constructor for a target collection.\n    let call = construct_starmap_call(name, iter, func);\n    let wrapped = wrap_with_call_to(call, builtin);\n    Ok(checker.generator().expr(&wrapped.into()))\n}\n\n/// Construct the call to `itertools.starmap` for suggestion.","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/astral-sh/ruff/blob/15f3fe6b15a5f00172f34b0f542f8ea277f5a586/crates/ruff_linter/src/rules/refurb/rules/reimplemented_starmap.rs#L276-L312","documentation":"The `reimplemented-starmap` (REF414) fix builds a replacement call using the `builtin` identifier (e.g. `zip`) as the constructor of the fixed expression. Before doing so it verifies the name is still bound to the actual built-in; if shadowed, it bails and reports this error internally, so no fix is offered. This is a guard against producing incorrect fixes when user code redefines the name.","triggerScenarios":"Applying the automatic fix for REF414 (`starmap` reimplemented via comprehension/lambda) when the builtin name (e.g. `zip`, `map`) is locally rebound: a local variable, function, import, or class parameter shadows it, or `del` removed the binding.","commonSituations":"User code defines a function or variable named `map`/`zip`/`sum` in the same scope; an `from foo import zip` import shadows the builtin; the diagnostic fires but the fixer refuses to autofix.","solutions":["Apply the fix manually instead of relying on autofix: rewrite the comprehension/lambda to call the built-in `starmap` directly with an explicit qualified builtin such as `import builtins` if needed","Rename the local shadowing the builtin (e.g. `map_` instead of `map`) so `zip`/`map` resolves to the builtin again, then rerun ruff","Use `--unsafe-fixes` scenarios carefully: confirm the binding with `checker.semantic().has_builtin_binding` mentally before hand-editing"],"exampleFix":"# before\nresult = [(f(*args)) for args in pairs]  # `zip` shadowed by local function\n# after (manual rewrite, since autofix is skipped)\nfrom itertools import starmap\nresult = list(starmap(f, pairs))","handlingStrategy":"validation","validationCode":"import builtins\n# check the builtin is not shadowed in the module scope before relying on autofix\ndef builtin_not_shadowed(name):\n    import ast, sys\n    src = open('module.py').read()\n    tree = ast.parse(src)\n    for node in ast.walk(tree):\n        if isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Assign)):\n            targets = getattr(node, 'targets', [getattr(node, 'name', None)])\n            if any(getattr(t, 'id', None) == name for t in targets if t):\n                return False\n    return True\n\nprint(builtin_not_shadowed('zip'))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid naming variables/functions after builtins like map, zip, sum","Run ruff's A001/A002 (builtins shadowing) rules to catch shadowing early","Treat missing autofix on REF414 as a hint that a builtin name is rebound in scope"],"tags":["ruff","refurb","autofix","shadowing"],"backgroundTag":"builtin-name-shadowed","analyzedSha":"15f3fe6b15a5f00172f34b0f542f8ea277f5a586","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}