You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+143-2Lines changed: 143 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -158,12 +158,13 @@ Type annotations are REQUIRED for all code in this project. This is strictly enf
158
158
### Import Style
159
159
160
160
-**ALWAYS** use absolute imports, never relative imports
161
-
- Place imports at module level, not inside functions
161
+
- Place imports at module level, not inside functions (exception: it is unavoidable for performance reasons)
162
162
- Import sorting is handled by `ruff`'s `isort` - imports should be grouped and sorted:
163
163
1. Standard library imports
164
-
2. Third-party imports
164
+
2. Third-party imports (use `lazy_heavy_imports` for heavy libraries)
165
165
3. First-party imports (`data_designer`)
166
166
- Use standard import conventions (enforced by `ICN`)
167
+
- See [Lazy Loading and TYPE_CHECKING](#lazy-loading-and-type_checking) section for optimization guidelines
167
168
168
169
```python
169
170
# Good
@@ -184,6 +185,146 @@ Type annotations are REQUIRED for all code in this project. This is strictly enf
184
185
path = Path(filename)
185
186
```
186
187
188
+
### Lazy Loading and TYPE_CHECKING
189
+
190
+
This project uses lazy loading for heavy third-party dependencies to optimize import performance.
191
+
192
+
#### When to Use Lazy Loading
193
+
194
+
**Heavy third-party libraries** (>100ms import cost) should be lazy-loaded via `lazy_heavy_imports.py`:
195
+
196
+
```python
197
+
# ❌ Don't import directly
198
+
import pandas as pd
199
+
import numpy as np
200
+
201
+
# ✅ Use lazy loading with IDE support
202
+
from typing importTYPE_CHECKING
203
+
from data_designer.lazy_heavy_imports import pd, np
204
+
205
+
ifTYPE_CHECKING:
206
+
import pandas as pd # For IDE autocomplete and type hints
207
+
import numpy as np
208
+
```
209
+
210
+
This pattern provides:
211
+
- Runtime lazy loading (fast startup)
212
+
- Full IDE support (autocomplete, type hints)
213
+
- Type checker validation
214
+
215
+
**See [lazy_heavy_imports.py](src/data_designer/lazy_heavy_imports.py) for the current list of lazy-loaded libraries.**
216
+
217
+
#### Adding New Heavy Dependencies
218
+
219
+
If you add a new dependency with significant import cost (>100ms):
220
+
221
+
1.**Add to `lazy_heavy_imports.py`:**
222
+
```python
223
+
_LAZY_IMPORTS= {
224
+
# ... existing entries ...
225
+
"your_lib": "your_library_name",
226
+
}
227
+
```
228
+
229
+
2.**Update imports across codebase:**
230
+
```python
231
+
from typing importTYPE_CHECKING
232
+
from data_designer.lazy_heavy_imports import your_lib
233
+
234
+
ifTYPE_CHECKING:
235
+
import your_library_name as your_lib # For IDE support
236
+
```
237
+
238
+
3.**Verify with performance test:**
239
+
```bash
240
+
make perf-import CLEAN=1
241
+
```
242
+
243
+
#### Using TYPE_CHECKING Blocks
244
+
245
+
`TYPE_CHECKING` blocks defer imports that are only needed for type hints, preventing circular dependencies and reducing import time.
246
+
247
+
**For internal data_designer imports:**
248
+
249
+
```python
250
+
from__future__import annotations # Always include at top
251
+
252
+
from typing importTYPE_CHECKING
253
+
254
+
# Runtime imports
255
+
from pathlib import Path
256
+
from data_designer.config.base import ConfigBase
257
+
258
+
ifTYPE_CHECKING:
259
+
# Type-only imports - only visible to type checkers
260
+
from data_designer.engine.models.facade import ModelFacade
261
+
262
+
defget_model(model: ModelFacade) -> str:
263
+
return model.name
264
+
```
265
+
266
+
**For lazy-loaded libraries (see pattern in "When to Use Lazy Loading" above):**
267
+
- Import from `lazy_heavy_imports` for runtime
268
+
- Add full import in `TYPE_CHECKING` block for IDE support
269
+
270
+
**Rules for TYPE_CHECKING:**
271
+
272
+
✅ **DO put in TYPE_CHECKING:**
273
+
- Internal `data_designer` imports used **only** in type hints
274
+
- Imports that would cause circular dependencies
275
+
-**Full imports of lazy-loaded libraries for IDE support** (e.g., `import pandas as pd` in addition to runtime `from data_designer.lazy_heavy_imports import pd`)
0 commit comments