Plugin hook to customize __repr__ of an Instance
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
For example, in Django, there's this QuerySet object with a lot of methods, and to properly support those methods we need two (and maybe more in the future) generic parameters.
With get_type_analyze_hook I can convert QuerySet[MyModel] into QuerySet[MyModel, MyModel], so in the code user don't need to think about number of generics. But in error messages, all those generics values are present, though they don't have any semantics, it's just implementation detail.
Or, for Field, I need to have two generic params, one for type of __set__, one for __get__, so they appear in error message as (CharField for example)
django.db.models.fields.CharField[Union[builtins.str, builtins.int, django.db.models.expressions.Combinable], builtins.str]
None of this info makes any sense to users, so I want it to just be django.db.models.fields.CharField.
This is monkeypatching version that works for one of the cases, should be simple enough to add hook for that,
def make_queryset_repr_return_only_one_type():
from mypy.types import TypeStrVisitor
old_visit_instance = TypeStrVisitor.visit_instance
def patched_visit_instance(self, t: Instance) -> str:
if t.type.has_base(helpers.QUERYSET_CLASS_FULLNAME):
return old_visit_instance(self, helpers.reparametrize_instance(t, [t.args[0]]))
else:
return old_visit_instance(self, t)
TypeStrVisitor.visit_instance = patched_visit_instance
I can make a PR, if you approve it as a viable feature.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 TypeStrVisitor.visit_instance 和现有的 get_type_analyze_hook 机制入手,使用所示的 monkeypatch 作为行为参考。确定受支持的 plugin hook 将如何自定义 Instance 的渲染,并将 QuerySet 和 Field 示例作为完成标准:仅用于实现的泛型参数不应再出现在面向用户的类型错误中。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100