this post was submitted on 21 Nov 2023
10 points (100.0% liked)

Python

6360 readers
9 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
 

Let's say I have the following structure:

my_module/
  __init__.py
  utilities.py

and __init__.py contains

from .utilities import SomeUtilityFunction

Is there a way to prevent or alert developers when they do

from my_module.utilities import SomeUtilityFunction

instead of

from my_module import SomeUtilityFunction

The problem arose when a few modules started using a function that was imported inside a module in which it wasn't used, while also being available on the module's __init__.py, so after linting the file and removing the unused import my tests started failing.

any other advice for situations like this?

you are viewing a single comment's thread
view the rest of the comments
[–] sebsch@discuss.tchncs.de -1 points 11 months ago (7 children)

I am normally define the interface to models while defining __all__.

Defined in the __init__ it allows to define a whitelist what can be Imported from the outside.

https://docs.python.org/3/tutorial/modules.html#importing-from-a-package

[–] Chais@sh.itjust.works 1 points 11 months ago (6 children)

That's not correct. __all__ is not a whitelist. It is only the list used for

from module import *

If you have a module with submodules foo, bar and baz and __all__ = ["foo", "bar"] it will not prevent you from importing baz manually. It just won't do it automatically.

[–] twoframesperminute@mastodon.social -1 points 11 months ago (1 children)

@Chais from module import \* should almost never be used anyway, so...

[–] Chais@sh.itjust.works 2 points 11 months ago* (last edited 11 months ago)

Renders correctly for me
Screenshot_20231121-134426_Trebuchet_1

load more comments (4 replies)
load more comments (4 replies)