Decompose god module into domain-specific modules#246
Open
Wolfvin wants to merge 2 commits into
Open
Conversation
This refactors the 4003-line inflect/__init__.py god module into 8 domain-specific modules while preserving the exact same public API. ## What was refactored The single 4003-line __init__.py contained 73 functions, 11 classes, and ~1800 lines of module-level data tables mixing 7 distinct domains. This made the module difficult to navigate, understand, and maintain. ## Decomposition | Module | Lines | Domain | |--------|-------|--------| | utils.py | 172 | Exception classes, utility functions, Words/Word types | | plurals.py | 2265 | Noun plural/singular data + _plnoun, _sinoun | | verbs.py | 226 | Verb pluralization data + _pl_special_verb | | adjectives.py | 72 | Adjective data + _pl_special_adjective | | articles.py | 117 | Indefinite article regex/data + _indef_article | | numbers.py | 429 | Number-to-words/ordinals data + ordinal, number_to_words | | comparisons.py | 106 | Comparison logic + _plequal | | template.py | 625 | Template engine + inflect, postprocess, get_count | The __init__.py was reduced from 4003 to 712 lines — it now contains only the engine class with thin wrapper methods that delegate to domain modules, plus re-exports of all public names for backward compatibility. ## Verification All 4 verification methods confirm the refactor is behavior-preserving: 1. Regrets cluster validation: 13/13 GREEN 2. Raw output vs KEBENARAN 1 (pre-refactor truth): IDENTICAL 3. Fingerprint vs KEBENARAN 2 (pre-refactor fingerprint): ALL MATCH 4. Chain hash validation: 3/3 MATCH All 207 existing tests pass (16 xfailed, same as before). ## Design decisions - Engine-as-parameter pattern: Domain functions that need engine state receive the engine instance as their first parameter - Full re-exports: All public names accessible from top-level inflect - Data co-located with logic: Each module contains its data tables and the functions that operate on them
The entire inflect library lived in a single __init__.py file (4,003 lines) containing ~2,000 lines of data tables and a single engine class with 66 methods. This made the codebase difficult to navigate, understand, and maintain. Changes: - Split data tables into inflect/data/ (plurals, singulars, pronouns, articles, numbers) - Split methods into inflect/methods/ (pluralize, singularize, articles, compare, ordinals, number_words, participles, inflection, user_defined) - Engine class now uses mixin pattern, inheriting from method mixins - Shared helpers and exceptions moved to inflect/_shared.py - __init__.py reduced to thin re-export layer (~83 lines) - Zero behavioral changes — all 207 existing tests pass - All public API preserved (engine class, exceptions, helper functions) Verification: - 8 Regrets clusters: ALL GREEN (identical fingerprints) - 3 Regrets chains: ALL MATCH (identical chain hashes) - Drift detection: ALL STABLE (5 runs, zero drift) - 207 pytest tests: ALL PASS (16 xfailed as before)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The entire
inflectlibrary lived in a single__init__.pyfile (4,003 lines) containing ~2,000 lines of data tables and a singleengineclass with 66 methods. This PR decomposes it into domain-specific packages for maintainability.What Changed
Before: 1 file (4,003 lines) containing everything.
After: 19 files across 4 packages, each with a single responsibility.
New structure:
inflect/__init__.py- thin re-export layer (~83 lines)inflect/_shared.py- helpers, types, exceptionsinflect/engine.py- engine class using mixinsinflect/data/- plurals, singulars, pronouns, articles, numbersinflect/methods/- pluralize, singularize, articles, compare, ordinals, number_words, participles, inflection, user_definedVerification
Regrets Cluster Fingerprints (8 clusters, ALL MATCH)
Regrets Chain Hashes (3 chains, ALL MATCH)
Test Suite
207 passed, 16 xfailed (identical to before refactoring).
Drift Detection
5 runs per cluster, all STABLE with zero drift.
Design