mypy: Blueprint type mismatch with `flask.sansio.blueprints.Blueprint`
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
调研方向
先从暴露 app.blueprints 的 Quart Blueprint 和 Quart 入口点入手,然后将它们的注解与 Flask 的 sansio Blueprint 定义进行比较。使用提供的最小示例和 mypy --strict,评估 Flask 的类型标注还是 Quart 侧的 cast 更适合解决这一不匹配问题,并在定义完成标准之前考虑对 Quart/Flask 生态系统的影响。
由索引模型根据 Issue 内容生成。
描述
When using Quart's Blueprint, mypy doesn't recognize its inheritance from flask.sansio.blueprints.Blueprint. This causes type errors when accessing blueprints through app.blueprints, which returns the Flask Blueprint type.
Environment
- Python version: 3.12.7
- Quart version: 0.20.0
Minimal Reproducible Example
from quart import Quart
from quart.blueprints import Blueprint
app = Quart(__name__)
bp = Blueprint("test", __name__)
app.register_blueprint(bp)
# This works at runtime but fails type check
def process_blueprint(blueprint: Blueprint) -> None:
print(f"Processing blueprint: {blueprint.name}")
# Type error here - blueprints.values() returns flask.sansio.blueprints.Blueprint
for blueprint in app.blueprints.values():
process_blueprint(blueprint) # Error: Expected quart.blueprints.Blueprint, got flask.sansio.blueprints.Blueprint
# Show inferred types
reveal_type(bp) # Shows quart.blueprints.Blueprint
reveal_type(app.blueprints) # Shows Dict[str, flask.sansio.blueprints.Blueprint]
Mypy output:
mypy quart_blueprint_mre.py --show-error-codes --strict
quart_blueprint_mre.py:19: error: Argument 1 to "process_blueprint" has incompatible type "flask.sansio.blueprints.Blueprint"; expected "quart.blueprints.Blueprint" [arg-type]
quart_blueprint_mre.py:22: note: Revealed type is "quart.blueprints.Blueprint"
quart_blueprint_mre.py:23: note: Revealed type is "builtins.dict[builtins.str, flask.sansio.blueprints.Blueprint]"
Found 1 error in 1 file (checked 1 source file)
In my real codebase, I have something like this:
from typing import TypeAlias
from quart import Quart
from quart.blueprints import Blueprint
Scaffold: TypeAlias = Blueprint | Quart
def _inject_routes(app: Scaffold) -> None:
for endpoint, func in app.view_functions.items():
if not injected(func):
wrapped = _make_wrapper(func)
app.view_functions[endpoint] = wrapped
It can be fixed, of course, with suppression or with adding flask.sansio.blueprints.Blueprint to the type alias, but I guess it's not the best solution.
As I found out, that's because of the more complex hierarchy of the App/Blueprint classes for Quart. So I'm wondering what would be the best way to handle this inheritance in the type system:
- Make Flask's sansio layer more generic to support framework extension:
TBlueprint = TypeVar('TBlueprint', bound='Blueprint', covariant=True)
blueprints: Dict[str, TBlueprint]
- Or add type casting on Quart's side:
@property
def blueprints(self) -> Dict[str, Blueprint]:
return cast(Dict[str, Blueprint], super().blueprints)
I'd be happy to submit a PR with either approach once you suggest me which is more appropriate for the Quart/Flask ecosystem.
- 主要语言
- Python
- 星标
- 3.7k
- 派生
- 206
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pallets/quart 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
-
难度 1/5 1 小时以内 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 42/100
-
难度 3/5 1-2 天 新手友好度 45/100
-
难度 4/5 3-5 天 新手友好度 35/100
相似的 Issue
-
bug status/needs-triage
难度 2/5 1-3 小时 新手友好度 86/100
prowler-cloud/prowler#12887 · 1 条评论 ·
维护者通常 1 天内回复
-
area: desktop platform: macos priority: p3 status: ready type: enhancement
难度 1/5 1 小时以内 新手友好度 92/100
use-agent-os/agent-os#3484 ·
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
open-telemetry/opentelemetry-python-contrib#5113 · 2 条评论 · 2 个 reaction ·
维护者通常 1 天内回复
-
external
难度 2/5 1-3 小时 新手友好度 68/100
langchain-ai/docs#6255 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复