Abstract classes and PyMongo; can't instantiate abstract class

后端 未结 2 852
天命终不由人
天命终不由人 2020-12-22 08:15

I created the empty abstract class AbstractStorage and inherited the Storage class from it:

import abc
import pymongo as mongo

hos         


        
2条回答
  •  有刺的猬
    2020-12-22 08:57

    This isn't really a problem with ABCs, it's a problem with PyMongo. There is an issue about it here. It seems that pymongo overrides __getattr__ to return some sort of database class. This means that host.__isabstractmethod__ returns a Database object, which is true in a boolean context. This cause ABCMeta to believe that host is an abstract method:

    >>> bool(host.__isabstractmethod__)
    True
    

    The workaround described in the issue report is to manually set host.__isabstractmethod__ = False on your object. The last comment on the issue suggests a fix has been put in for pymongo 3.0.

提交回复
热议问题