Commit d19b67f
refactor: modernize typing and defaults across core API (#1299)
* refactor: modernize typing to PEP 604 style across core API
- Replace Union[X, Y] with X | Y
- Replace Optional[X] with X | None
- Replace List[X], Dict[K, V], Tuple[X] with lowercase builtins
- Import Callable from collections.abc instead of typing
- Fix mutable default arguments (Config(), DependencyMap(), list defaults)
- Improve OpenAPI union schema generation using get_origin/get_args
- Make TemplateInterface.__init__ accept *args/**kwargs
- Clean up unused typing imports
Co-authored-by: Sachin-Bhat <sachin.bhat@users.noreply.github.com>
Made-with: Cursor
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix: lint and Generator typing failures
- robyn/responses.py: Generator[str | None | None] is malformed (typing.Generator
needs 3 args yield/send/return); restore Generator[str, None, None] | AsyncGenerator[str, None].
- robyn/processpool.py: process_pool: List = [] → list = [] (List was unimported).
- integration_tests/base_routes.py: TestTypedBody.items: List[str] → list[str].
- Drop unused typing imports flagged by ruff F401: Optional in robyn/ai.py,
Dict/List in robyn/mcp.py, Protocol/TypeAlias in robyn/openapi.py, the
TYPE_CHECKING-gated SubRouter import in robyn/router.py.
- ruff-format pass: insert PEP 8 blank lines between top-level defs across
ai/authentication/cli/logger/mcp/processpool/reloader/responses/router/
types/__init__.py.
Fixes ubuntu/macos/windows tests, codspeed-benchmarks, lint, and pre-commit.ci.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: restore mutable defaults for Config/DependencyMap and handle PEP 604 unions in param coercion
Two test regressions exposed by the typing modernization:
1. test_subrouter_global_dependency_injection expected app's later
inject_global(GLOBAL_DEPENDENCY="GLOBAL DEPENDENCY") to override the
subrouter's earlier inject_global(...="GLOBAL DEPENDENCY OVERRIDE").
That worked on main only because BaseRobyn(... config=Config(),
dependencies=DependencyMap()) used mutable defaults — every Robyn /
SubRouter constructed without explicit args shared the same instances,
so app.inject_global mutated the same dict the subrouter held. The PR
moved both to None + lazy-init in __init__, breaking the sharing.
Restored the mutable defaults to match main's semantics. SubRouter
signature also restored to match main (Config()/OpenAPI() defaults).
2. test_easy_access_optional_present[sync|async] returned 500 because
robyn/_param_utils.py:unwrap_optional only recognized typing.Union
via .__origin__. PEP 604 `int | None` is types.UnionType and has no
__origin__ attribute, so unwrap_optional returned the raw union and
coerce_value tried (int | None)("30"), raising. Replaced
getattr(annotation, "__origin__", None) with typing.get_origin and
accept both Union and types.UnionType. Same fix applied to
is_list_type for symmetry.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Sanskar Jethi <sansyrox@gmail.com>
Co-authored-by: Sachin-Bhat <sachin.bhat@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 2630311 commit d19b67f
16 files changed
Lines changed: 476 additions & 319 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
1263 | 1263 | | |
1264 | 1264 | | |
1265 | 1265 | | |
1266 | | - | |
| 1266 | + | |
1267 | 1267 | | |
1268 | 1268 | | |
1269 | 1269 | | |
| |||
1273 | 1273 | | |
1274 | 1274 | | |
1275 | 1275 | | |
1276 | | - | |
| 1276 | + | |
1277 | 1277 | | |
1278 | 1278 | | |
1279 | 1279 | | |
| |||
1501 | 1501 | | |
1502 | 1502 | | |
1503 | 1503 | | |
1504 | | - | |
| 1504 | + | |
1505 | 1505 | | |
1506 | 1506 | | |
1507 | 1507 | | |
1508 | 1508 | | |
1509 | | - | |
| 1509 | + | |
1510 | 1510 | | |
1511 | 1511 | | |
1512 | 1512 | | |
1513 | 1513 | | |
1514 | | - | |
| 1514 | + | |
1515 | 1515 | | |
1516 | 1516 | | |
1517 | 1517 | | |
1518 | 1518 | | |
1519 | | - | |
| 1519 | + | |
1520 | 1520 | | |
1521 | 1521 | | |
1522 | 1522 | | |
| |||
1715 | 1715 | | |
1716 | 1716 | | |
1717 | 1717 | | |
1718 | | - | |
| 1718 | + | |
1719 | 1719 | | |
1720 | 1720 | | |
1721 | 1721 | | |
| |||
0 commit comments