Skip to content

Allow subclassing from missing dependency #27

@ZanSara

Description

@ZanSara

Problem

So far generalimport does not allow users to create subclasses of missing dependencies. So this code:

generalimport("my_module")
import my_module

class SubClass(my_module.BaseClass):
    def __init__(self):
        ...

fails with:

Traceback (most recent call last):
  File "/work/generalimport/example.py", line 6, in <module>
    class SubClass(test_module.BaseClass):
  File "/work/generalimport/generalimport/fake_module.py", line 21, in error_func
    raise MissingOptionalDependency(f"Optional dependency {name} was used but it isn't installed.")
generalimport.exception.MissingOptionalDependency: Optional dependency 'test_module' was used but it isn't installed.

This is quite limiting in OOP and makes using generalimport with libraries like sqlalchemy close to impossible.

Proposed solution

I propose leveraging __mro_entries__ to make this usecase work. Classes based on non-existing modules should fail only when initialized.

PR coming soon 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions