When a dependency of a class is not available, create a dummy class which throws ImportError when used. Args: klass (str): name of the class. dependency (str): name of the dependency. Returns: class: a class object
(klass, dependency)
| 20 | |
| 21 | |
| 22 | def create_dummy_class(klass, dependency): |
| 23 | """ |
| 24 | When a dependency of a class is not available, create a dummy class which throws ImportError when used. |
| 25 | |
| 26 | Args: |
| 27 | klass (str): name of the class. |
| 28 | dependency (str): name of the dependency. |
| 29 | |
| 30 | Returns: |
| 31 | class: a class object |
| 32 | """ |
| 33 | assert not building_rtfd() |
| 34 | |
| 35 | class _DummyMetaClass(type): |
| 36 | # throw error on class attribute access |
| 37 | def __getattr__(_, __): |
| 38 | raise AttributeError("Cannot import '{}', therefore '{}' is not available".format(dependency, klass)) |
| 39 | |
| 40 | @six.add_metaclass(_DummyMetaClass) |
| 41 | class _Dummy(object): |
| 42 | # throw error on constructor |
| 43 | def __init__(self, *args, **kwargs): |
| 44 | raise ImportError("Cannot import '{}', therefore '{}' is not available".format(dependency, klass)) |
| 45 | |
| 46 | return _Dummy |
| 47 | |
| 48 | |
| 49 | def create_dummy_func(func, dependency): |
no test coverage detected