Assigning a variable from either of two optional variables
还没有人认领这个 Issue。
评估
调研方向
使用提供的 Python 复现程序作为第一个切入点,并对提前返回后的 x or y 赋值运行 mypy。跟踪此控制流是如何分析的;完成标准是示例接受 other 为 str,同时不削弱其他位置报告的 Optional 行为。
由索引模型根据 Issue 内容生成。
描述
Bug Report
consider the following example:
from typing import Optional
def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
if not (x or y):
return "early"
other: str = x or y # <<< mypy complains that other should be Optional[str]
return other
am i not able to assume that other would be of type str given that if neither x nor y is given then the function would return early and the assignment of other would never happen? in other words, at the assignment of other, surely one or both of x or y would be of type str, therefore other would be of type str?
to further the argument, if i modify the example like so:
#!/usr/bin/env python3
from typing import Optional
def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
if not (x or y):
return "early"
other: str = x or y
print(f"x = {x}")
print(f"y = {y}")
print(f"other class: {type(other)}")
print()
return other
test_something()
test_something(x="hello")
test_something(y="goodbye")
test_something(x="hello", y="goodbye")
and run it i get:
x = hello
y = None
other class: <class 'str'>
x = None
y = goodbye
other class: <class 'str'>
x = hello
y = goodbye
other class: <class 'str'>
To Reproduce
run mypy on the example above
Expected Behavior
mypy would not complain about typing other as str
Actual Behavior
running mypy on the above example yields:
.../test.py: note: In function "test_something":
.../test.py:8:18: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str") [assignment]
other: str = x or y
^
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files):
strict = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
- Python version used: 3.7.9
- Operating system and version: MacOSX 10.15.7 Catalina
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 74/100
bancolombia/sentinel#23 ·
-
test md 未关闭CI
难度 2/5 1-3 小时 新手友好度 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
难度 2/5 1-3 小时 新手友好度 74/100
langchain-ai/deepagents#6450 ·
-
bug client
难度 2/5 1-3 小时 新手友好度 88/100