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

Replace implicit reexport with explicit #16597

Open
aganders3 opened this issue Dec 1, 2023 · 0 comments
Open

Replace implicit reexport with explicit #16597

aganders3 opened this issue Dec 1, 2023 · 0 comments
Labels
feature topic-implicit-reexport The --no-implicit-reexport option

Comments

@aganders3
Copy link

Feature

I would like to create a way to explicitly suppress a specific implicit export.

Pitch

I am working with a wrapper library that attempts to unify an interface between multiple backends. So for example it does something like this:

# my_lib/foo.py

from . import BACKEND_A, BACKEND_B

if BACKEND_A:
    from lib_a.foo import *

    Confabulator.bar = Confabulator.baz

elif BACKEND_B:
    from lib_b.foo import *

The implicit export via star-import here is helpful because there are a lot of classes to import, more may be added, and most of them don't need to be modified like this at all.

Unfortunately this causes some minor problems with typing - I get an attr-defined error depending on the installed backend:

from my_lib.foo import Confabulator
c = Confabulator()
reveal_type(c)  # revealed type is lib_a.foo.Confabulator
c.bar()  # raises an [attr-defined] error

At first I thought this was an issue with adding class attributes after definition (rejected in #5363), but it's also a problem if I try to instead to subclass and re-assign. I think the implicit re-export is getting in the way of redefining the class.

My initial thought on how to achieve this would be to allow from lib_a.foo import Confabulator as _Confabulator after the from lib_a.foo import * to kind of undo the implicit export. I poked around in the code a bit but have not figured out the best way to do this yet. I do worry this would be a breaking change, however. I am open to any other suggestions or workarounds!

@AlexWaygood AlexWaygood added the topic-implicit-reexport The --no-implicit-reexport option label Dec 1, 2023
@aganders3 aganders3 changed the title Replace implicit import with explicit Replace implicit reexport with explicit Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-implicit-reexport The --no-implicit-reexport option
Projects
None yet
Development

No branches or pull requests

2 participants