Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subclassing from missing dependency #27

Open
ZanSara opened this issue Apr 18, 2023 · 1 comment · May be fixed by #28
Open

Allow subclassing from missing dependency #27

ZanSara opened this issue Apr 18, 2023 · 1 comment · May be fixed by #28

Comments

@ZanSara
Copy link
Contributor

ZanSara commented Apr 18, 2023

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 🙂

@ZanSara ZanSara linked a pull request Apr 18, 2023 that will close this issue
@Mandera
Copy link
Contributor

Mandera commented Apr 19, 2023

This is a really good idea, I haven't thought of this! I noticed that __init_subclass__ = error_func is the only method preventing subclassing currently.

I've never heard of __mro_entries__ before, never found it when I did my initial dunder research!

Found PEP 560 referencing it

Like I mentioned in the PR - I'd love if we could return a FakeModule object when it's subclasses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants