TypeChecker.lookup_qualified can't find names in submodules

未关闭
#14,954 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
compilers

调研方向

从 mypy/checker.py 中链接的 lookup_qualified 实现开始,跟踪 ctx.api.named_generic_type 如何解析点分隔的名称。使用 plugin 重现报告中的嵌套子模块案例,然后验证无需依赖未公开文档的 TypeChecker.modules 接口,也能找到 foo.bar.baz.Qux 这样的限定名称。

由索引模型根据 Issue 内容生成。

描述

bug topic-plugins

Bug Report

Writing my first mypy plugin, I tried to use the exposed interface ctx.api.named_generic_type to look up a type in my package by name, which is in a submodule like:

ctx.api.named_generic_type('foo.bar.baz.Qux', [])

In this case ctx is a FunctionContext but it applies to most (all?) relevant plugin hook contexts.

The name is not found because foo.bar.baz is a submodule.

To Reproduce

See above.

Expected Behavior

Since this is the only public API for looking up named types, it should be possible--especially for plugins that could be intended for some specific third-party libraries--to be able to look into nested module namespaces.

Actual Behavior

The current implementation uses lookup_qualified which as written assumes that only the first component of a dotted name is necessarily a module name. It then looks into the symbol table for that module to find subsequent name components. This works fine for type lookups like typing.Type but not for other cases, as there is no guarantee that a submodule name is found in its parent module's namespace.

Probably should check the symbol table first, and if not found try the next component as a module name.

To work around this, have to dig into the undocumented TypeChecker.modules to find the required type.

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9
主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 18 小时
30 天内合并 PR
54

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。