on_version_apply is typed as a positional callable, but alembic calls it with keyword arguments
还没有人认领这个 Issue。
- #1886 来自 @anish-yen —— 已关闭,未合并
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
调研方向
阅读 alembic/runtime/environment.py 和 alembic/context.pyi 中的 OnVersionApplyFn 定义,然后将它们与 alembic/runtime/migration.py 中使用关键字参数的调用进行比较。更新可调用类型,使其符合文档中说明的关键字参数,并检查相关的类型检查覆盖情况。完成标准是:类型检查器接受按文档编写的回调,并拒绝仅接受位置参数的回调。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
EnvironmentContext.configure(on_version_apply=...) documents that "Each callable accepts no positional arguments and the following keyword arguments: ctx, step, heads, run_args", and MigrationContext.run_migrations() calls each callable that way (alembic/runtime/migration.py):
callback(
ctx=self,
step=step.info,
heads=set(head_maintainer.heads),
run_args=kw,
)
But the parameter is typed as a callable of four positional arguments, in alembic/runtime/environment.py and in alembic/context.pyi:
OnVersionApplyFn = Callable[
[MigrationContext, "MigrationInfo", Collection[Any], Mapping[str, Any]],
None,
]
So a type checker rejects a callback written as documented, and accepts one that fails at runtime.
Expected behavior
The type of on_version_apply describes the keyword arguments, for example:
class OnVersionApplyFn(Protocol):
def __call__(
self,
*,
ctx: MigrationContext,
step: MigrationInfo,
heads: Collection[Any],
run_args: Mapping[str, Any],
) -> None: ...
To Reproduce
from typing import Any
from alembic import context
from alembic.runtime.migration import MigrationContext, MigrationInfo
def as_documented(
*,
ctx: MigrationContext,
step: MigrationInfo,
heads: set[str],
run_args: dict[str, Any],
) -> None:
"""Takes the arguments by keyword, as documented."""
context.configure(on_version_apply=as_documented) # rejected by type checkers
def as_typed(
context_: MigrationContext, step: MigrationInfo, heads: Any, args: Any
) -> None:
"""Takes them by place, as typed."""
context.configure(on_version_apply=as_typed) # accepted; run_migrations() then fails
Error
pyright 1.1.407:
error: Argument of type "(*, ctx: MigrationContext, step: MigrationInfo, heads: set[str], run_args: dict[str, Any]) -> None" cannot be assigned to parameter "on_version_apply" of type "((MigrationContext, MigrationInfo, Collection[Any], Mapping[str, Any]) -> None) | None" in function "configure" (reportArgumentType)
pyrefly 1.3.2:
ERROR Argument `(*, ctx: MigrationContext, step: MigrationInfo, heads: set[str], run_args: dict[str, Any]) -> None` is not assignable to parameter `on_version_apply` with type `((MigrationContext, MigrationInfo, Collection[Any], Mapping[str, Any]) -> None) | None` in function `alembic.context.configure` [bad-argument-type]
at runtime, with as_typed:
TypeError: as_typed() got an unexpected keyword argument 'ctx'
Versions.
- OS: Linux, macOS
- Python: 3.14
- Alembic: 1.20.0
- SQLAlchemy: 2.0.54
- Database: PostgreSQL 17
- DBAPI: asyncpg
Additional context
Our workaround takes the arguments both ways: def on_apply(ctx, step, heads, run_args) -> None, the parameters named as alembic passes them.
Have a nice day!
- 主要语言
- Python
- 星标
- 4.5k
- 派生
- 391
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sqlalchemy/alembic 的其他 Issue
-
Revision message is rendered unescaped into the migration docstring; triple quotes and backslashes break the generated script可能重新可做 @ApparentlyTejas 于 21 天前认领,目前没有进行中的 PR。 未关闭bug command interface
难度 2/5 1-3 小时 新手友好度 70/100
sqlalchemy/alembic#1879 · 3 条评论 ·
-
bug pep 484
难度 2/5 1-3 小时 新手友好度 74/100
sqlalchemy/alembic#1341 · 2 条评论 ·
-
autogenerate - detection bug
难度 2/5 1-3 小时 新手友好度 74/100
sqlalchemy/alembic#1121 · 3 条评论 ·
-
Identity and Computed comparison skipped when compare_server_default is not set, as of 1.18.0可能重新可做 关联的 PR 已关闭且未合并。 未关闭autogenerate - defaults bug regression
难度 3/5 1-2 天 新手友好度 50/100
sqlalchemy/alembic#1877 · 1 条评论 ·
-
Entry point for DefaultImpl subclasses? (or, just use plugins?)可能已有人在做 @dwreeves 于 40 天前认领。 未关闭code review in progress migration environment use case
难度 5/5 一周以上 新手友好度 25/100
sqlalchemy/alembic#1874 · 1 条评论 ·
查看 sqlalchemy/alembic 的全部 Issue
相似的 Issue
-
area:core-platform effort:S priority:P3 risk:night-safe status:ready type:test
难度 2/5 1-3 小时 新手友好度 78/100
randomparity/kdive#3620 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
FinanceFlash/unvibecode#760 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
google-research/google-research#3470 ·
维护者通常 21 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
mrvautin/ha-workouts#11 ·