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

Python

6360 readers
12 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
[โ€“] benc@mastodon.hawaga.org.uk 1 points 11 months 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.

[โ€“] fixmycode 1 points 11 months ago

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