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

Python

6413 readers
4 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?

top 13 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

You could guard it.
__init__.py:

_GUARD_SOME_UTILITY_FUNCTION = True
from .utilities import SomeUtilityFunction

utilities.py:

def SomeUtilityFunction():
    if not _GUARD_SOME_UTILITY_FUNCTION:
        raise SomeException("Helpful error message")

Take this with a grain of salt, as I'm typing this on my phone and haven't actually tried it.

Alternatively there's the import-guard package on PyPI. No idea if it's any good, though. Just something a quick search brought up.

Edit:
Ok, I tried my suggestion and it doesn't work.

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago) (1 children)

This approach seems quite overkomplex. Instead of having these errors on runtime, stuff like this should sit in linter rules of any kind.

[–] [email protected] 2 points 1 year ago

It's only useful during development there.

[–] [email protected] 0 points 1 year ago (1 children)

@fixmycode mypy type checking can report this error in your code:

iox3.py:3: error: Module "iox2" does not explicitly export attribute "y" [attr-defined]

which I think is roughly the problem you are encountering: an attribute in an imported module that wasn't explicitly defined in that module, but instead came from somewhere else.

[–] [email protected] 1 points 1 year ago

I think this is the more sensitive approach, I'll take a look at putting mypy in my pipeline

[–] [email protected] -1 points 1 year ago (1 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

[–] [email protected] 1 points 1 year ago (2 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.

[–] [email protected] 0 points 1 year ago* (last edited 1 year ago) (1 children)

It works exactly like one. You get a warning if you try to import something not defined in it. The docs are just very confusing here ;)

[–] [email protected] 0 points 1 year ago* (last edited 1 year ago) (1 children)

Bullshit!

module/__init__.py:

__all__ = ["foo", "bar"]

module/foo.py:

def foo():
    print("foo")

module/bar.py:

def bar():
    print("bar")

module/baz.py:

def baz():
    print("baz")

main.py:

from module import *
from module import baz

if __name__ == "__main__":
    print("main")
    foo.foo()
    bar.bar()
    baz.baz()

Output:

$ python main.py 
main
foo
bar
baz

No errors, warnings or anything.

[–] [email protected] 0 points 1 year ago (1 children)

You're running python without linters? Interesting approach.

[–] [email protected] 1 points 1 year ago

You can't expect the user to have one.

[–] [email protected] -1 points 1 year ago (1 children)

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

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

Renders correctly for me
Screenshot_20231121-134426_Trebuchet_1