Skip to main content

SKY-Q804 · Quality, structure and performance

Stable module depends on an unstable one

Varies

The catalog gives no severity. HIGH when the stable module's instability is below 0.1, MEDIUM otherwise.

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-advice or when the rule is named in [tool.skylos.gate] rules.
Finding type
advice
Catalog name
Dependency inversion violation
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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/models.py · PythonReported on line 1
"""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)}"
shop/formatting.py · Python
"""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}")
shop/locale.py · Python
"""Locale data."""  def currency_symbol():    return "$"
shop/settings.py · Python
"""Display settings.""" DECIMALS = 2
shop/themes.py · Python
"""Text themes."""  def highlight(text):    return f"*{text}*"
shop/views.py · Python
"""Web views.""" from shop.models import Order  def order_page(order_id):    return Order(order_id, 10).label()
shop/jobs.py · Python
"""Background jobs.""" from shop.models import Order  def nightly(ids):    return [Order(i, 0).label() for i in ids]
shop/api.py · Python
"""JSON API.""" from shop.models import Order  def order_json(order_id):    return {"label": Order(order_id, 10).label()}
shop/__init__.py · Python
"""The shop package."""

What Skylos leaves alone

shop/models.py: same as in the flagged example.

shop/formatting.py · Python
"""Display helpers.""" from shop.locale import currency_symbolfrom shop.settings import DECIMALS  def money(amount):    return f"{currency_symbol()}{amount:.{DECIMALS}f}"

shop/locale.py: same as in the flagged example.

shop/settings.py: same as in the flagged example.

shop/views.py: same as in the flagged example.

shop/jobs.py: same as in the flagged example.

shop/api.py: same as in the flagged example.

shop/__init__.py: same as in the flagged example.

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

  1. Runs with --quality, on the import graph of the project's Python modules (top-level imports only) and TypeScript and JavaScript modules.
  2. For each module computes instability: the project modules it imports, divided by those plus the project modules that import it.
  3. 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).
  4. 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/models.py · Python · the fix, against the flagged file
"""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.

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