-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-try-import.mdc
More file actions
97 lines (75 loc) · 2.89 KB
/
Copy pathno-try-import.mdc
File metadata and controls
97 lines (75 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
description: Do not wrap imports in try-except - let import errors fail loudly
alwaysApply: true
---
# No Try-Except for Imports
Import statements should not be wrapped in try-except blocks. If an import fails, it should raise an error immediately rather than being silently caught.
## Rules
- **No try-except around imports**: Import statements must be at the top level, not inside try-except blocks
- **Fail fast**: If a required dependency is missing, the error should be raised immediately
- **Optional imports**: If a module is truly optional, use conditional imports at module level with clear error messages, not try-except
## Examples
```python
# ❌ BAD - Hiding import errors
try:
from langchain_core.tools import tool
except ImportError:
tool = None # Silently fails, hides the problem
def my_function():
if tool is None:
# Error only appears when function is called
raise ImportError("langchain_core is required")
return tool(...)
# ✅ GOOD - Let import errors fail immediately
from langchain_core.tools import tool
def my_function():
return tool(...)
```
```python
# ❌ BAD - Catching import errors
try:
import pandas as pd
except ImportError:
pd = None # Hides missing dependency
# ✅ GOOD - Import at top, fail if missing
import pandas as pd
```
## Optional Dependencies
If a dependency is truly optional (e.g., for optional features), document it clearly and let the import fail:
```python
# ✅ GOOD - Optional dependency with clear documentation
# Note: langchain_core is optional, only needed for create_sandbox_tool
# Install with: pip install langchain-core
from langchain_core.tools import tool as langchain_tool
def create_tool():
"""Create tool. Requires langchain-core to be installed."""
return langchain_tool(...)
```
Or use a clear check at module level:
```python
# ✅ ACCEPTABLE - Only if truly optional with clear error
try:
from optional_module import feature
except ImportError:
feature = None
# Document why it's optional
# This should be rare and well-documented
def use_feature():
if feature is None:
raise ImportError(
"optional_module is required for this feature. "
"Install with: pip install optional-module"
)
return feature()
```
## Rationale
- **Fail fast**: Import errors should be caught at import time, not at runtime
- **Clear errors**: Users should know immediately if dependencies are missing
- **No silent failures**: Hiding import errors makes debugging harder
- **Dependency clarity**: Missing dependencies should be obvious, not hidden
## Exception
The only acceptable exception is when:
1. The dependency is truly optional (not required for core functionality)
2. The try-except is at module level (not inside functions)
3. There is clear documentation explaining why it's optional
4. A clear error message is provided when the optional feature is used