我有以下代码:
def from_utf8(string: bytes | str) -> str:
if isinstance(string, bytes):
return string.decode("utf-8")
else:
return string # <- type warning on this line
pylance 在这一行给了我一个类型警告return string
:
Type "bytearray | memoryview[_I@memoryview] | str" is not assignable to return type "str"
Type "bytearray | memoryview[_I@memoryview] | str" is not assignable to type "str"
"bytearray" is not assignable to "str"
我的理解是:
类型注释x: bytes
实际上是“运行时类型”的别名x: bytes | bytearray | memoryview[_I@memoryview]
,但isinstance(x, bytes)
只检查bytes
,而不检查另外两个。
我尝试以相反的方式检查类型:
def from_utf8(string: bytes | str) -> str:
if isinstance(string, str):
return string
else:
return string.decode("utf-8") # <- no attribute 'decode' for 'memoryview'
错误现在变成:
Cannot access attribute "decode" for class "memoryview[_I@memoryview]"
Attribute "decode" is unknown
上下文:
- 我的项目使用python 3.11
- 我在 vscode 中看到这些警告,使用 pylance 版本 2025.2.1 和 python (
ms-python.python
) 扩展版本 2025.0.0
我是否有一种方便的方法来编写一个from_utf8(string)
可以通过类型检查器的版本?
另外:我的假设正确吗?有记录吗?