SKY-Q702 · Quality, structure and performance
Low cohesion
A cohesive class is one whose methods work on the same data. SKY-Q702 splits a Python class's methods into groups that share instance attributes or call each other (a measure called LCOM4) and reports a class with more than 2 groups that have nothing in common.
- Languages
- Python
- 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
Why it matters
Unconnected groups are separate jobs sharing one name. Every user of the class depends on all of them, a change to the mail settings means retesting database code, and the class keeps growing because it is the obvious place for "one more helper".
What Skylos flags
"""Shop helpers that grew into one class.""" import smtplibimport sqlite3 FIND_ORDER = "SELECT * FROM orders WHERE id = ?" class ShopToolkit: def open_database(self, path): self.connection = sqlite3.connect(path) def find_order(self, order_id): return self.connection.execute(FIND_ORDER, (order_id,)).fetchone() def configure_mail(self, host, sender): self.smtp_host = host self.sender = sender def send_receipt(self, to, text): with smtplib.SMTP(self.smtp_host) as smtp: smtp.sendmail(self.sender, [to], text) def reset_counters(self): self.page_views = 0 def count_page_view(self): self.page_views += 1 return self.page_viewsWhat Skylos leaves alone
"""Shop helpers that grew into one class.""" import smtplibimport sqlite3 FIND_ORDER = "SELECT * FROM orders WHERE id = ?" class ShopToolkit: def open_database(self, path): self.connection = sqlite3.connect(path) def find_order(self, order_id): return self.connection.execute(FIND_ORDER, (order_id,)).fetchone() def configure_mail(self, host, sender): self.smtp_host = host self.sender = sender def send_receipt(self, to, text): with smtplib.SMTP(self.smtp_host) as smtp: smtp.sendmail(self.sender, [to], text)Why Skylos leaves it alone
Two unconnected groups remain, database and mail. That is the limit; Skylos reports a class only from three groups.
How Skylos decides
- Runs with
--quality, on Python classes outside test code, examples, benchmarks, a top-leveldocsfolder and the rootsetup.py.Protocolclasses and classes whose methods are all abstract are skipped. - Builds a graph of the class's methods: two methods are connected when they use the same instance attribute or one calls the other through
self.__init__counts like any method, so one that sets every attribute joins all the groups. Methods that use no instance state are left out. - Reports when the number of separate groups is above 2 and at least two of them use attributes the class sets itself. Dataclasses, attrs classes, pydantic models,
TypedDictandNamedTupleneed more than 6 groups. - Reports on the
classline, MEDIUM for 3 or 4 groups and HIGH above 4, and lists up to four groups with their methods. The limits are fixed.
How to fix it
Give each group its own class, named after its job, and let callers use only the one they need.
Removed: """Shop helpers that grew into one class."""Added: """Order lookups, receipts and page statistics.""" import smtplibimport sqlite3 FIND_ORDER = "SELECT * FROM orders WHERE id = ?" Removed: class ShopToolkit:Removed: def open_database(self, path):Added: class OrderStore:Added: def __init__(self, path): self.connection = sqlite3.connect(path) def find_order(self, order_id): return self.connection.execute(FIND_ORDER, (order_id,)).fetchone() Removed: def configure_mail(self, host, sender):Added: Added: class ReceiptMailer:Added: def __init__(self, host, sender): self.smtp_host = host self.sender = sender def send_receipt(self, to, text): with smtplib.SMTP(self.smtp_host) as smtp: smtp.sendmail(self.sender, [to], text) Removed: def reset_counters(self):Added: Added: class PageCounter:Added: def __init__(self): self.page_views = 0 def count_page_view(self): self.page_views += 1 return self.page_viewsWhen it can be wrong
- A constructor that assigns every attribute connects all methods, so a class with unrelated jobs set up in
__init__is not reported. - A facade that deliberately groups unrelated operations behind one object, such as an SDK client, is reported.
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
On the
classline (the reported line):Python class ShopToolkit: # skylos: ignore[SKY-Q702] split tracked in SHOP-142[tool.skylos] ignore = ["SKY-Q702"]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.