Skip to content

PY-CLAS0002 · coupled_nested_type_candidate

Find short tightly named classes that may form one nested namespace.

This is a deterministic rule for python. Read its implementation.

Find top-level classes whose names share a prefix and end in configured role suffixes. Require at least minimum_types, require every class to span no more than maximum_type_lines, and require at least minimum_coimports other modules to import two or more of the classes from the same defining module. The default recognizes pairs such as MessageContent and MessageKind. The value is the number of qualifying groups.

Findings identify the definitions and every qualifying co-import site. The proposed namespace is the shared prefix, producing access such as Message.Content and Message.Kind. The value is the number of qualifying groups rather than the number of classes in them.

Nested classes do not capture an outer instance and change __qualname__, import paths, pickling identity, framework discovery, and public APIs. Keep top-level classes when either type is independently useful, subclassed externally, registered by qualified name, or easier to test separately. A small module namespace can be clearer than a namespace-only class. This is an opt-in candidate rule and has no automatic fix. minimum_prefix_length keeps a one or two character shared prefix from grouping unrelated classes, since a namespace named after two letters explains nothing.

Two twelve-line classes named EventContent and EventKind that are imported together by three modules are reported as an Event namespace candidate. A large EventContent, a Kind used alone, or a pair imported together only once is not reported.

  • Cites “The Python Tutorial”, class namespaces and scopes. Open reference
  • Cites “Google Python Style Guide”, nested classes and functions. Open reference