Skip to content

Commit cc4afb3

Browse files
docs: add guidance on using Protocol instead of Callable for extensible interfaces (#1637)
1 parent 42f15c2 commit cc4afb3

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

docs/STYLE_GUIDE.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,20 @@ logger.warning("Retry limit approaching! attempt=%d max_attempts=%d", attempt, m
5757
```
5858

5959
By following these log formatting guidelines, we ensure that logs are both human-readable and machine-parseable, making debugging and monitoring more efficient.
60+
61+
## Type Annotations
62+
63+
### Avoid `Callable` for Extensible Interfaces
64+
65+
Do not use `Callable` for function type annotations that may need additional parameters in the future. `Callable` signatures are fixed and cannot be expanded without breaking existing implementations.
66+
67+
```python
68+
# Bad: Cannot add parameters later without breaking all existing implementations
69+
EdgeCondition = Callable[[GraphState], bool]
70+
71+
# Good: Protocol allows adding optional keyword arguments in the future
72+
class EdgeCondition(Protocol):
73+
def __call__(self, state: GraphState, **kwargs: Any) -> bool: ...
74+
```
75+
76+
Using `Protocol` with `**kwargs` allows the interface to evolve by adding new keyword arguments without breaking existing implementations that don't use them.

0 commit comments

Comments
 (0)