|
| 1 | +.. vim: set fileencoding=utf-8: |
| 2 | +.. -*- coding: utf-8 -*- |
| 3 | +.. +--------------------------------------------------------------------------+ |
| 4 | + | | |
| 5 | + | Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 | + | you may not use this file except in compliance with the License. | |
| 7 | + | You may obtain a copy of the License at | |
| 8 | + | | |
| 9 | + | http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | + | | |
| 11 | + | Unless required by applicable law or agreed to in writing, software | |
| 12 | + | distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | + | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | + | See the License for the specific language governing permissions and | |
| 15 | + | limitations under the License. | |
| 16 | + | | |
| 17 | + +--------------------------------------------------------------------------+ |
| 18 | +
|
| 19 | +
|
| 20 | +******************************************************************************* |
| 21 | +002. Centralized Import Management via __ Subpackage Pattern |
| 22 | +******************************************************************************* |
| 23 | + |
| 24 | +Status |
| 25 | +=============================================================================== |
| 26 | + |
| 27 | +Accepted |
| 28 | + |
| 29 | +Context |
| 30 | +=============================================================================== |
| 31 | + |
| 32 | +Python projects generated from this template need consistent import management across modules to address several challenges: |
| 33 | + |
| 34 | +**Namespace Pollution**: Individual modules importing common dependencies directly leads to namespace pollution and makes module interfaces unclear. |
| 35 | + |
| 36 | +**Import Duplication**: Common imports (collections.abc, immutable containers, type hints) are repeated across many modules, creating maintenance overhead. |
| 37 | + |
| 38 | +**Performance Considerations**: Repeated import costs across modules can impact package initialization time. |
| 39 | + |
| 40 | +**Maintenance Overhead**: Changes to common dependencies require updates across multiple modules. |
| 41 | + |
| 42 | +**Code Readability**: Module code becomes cluttered with import statements rather than focusing on core functionality. |
| 43 | + |
| 44 | +The template system must provide a scalable solution that works consistently across: |
| 45 | +- Single-package projects |
| 46 | +- Multi-package projects with subpackages |
| 47 | +- Projects with varying complexity levels |
| 48 | +- Projects using different optional features (CLI, Rust extensions, etc.) |
| 49 | + |
| 50 | +Decision |
| 51 | +=============================================================================== |
| 52 | + |
| 53 | +Implement centralized import management using the double underscore (``__``) subpackage pattern: |
| 54 | + |
| 55 | +**Core Implementation**: |
| 56 | +- Every Python package includes a ``__`` subdirectory |
| 57 | +- Contains ``__init__.py`` (re-exports), ``imports.py`` (raw imports), ``nomina.py`` (naming constants) |
| 58 | +- All modules use identical ``from . import __`` import pattern |
| 59 | + |
| 60 | +**Hierarchical Extension**: |
| 61 | +- Subpackages implement cascading imports with ``from ..__ import *`` |
| 62 | +- Each level can add specialized imports without duplicating parent imports |
| 63 | +- Maintains consistent interface regardless of package depth |
| 64 | + |
| 65 | +**Template Integration**: |
| 66 | +- Template system generates appropriate ``__`` structure based on project features |
| 67 | +- Optional components (CLI, Rust, etc.) extend the import hierarchy as needed |
| 68 | +- Standard imports are pre-configured for immediate development productivity |
| 69 | + |
| 70 | +Alternatives |
| 71 | +=============================================================================== |
| 72 | + |
| 73 | +**Direct Module Imports** |
| 74 | +- Each module imports dependencies directly |
| 75 | +- Rejected due to namespace pollution and maintenance overhead |
| 76 | +- Would require updating every module when common dependencies change |
| 77 | +- Creates inconsistent import patterns across project modules |
| 78 | + |
| 79 | +**Package-Level __init__.py Imports** |
| 80 | +- Central imports at package ``__init__.py`` level |
| 81 | +- Rejected because it pollutes the main package namespace |
| 82 | +- Makes it unclear which imports are part of the public API vs internal dependencies |
| 83 | +- Doesn't scale well to multi-package projects |
| 84 | + |
| 85 | +**Import Utility Module** |
| 86 | +- Single ``imports.py`` or ``utils.py`` module with all dependencies |
| 87 | +- Rejected because it creates a monolithic import module that becomes unwieldy |
| 88 | +- Doesn't provide clear organization for different types of imports |
| 89 | +- No natural extension pattern for subpackages |
| 90 | + |
| 91 | +**Per-Module Import Files** |
| 92 | +- Each module has an associated ``_imports.py`` file |
| 93 | +- Rejected due to file proliferation and maintenance complexity |
| 94 | +- Creates 2x file count for every functional module |
| 95 | +- No clear inheritance pattern for shared imports |
| 96 | + |
| 97 | +Consequences |
| 98 | +=============================================================================== |
| 99 | + |
| 100 | +**Positive Consequences** |
| 101 | + |
| 102 | +**Consistent Interface**: All modules use identical ``from . import __`` regardless of package depth or complexity, reducing cognitive load. |
| 103 | + |
| 104 | +**Namespace Clarity**: Module namespaces contain only module-specific functionality, making interfaces clearer and more maintainable. |
| 105 | + |
| 106 | +**Maintenance Efficiency**: Common import changes propagate automatically to all modules without requiring individual module updates. |
| 107 | + |
| 108 | +**Performance Optimization**: Import costs are centralized to package initialization, enabling strategic optimization for performance-critical paths. |
| 109 | + |
| 110 | +**Scalability**: Pattern extends naturally from simple single-package projects to complex multi-package hierarchies without changing the interface. |
| 111 | + |
| 112 | +**Template Flexibility**: Template system can generate appropriate import structures based on selected features without affecting module code patterns. |
| 113 | + |
| 114 | +**Negative Consequences** |
| 115 | + |
| 116 | +**Learning Curve**: Developers unfamiliar with the pattern require education about the import hierarchy and naming conventions. |
| 117 | + |
| 118 | +**Initial Setup Complexity**: Each package requires proper ``__`` subpackage setup, though this is handled by the template system. |
| 119 | + |
| 120 | +**Import Resolution Indirection**: Imports go through an additional layer of indirection, though this is minimal performance impact. |
| 121 | + |
| 122 | +**Debugging Complexity**: Import-related debugging requires understanding the cascading import hierarchy. |
| 123 | + |
| 124 | +**Neutral Consequences** |
| 125 | + |
| 126 | +**File Structure Overhead**: Each package contains additional ``__`` subdirectory structure, but this is consistent and predictable. |
| 127 | + |
| 128 | +**IDE Integration**: Modern IDEs handle the import pattern well, providing proper autocompletion and navigation through the import hierarchy. |
| 129 | + |
| 130 | +**Convention Enforcement**: Pattern requires discipline to maintain, but template system and documentation provide clear guidance. |
0 commit comments