SKY-L036 · Logic and resilience
Import repeated inside function
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-adviceor select the rule (--select SKY-L036). - Finding type
- advice
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
"""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
"""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
- Runs with
--qualityon Python files. Test and sample code is not checked, nor is a file that reads function source withinspect.getsource(). - Reports an import inside a function when an earlier, unconditional module-level
import mbinds the same module to the same name. Afrom pkg import subcounts only whensubis a module file of a project package. from m import nameof a function, class or constant is not reported, because tests often patchm.nameand a later import reads the patched value.- Skips module imports under
if TYPE_CHECKING,try/except ImportErroror other blocks, names bound twice, declaredglobal, used withglobals()orexec, rebound in the function or an enclosing one, and modules the file writes to or reloads. - 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.
"""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.