question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Restrict any logics inside `__init__.py`

See original GitHub issue

Side effect inside __init__.py are nightmares.

We need to restrict any kind of python code inside __init__.py except:

  1. comments
  2. docstrings

Imports should be forbidden. Each separate module should have its own public API.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Jul 14, 2018

Oh, great!

What I have in mind is: 0. We need to create new error for this types of issues, let it be Z400 (as I reserve Z3xx for classes)

  1. We need to create ModuleFileVisiter to check these kinds of features
  2. Then we need to pass the filename and the ast tree into this visiter
  3. And then check only root-level nodes to be in whitelist (just docs for now)
  4. Raise errors if other things will be there

Other things that will be required:

  1. integration test
  2. noqa example, since sometimes we really need this feature, and we need a way to disable the check for a single file

Thanks for your support!

0reactions
sobolevncommented, Dec 19, 2021

Yes, you can disable WPS113 per all __init__.py files or per line.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Module imports and __init__.py - Stack Overflow
So if I should remove all scripts follows if __name__ == '__main__': inside a module to prevent the relative import(s) be broken? –...
Read more >
__main__ — Top-level code environment — Python 3.11 ...
This is where using the if __name__ == '__main__' code block comes in handy. Code within this block won't run unless the module...
Read more >
Structuring Your Project - The Hitchhiker's Guide to Python
In practical terms, “structure” means making clean code whose logic and dependencies are clear as well as how the files and folders are...
Read more >
Python: __init__() is not the only constructor
Some practical use of __new__() can be to restrict the number of objects created from a class, make a singleton and return a...
Read more >
how the Python import system works - Ten thousand meters
The reason why we import any module is because we want to get an access to ... The initial idea of requiring __init__.py...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found