SKY-Q804 · Quality, structure and performance
Stable module depends on an unstable one
A module's instability (I) is the share of its import links that point outward: the modules it imports, divided by those plus the modules that import it. SKY-Q804 reports a stable module (I below 0.3, many importers) that imports an unstable one (I above 0.7, which itself imports a lot). The catalog calls this a dependency inversion violation.
- Languages
- Python, TypeScript, JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans). A measurement, not listed by default. It is listed, and counted toward
max_quality, with--all-adviceor when the rule is named in[tool.skylos.gate] rules. - Finding type
- advice
- Catalog name
- Dependency inversion violation
Why it matters
Everything that imports the stable module now also depends, through it, on a module that changes whenever any of its own dependencies change. A tweak to a theme or a locale table can then break the data models that half the project imports.
What Skylos flags
"""Shop data records.""" from shop.formatting import money class Order: def __init__(self, order_id, total): self.order_id = order_id self.total = total def label(self): return f"Order {self.order_id}: {money(self.total)}""""Display helpers.""" from shop.locale import currency_symbolfrom shop.settings import DECIMALSfrom shop.themes import highlight def money(amount): return highlight(f"{currency_symbol()}{amount:.{DECIMALS}f}")"""Locale data.""" def currency_symbol(): return "$""""Display settings.""" DECIMALS = 2"""Text themes.""" def highlight(text): return f"*{text}*""""Web views.""" from shop.models import Order def order_page(order_id): return Order(order_id, 10).label()"""Background jobs.""" from shop.models import Order def nightly(ids): return [Order(i, 0).label() for i in ids]"""JSON API.""" from shop.models import Order def order_json(order_id): return {"label": Order(order_id, 10).label()}"""The shop package."""What Skylos leaves alone
"""Display helpers.""" from shop.locale import currency_symbolfrom shop.settings import DECIMALS def money(amount): return f"{currency_symbol()}{amount:.{DECIMALS}f}"Why Skylos leaves it alone
Without the theme import, formatting imports two modules and is imported by one, so its instability is 2/3. That is not above 0.7, so models no longer depends on an unstable module.
How Skylos decides
- Runs with
--quality, on the import graph of the project's Python modules (top-level imports only) and TypeScript and JavaScript modules. - For each module computes instability: the project modules it imports, divided by those plus the project modules that import it.
- For every module with instability below 0.3, checks each module it imports. It reports the pair when the imported module's instability is above 0.7, unless the imported module is one of its own submodules or the importing module is only a re-export facade (an
__init__that imports names to publish them). - Reports on line 1 of the stable module, HIGH when its instability is below 0.1 and MEDIUM otherwise. The limits are fixed.
How to fix it
Let the stable module depend on something stable: pass the formatter in, depend on a small interface, or move the formatting call out of the model into the views that need it.
"""Shop data records.""" Removed: from shop.formatting import money Removed: class Order: def __init__(self, order_id, total): self.order_id = order_id self.total = total Removed: def label(self):Removed: return f"Order {self.order_id}: {money(self.total)}"Added: def label(self, format_money):Added: return f"Order {self.order_id}: {format_money(self.total)}"When it can be wrong
- Instability alone does not prove a problem: a stable module that calls a busy but well-tested helper is reported although nothing is wrong.
- Only imports within the scanned project count, and only top-level imports in Python.
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
An inline marker does not work for this rule: with this comment on line 1 of the stable module, the reported line, it is still reported.
Python """Shop data records.""" # skylos: ignore[SKY-Q804] formatting is frozen[tool.skylos] ignore = ["SKY-Q804"]turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.