Skip to content

Commit 8496596

Browse files
Python: Replace wildcard imports with explicit imports
- Replace all 'from ... import *' with explicit symbol imports - Add __all__ declarations to namespace packages for re-exports - Update CODING_STANDARD.md to prohibit wildcard imports - Maintain exported API and preserve all functionality fixes #3605
1 parent 77e90e6 commit 8496596

6 files changed

Lines changed: 478 additions & 23 deletions

File tree

‎python/CODING_STANDARD.md‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,37 @@ If in doubt, use the link above to read much more considerations of what to do a
403403

404404
### Explicit Exports
405405

406-
> **Note:** This convention is being adopted. See [#3605](https://github.com/microsoft/agent-framework/issues/3605) for progress.
406+
**All wildcard imports (`from ... import *`) are prohibited** in production code, including both `.py` and `.pyi` files. Always use explicit import lists to maintain clarity and avoid namespace pollution.
407407

408-
Define `__all__` in each module to explicitly declare the public API. Avoid using `from module import *` in `__init__.py` files as it can impact performance and makes the public API unclear:
408+
Define `__all__` in each module to explicitly declare the public API, then import specific symbols by name:
409409

410410
```python
411-
# ✅ Preferred - explicit __all__ and imports
411+
# ✅ Preferred - explicit __all__ and named imports
412412
__all__ = ["Agent", "Message", "ChatResponse"]
413413

414414
from ._agents import Agent
415415
from ._types import Message, ChatResponse
416416

417-
# ❌ Avoid - star imports
417+
# ✅ For many exports, use parenthesized multi-line imports
418+
from ._types import (
419+
AgentResponse,
420+
ChatResponse,
421+
Message,
422+
ResponseStream,
423+
)
424+
425+
# ❌ Prohibited - wildcard/star imports
418426
from ._agents import *
419427
from ._types import *
420428
```
421429

430+
**Rationale:**
431+
- **Clarity**: Explicit imports make it clear exactly what is being exported and used
432+
- **IDE Support**: Enables better autocomplete, go-to-definition, and refactoring
433+
- **Type Checking**: Improves static analysis and type checker accuracy
434+
- **Maintenance**: Makes it easier to track symbol usage and detect breaking changes
435+
- **Performance**: Avoids unnecessary symbol resolution during module import
436+
422437
## Performance considerations
423438

424439
### Cache Expensive Computations

0 commit comments

Comments
 (0)