抽象类和PyMongo;无法实例化抽象类

时间:2015-01-28 20:39:31

标签: python python-3.x abstract-class pymongo pymongo-2.x

我创建了空的抽象类AbstractStorage并从中继承了Storage类:

import abc
import pymongo as mongo

host = mongo.MongoClient()

print(host.alive()) # True

class AbstractStorage(metaclass=abc.ABCMeta):
    pass

class Storage(AbstractStorage):
    dbh = host
    def __init__(self):
        print('__init__')

Storage()

我预计输出为

True
__init__

但是,我得到的是

True
Traceback (most recent call last):
  File "/home/vaultah/run.py", line 16, in <module>
    Storage()
TypeError: Can't instantiate abstract class Storage with abstract methods dbh

如果我删除metaclass=abc.ABCMeta(以便AbstractStorage成为普通类)和/或我将dbh设置为其他值,则问题(显然)会消失。

这里发生了什么?

2 个答案:

答案 0 :(得分:4)

这对ABCs来说确实不是问题,但这是PyMongo的一个问题。有一个问题here。看起来pymongo会覆盖__getattr__来返回某种类型的数据库类。这意味着host.__isabstractmethod__返回一个Database对象,在布尔上下文中为true。这导致ABCMeta认为host是一种抽象方法:

>>> bool(host.__isabstractmethod__)
True

问题报告中描述的解决方法是在您的对象上手动设置host.__isabstractmethod__ = False。关于这个问题的最后评论表明已经为pymongo 3.0提供了修复。

答案 1 :(得分:1)

短版

mongo.MongoClient返回一个似乎是(是?)抽象方法的对象,然后将其分配给dbh中的Storage字段。这使得Storage成为一个抽象类,因此实例化它会引发TypeError

请注意,我没有pymongo,因此我无法告诉您有关MongoClient的更多信息,而不是ABCMeta如何对待它。

长版

ABCMeta.__new__方法查看它正在创建的新类的每个字段。任何本身具有True(或&#34;类似于&#34;)__isabstractmethod__字段的字段都被视为抽象方法。如果一个类具有任何非重写的抽象方法,则整个类被认为是抽象的,因此任何实例化它的尝试都是错误的。

来自早期版本的标准库abc.py

def __new__(mcls, name, bases, namespace):
    cls = super().__new__(mcls, name, bases, namespace)
    # Compute set of abstract method names
    abstracts = {name
                 for name, value in namespace.items()
                 if getattr(value, "__isabstractmethod__", False)}
    # ...
    cls.__abstractmethods__ = frozenset(abstracts)
    # ...

abc.ABCMeta类文档中未提及此问题,但在@abc.abstractmethod装饰器下方略低一点:

  

为了正确地与抽象基类机制进行互操作,描述符必须使用__isabstractmethod__将自身标识为抽象。通常,如果用于组成描述符的任何方法都是抽象的,则此属性应为True

示例

我创造了一个虚假的&#34;抽象的&#34;具有__isabstractmethod__属性的类,以及AbstractStorage的两个所谓具体子类。您会看到一个产生您正在获得的确切错误:

#!/usr/bin/env python3


import abc
# I don't have pymongo, so I have to fake it.  See CounterfeitAbstractMethod.
#import pymongo as mongo


class CounterfeitAbstractMethod():
    """
    This class appears to be an abstract method to the abc.ABCMeta.__new__
    method.

    Normally, finding an abstract method in a class's namespace means
    that class is also abstract, so instantiating that class is an
    error.

    If a class derived from abc.ABCMeta has an instance of
    CounterfeitAbstractMethod as a value anywhere in its namespace
    dictionary, any attempt to instantiate that class will raise a
    TypeError: Can't instantiate abstract class <classname> with
    abstract method <fieldname>.
    """
    __isabstractmethod__ = True


class AbstractStorage(metaclass=abc.ABCMeta):

    def __init__(self):
        """
        Do-nothing initializer that prints the name of the (sub)class
        being initialized.
        """
        print(self.__class__.__name__ + ".__init__ executing.")
        return


class ConcreteStorage(AbstractStorage):
    """
    A concrete class that also _appears_ concrete to abc.ABCMeta.  This
    class can be instantiated normally.
    """
    whatever = "Anything that doesn't appear to be an abstract method will do."


class BogusStorage(AbstractStorage):
    """
    This is (supposedly) a concrete class, but its whatever field appears
    to be an abstract method, making this whole class abstract ---
    abc.ABCMeta will refuse to construct any this class.
    """
    #whatever = mongo.MongoClient('localhost', 27017)
    whatever = CounterfeitAbstractMethod()


def main():
    """
    Print details of the ConcreteStorage and BogusStorage classes.
    """
    for cls in ConcreteStorage, BogusStorage:
        print(cls.__name__ + ":")
        print("    whatever field: " + str(cls.whatever))
        print("    abstract methods: " + str(cls.__abstractmethods__))
        print("    Instantiating...")
        print("    ", end="")
        # KABOOM!  Instantiating BogusStorage will raise a TypeError,
        # because it appears to be an _abstract_ class.
        instance = cls()
        print("    instance: " + str(instance))
        print()
    return


if "__main__" == __name__:
    main()

运行它会产生:

$ ./storage.py
ConcreteStorage:
    whatever field: Anything that doesn't appear to be an abstract method will do.
    abstract methods: frozenset()
    Instantiating...
    ConcreteStorage.__init__ executing.
    instance: <__main__.ConcreteStorage object at 0x253afd0>

BogusStorage:
    whatever field: <__main__.CounterfeitAbstractMethod object at 0x253ad50>
    abstract methods: frozenset({'whatever'})
    Instantiating...
    Traceback (most recent call last):
  File "./storage.py", line 75, in <module>
    main()
  File "./storage.py", line 68, in main
    instance = cls()
TypeError: Can't instantiate abstract class BogusStorage with abstract methods whatever