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

How to bypass mypyc when importing #1034

Open
flying-sheep opened this issue Nov 21, 2023 · 4 comments
Open

How to bypass mypyc when importing #1034

flying-sheep opened this issue Nov 21, 2023 · 4 comments

Comments

@flying-sheep
Copy link

I want to import a module from its sources to avoid

TypeError: interpreted classes cannot inherit from compiled

How can I skip the .so files and do that?

@eli-schwartz
Copy link

Isn't this more of a question for the python interpreter? What should mypyc be doing at the point where cpython already attempted to load a .so, to make it load a .py instead?

@flying-sheep
Copy link
Author

It is, I’m just asking if there’s a blessed way to do it.

I tried temporarily modifying sys.meta_path, but the following will fail with

AssertionError: Where do you come from?

and I’m completely stumped why.

def import_from_python(mod_name: str) -> ModuleType:
    assert mod_name not in sys.modules
    meta_path = sys.meta_path.copy()

    sys.meta_path[:] = [
        loader
        for loader in meta_path
        if getattr(loader, "__name__", None) != "ExtensionFileLoader"
    ]

    try:
        spec = find_spec(mod_name)
        assert spec
        assert type(spec.loader).__name__ != "ExtensionFileLoader", "Where do you come from?"
        return import_module(mod_name)
    finally:
        sys.meta_path[:] = meta_path

@eli-schwartz
Copy link

Again, that sounds like a question for the CPython repository?

@flying-sheep
Copy link
Author

The actual issue is “interpreted classes cannot inherit from compiled”, which comes from mypyc.

I’m asking here to get an answer about the best way to deal with that. I assume that way is “import everything from the Python files somehow”, but I’m not sure.

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

No branches or pull requests

2 participants