|
| 1 | +# Migration to aiohttp web.AppKey |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This migration updates all application keys from string-based keys to type-safe `web.AppKey` instances as recommended by aiohttp for type safety and better development experience. |
| 6 | + |
| 7 | +## Changes Made |
| 8 | + |
| 9 | +### Service Library (`packages/service-library/src/servicelib/aiohttp/`) |
| 10 | + |
| 11 | +1. **application_keys.py** - Updated with web.AppKey imports and pattern |
| 12 | +2. **aiopg_utils.py** - `APP_AIOPG_ENGINE_KEY` |
| 13 | +3. **client_session.py** - `APP_CLIENT_SESSION_KEY` |
| 14 | +4. **docker_utils.py** - `APP_DOCKER_ENGINE_KEY` |
| 15 | +5. **monitor_slow_callbacks.py** - `APP_SLOW_CALLBACKS_MONITOR_KEY` |
| 16 | +6. **monitoring.py** - `APP_MONITORING_NAMESPACE_KEY` |
| 17 | +7. **observer.py** - `APP_FIRE_AND_FORGET_TASKS_KEY` |
| 18 | +8. **requests_validation.py** - `APP_JSON_SCHEMA_SPECS_KEY` |
| 19 | +9. **rest_middlewares.py** - `APP_JSONSCHEMA_SPECS_KEY` |
| 20 | +10. **status.py** - `APP_HEALTH_KEY` |
| 21 | +11. **tracing.py** - `APP_OPENTELEMETRY_INSTRUMENTOR_KEY` |
| 22 | +12. **long_running_tasks/server.py** - `APP_LONG_RUNNING_TASKS_KEY` |
| 23 | + |
| 24 | +### Webserver Service (`services/web/server/src/simcore_service_webserver/`) |
| 25 | + |
| 26 | +1. **application.py** - `APP_WEBSERVER_SETTINGS_KEY` |
| 27 | +2. **db/plugin.py** - `APP_DB_ENGINE_KEY` |
| 28 | +3. **redis.py** - `APP_REDIS_CLIENT_KEY` |
| 29 | +4. **rabbitmq.py** - `APP_RABBITMQ_CLIENT_KEY` |
| 30 | +5. **catalog/plugin.py** - `APP_CATALOG_CLIENT_KEY` |
| 31 | +6. **director_v2/_client.py** - `APP_DIRECTOR_V2_CLIENT_KEY` |
| 32 | +7. **storage/plugin.py** - `APP_STORAGE_CLIENT_KEY` |
| 33 | +8. **socketio/plugin.py** - `APP_SOCKETIO_SERVER_KEY` |
| 34 | +9. **session/plugin.py** - `APP_SESSION_KEY` |
| 35 | +10. **projects/plugin.py** - `APP_PROJECTS_CLIENT_KEY` |
| 36 | +11. **users/plugin.py** - `APP_USERS_CLIENT_KEY` |
| 37 | +12. **login/plugin.py** - `APP_LOGIN_CLIENT_KEY` |
| 38 | +13. **security/plugin.py** - `APP_SECURITY_CLIENT_KEY` |
| 39 | +14. **resource_manager/plugin.py** - `APP_RESOURCE_MANAGER_CLIENT_KEY` |
| 40 | +15. **diagnostics/plugin.py** - `APP_DIAGNOSTICS_CLIENT_KEY` |
| 41 | + |
| 42 | +## Pattern Used |
| 43 | + |
| 44 | +Before: |
| 45 | +```python |
| 46 | +from typing import Final |
| 47 | + |
| 48 | +APP_MY_KEY: Final[str] = f"{__name__}.my_key" |
| 49 | +app[APP_MY_KEY] = value |
| 50 | +data = request.app[APP_MY_KEY] |
| 51 | +``` |
| 52 | + |
| 53 | +After: |
| 54 | +```python |
| 55 | +from typing import Final |
| 56 | +from aiohttp import web |
| 57 | + |
| 58 | +APP_MY_KEY: Final = web.AppKey("APP_MY_KEY", SomeSpecificType) |
| 59 | +app[APP_MY_KEY] = value |
| 60 | +data = request.app[APP_MY_KEY] # Now type-safe with proper type |
| 61 | +``` |
| 62 | + |
| 63 | +## Type Improvements |
| 64 | + |
| 65 | +Where possible, we've used specific types instead of generic `object`: |
| 66 | + |
| 67 | +- `APP_AIOPG_ENGINE_KEY` uses `Engine` (from aiopg) |
| 68 | +- `APP_CLIENT_SESSION_KEY` uses `aiohttp.ClientSession` |
| 69 | +- `APP_DOCKER_ENGINE_KEY` uses `aiodocker.Docker` |
| 70 | +- `APP_REDIS_CLIENT_KEY` uses `RedisClientsManager` |
| 71 | +- `APP_CONFIG_KEY` uses `dict[str, object]` |
| 72 | +- `APP_JSON_SCHEMA_SPECS_KEY` uses `dict[str, object]` |
| 73 | +- `APP_FIRE_AND_FORGET_TASKS_KEY` uses `set[object]` |
| 74 | +- `APP_SLOW_CALLBACKS_MONITOR_KEY` uses `LimitedOrderedStack[SlowCallback]` |
| 75 | + |
| 76 | +## Benefits |
| 77 | + |
| 78 | +1. **Type Safety**: mypy can now properly type-check app key access with specific types |
| 79 | +2. **Better IDE Support**: IDEs can provide better autocomplete and error detection |
| 80 | +3. **Runtime Safety**: aiohttp validates key types at runtime |
| 81 | +4. **Documentation**: Key types are self-documenting and more precise |
| 82 | + |
| 83 | +## Next Steps |
| 84 | + |
| 85 | +- Update unit tests to use the new AppKey patterns |
| 86 | +- Update any remaining string-based app key usage |
| 87 | +- Consider consolidating common keys in shared modules for frequently used keys across multiple plugins |
| 88 | +- Continue refining types from `object` to more specific types where the actual type is known |
0 commit comments