Skip to main content

SKY-L036 · Logic and resilience

Import repeated inside function

Low

A function imports a module that the file already imports at the top, under the same name. The inner import binds the very same module object again, so it changes nothing.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans). Only when listed. This is advice outside the default set: the report keeps it in JSON but does not list it, and the gate does not count it, unless you pass --all-advice or select the rule (--select SKY-L036).
Finding type
advice
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

It is clutter that makes readers wonder whether the function needs a different module or a lazy import for a reason. Repeated imports are a common trace of code pasted in from another file or generated piece by piece.

What Skylos flags

orders/export.py · PythonReported on line 7
"""Export orders as JSON.""" import json  def export_order(order):    import json     return json.dumps(order.to_dict(), sort_keys=True)

What Skylos leaves alone

orders/export.py · Python
"""Export orders as JSON and PDF.""" import json  def export_order(order):    return json.dumps(order.to_dict(), sort_keys=True)  def export_order_pdf(order):    import weasyprint     return weasyprint.HTML(string=order.to_html()).write_pdf()

Why Skylos leaves it alone

weasyprint is not imported at the top of the file. Importing a heavy, optional library inside the one function that needs it is a deliberate lazy import, not a repeat.

How Skylos decides

  1. Runs with --quality on Python files. Test and sample code is not checked, nor is a file that reads function source with inspect.getsource().
  2. Reports an import inside a function when an earlier, unconditional module-level import m binds the same module to the same name. A from pkg import sub counts only when sub is a module file of a project package.
  3. from m import name of a function, class or constant is not reported, because tests often patch m.name and a later import reads the patched value.
  4. Skips module imports under if TYPE_CHECKING, try/except ImportError or other blocks, names bound twice, declared global, used with globals() or exec, rebound in the function or an enclosing one, and modules the file writes to or reloads.
  5. Reports LOW on the line of the inner import.

How to fix it

Delete the inner import; the module-level one already covers the function.

orders/export.py · Python · the fix, against the flagged file
"""Export orders as JSON.""" import json  def export_order(order):Removed:     import jsonRemoved:      return json.dumps(order.to_dict(), sort_keys=True)

When it can be wrong

  • A function that is shipped elsewhere as source (for example, sent to a remote worker with a serializer) needs its own imports. Skylos only sees that when the file calls inspect.getsource().

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of the inner import.

    Python
    import json  # skylos: ignore[SKY-L036] function is shipped as source
  • [tool.skylos] ignore = ["SKY-L036"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.