类与it的Python类型并集&x27;例如

类与it的Python类型并集&x27;例如,python,python-3.x,type-hinting,typing,Python,Python 3.x,Type Hinting,Typing,说我有 class Foo: pass 我想写一个函数,可以接受Foo实例或一些Foo子类(不是实例)。所以我写了 def bar(o: Union[Foo, Type[Foo]]): print(1) 并得到这样的错误: TypeError Traceback (most recent call last) <ipython-input-161-2a8355efa688> in <module&

说我有

class Foo:
   pass
我想写一个函数,可以接受Foo实例或一些Foo子类(不是实例)。所以我写了

def bar(o: Union[Foo, Type[Foo]]):
    print(1)
并得到这样的错误:

TypeError                                 Traceback (most recent call last) <ipython-input-161-2a8355efa688> in <module>()
----> 1 def bar(o: Union[Foo, Type[Foo]]):
      2     print(1)
      3 

/usr/lib/python3.5/typing.py in __getitem__(self, parameters)
    550             parameters = (parameters,)
    551         return self.__class__(self.__name__, self.__bases__,
--> 552                               dict(self.__dict__), parameters, _root=True)
    553 
    554     def __eq__(self, other):

/usr/lib/python3.5/typing.py in __new__(cls, name, bases, namespace, parameters, _root)
    510                 continue
    511             if any(isinstance(t2, type) and issubclass(t1, t2)
--> 512                    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
    513                 all_params.remove(t1)
    514         # It's not a union if there's only one type left.

/usr/lib/python3.5/typing.py in <genexpr>(.0)
    510                 continue
    511             if any(isinstance(t2, type) and issubclass(t1, t2)
--> 512                    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
    513                 all_params.remove(t1)
    514         # It's not a union if there's only one type left.

/usr/lib/python3.5/typing.py in __subclasscheck__(self, cls)    1075   return True    1076                 # If we break out of the loop, the superclass gets a chance.
-> 1077         if super().__subclasscheck__(cls):    1078             return True    1079         if self.__extra__ is None or isinstance(cls, GenericMeta):

/home/alexey/dev/gcore/django/lib/python3.5/abc.py in
__subclasscheck__(cls, subclass)
    223                 return True
    224         # Check if it's a subclass of a subclass (recursive)
--> 225         for scls in cls.__subclasses__():
    226             if issubclass(subclass, scls):
    227                 cls._abc_cache.add(subclass)

TypeError: descriptor '__subclasses__' of 'type' object needs an argument
() ---->1个def条(o:Union[Foo,Type[Foo]]): 2份印刷品(1份) 3. /usr/lib/python3.5/typing.py in_uuugetItem_uuu(self,参数) 550参数=(参数,) 551返回self.\uuuuu类\uuuuuu(self.\uuuuuu名称\uuuuuuuu,self.\uuuuuuuuu基, -->552 dict(self.\u dict\u),参数,\u root=True) 553 554定义均衡(自身、其他): /usr/lib/python3.5/typing.py in_uuuuunew_uuuu(cls、名称、基、命名空间、参数、根) 510继续 511(如有)isinstance(t2,类型)和issubclass(t1,t2) -->512表示所有参数中的t2-{t1}如果不存在(t2,TypeVar)): 513所有参数移除(t1) 如果只剩下一种类型,那就不是工会。 /usr/lib/python3.5/typing.py in(.0) 510继续 511(如有)isinstance(t2,类型)和issubclass(t1,t2) -->512表示所有参数中的t2-{t1}如果不存在(t2,TypeVar)): 513所有参数移除(t1) 如果只剩下一种类型,那就不是工会。 /usr/lib/python3.5/typing.py在uuu subasscheck_uuu(self,cls)1075中返回True 1076#如果我们打破循环,超类就有机会了。 ->1077如果super()。\uuuuu子检查(cls):1078如果self.\uuuuu extra\uuuuuuuu为无或不存在,则返回真1079(cls,GenericMeta): /home/alexey/dev/gcore/django/lib/python3.5/abc.py in __子类检查(cls,子类) 223返回真值 224#检查它是否是子类的子类(递归) -->225对于cls中的SCL._子类_uu(): 226如果发布类别(子类别,SCL): 227 cls.\u abc\u cache.add(子类) TypeError:“type”对象的描述符“\uuuu子类”需要参数 是无法在python中指定该类型,还是我使用了错误的方法?

请参阅。这已被修复,不再出现在Python的更新版本中

使用Python
3.6.1
,可以很好地编译函数并正确存储注释:

>>> def bar(o: Union[Foo, Type[Foo]]):
...     print(1)
>>> typing.get_type_hints(bar)
{'o': typing.Union[__main__.Foo, typing.Type[__main__.Foo]]}

无法复制。既不使用python
3.6
也不使用python
3.5