Redefinition using the same type should not be an error
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
描述
Possibly related to #10563.
Bug Report
Consider the following:
# a.py
x: int = 5
# b.py
from a import *
x: int = 7 # error: Name "x" already defined (possibly by an import) [no-redef]
I would not call it an error. Sure, it's already defined, but it was an int originally.
If you do this:
# c.py
from a import *
x: float = 7.5 # error: Name "x" already defined (possibly by an import) [no-redef]
You get the same error, but in this case, I'd say that's correct, because maybe I didn't realize it was an int.
And this:
# d.py
from a import *
x = 'boo' # error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
This I also agree with.
Expected Behavior
I would argue that I should not get an error in b.py, whereas the errors in c.py and d.py are correct.
Typehints to me are as much a documentation tool as they are indispensable to finding bugs. Not being able to reiterate that something is an int means that I cannot document it in the code. I believe this is particularly important with star-imports because by just reading the current file, you have no idea what's being imported.
Here's something else to consider:
# a.py
x: float = 3.14
# b.py
from a import *
x = 5 # no error, because 5 is a float.
Do we know that the author new that x is a float and thus 5 is OK, or did they really mean to write x: int = 5? If I were allowed to write x: float = 5, we would have a definitive answer.
- Mypy version used: 1.3.0
- Mypy configuration options from
mypy.ini(and other config files):ignore_missing_imports = True - Python version used: 3.8.10
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用报告中的 mypy 配置和 Python 版本复现 a.py、b.py、c.py 和 d.py 中的示例。比较同类型重新定义、不同注解和不兼容赋值的诊断结果。当预期的情况能够得到一致区分,并且针对报告行为具有回归测试覆盖时,即可视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100