Optional[Type[Foo]] 在 Python 3.5.2 中引发 TypeError
Optional[Type[Foo]] raises TypeError in Python 3.5.2
此代码:
#!/usr/bin/env python
from typing import Optional, Type
class Foo(object):
pass
class Bar(Foo):
pass
def test_me() -> Optional[Type[Foo]]:
print("Hi there!")
return Bar
if __name__ == "__main__":
test_me()
将在 3.5.2 上提高 TypeError
:
Traceback (most recent call last):
File "./test.py", line 11, in <module>
def test_me() -> Optional[Type[Foo]]:
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 649, in __getitem__
return Union[arg, type(None)]
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 552, in __getitem__
dict(self.__dict__), parameters, _root=True)
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in __new__
for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in <genexpr>
for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 1077, in __subclasscheck__
if super().__subclasscheck__(cls):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/abc.py", line 225, in __subclasscheck__
for scls in cls.__subclasses__():
TypeError: descriptor '__subclasses__' of 'type' object needs an argument
而它在 3.6 上运行良好。如果我将 Optional 拼写为 Union[None, Type[Foo]]
.
同样的问题
对于 3.5.2 是否有任何解决方法,同时仍然准确地注释 return 类型?
这是 Python 3.5.2 中的 bug。
Optional[cls]
是 Union[cls, type(None)]
的包装器,它使用 __subclasses__()
来确定一个 class 是否是另一个的子 class。
然而,Type
是Python 3.5.2中type
的子class,也就是说
Union[Type[anything], anything_else]
最终会调用
type.__subclasses__()
…这是一个问题,因为 type
是一个元 class,因此期望与正在寻找子 class 的 class 一起调用,与在常规 class 上调用实例方法要求您提供其自身的实例完全相同,例如str.upper('foo')
.
问题是 fixed 在 Python 3.5.3(以及,如您所见,3.6)中使 Type
不再是 class 的子class type
.
感谢零比雷埃夫斯
我在使用 python 3.5.2 时遇到了同样的问题
我已经编辑
usr/lib/python3.5/typing.py 文件
class Type(type, Generic[CT_co], extra=type):
更改为
class Type(Generic[CT_co], extra=type):
然后它修复了...
我在 python 3.2
上遇到了同样的问题
我刚刚安装了低版本pandas
pandas==0.24.2
默认安装的版本是
pandas==0.25.1
希望对您有所帮助。
此代码:
#!/usr/bin/env python
from typing import Optional, Type
class Foo(object):
pass
class Bar(Foo):
pass
def test_me() -> Optional[Type[Foo]]:
print("Hi there!")
return Bar
if __name__ == "__main__":
test_me()
将在 3.5.2 上提高 TypeError
:
Traceback (most recent call last):
File "./test.py", line 11, in <module>
def test_me() -> Optional[Type[Foo]]:
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 649, in __getitem__
return Union[arg, type(None)]
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 552, in __getitem__
dict(self.__dict__), parameters, _root=True)
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in __new__
for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 512, in <genexpr>
for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/typing.py", line 1077, in __subclasscheck__
if super().__subclasscheck__(cls):
File "/Users/mnot/.pyenv/versions/3.5.2/lib/python3.5/abc.py", line 225, in __subclasscheck__
for scls in cls.__subclasses__():
TypeError: descriptor '__subclasses__' of 'type' object needs an argument
而它在 3.6 上运行良好。如果我将 Optional 拼写为 Union[None, Type[Foo]]
.
对于 3.5.2 是否有任何解决方法,同时仍然准确地注释 return 类型?
这是 Python 3.5.2 中的 bug。
Optional[cls]
是 Union[cls, type(None)]
的包装器,它使用 __subclasses__()
来确定一个 class 是否是另一个的子 class。
然而,Type
是Python 3.5.2中type
的子class,也就是说
Union[Type[anything], anything_else]
最终会调用
type.__subclasses__()
…这是一个问题,因为 type
是一个元 class,因此期望与正在寻找子 class 的 class 一起调用,与在常规 class 上调用实例方法要求您提供其自身的实例完全相同,例如str.upper('foo')
.
问题是 fixed 在 Python 3.5.3(以及,如您所见,3.6)中使 Type
不再是 class 的子class type
.
感谢零比雷埃夫斯
我在使用 python 3.5.2 时遇到了同样的问题 我已经编辑 usr/lib/python3.5/typing.py 文件
class Type(type, Generic[CT_co], extra=type):
更改为
class Type(Generic[CT_co], extra=type):
然后它修复了...
我在 python 3.2
上遇到了同样的问题我刚刚安装了低版本pandas
pandas==0.24.2
默认安装的版本是
pandas==0.25.1
希望对您有所帮助。