Importing modules in places other than the toplevel is usually a bad idea, but Pylint doesn't check for it. I would like the following example to be flagged:
def import_in_function():
import random
return random.random()
This would also go for classes, etc.
https://github.com/PyCQA/pylint/issues/692 discusses this, but it was closed long ago. Perhaps it's worth revisiting?
Yeah, this would be great to have. At my work, we implemented a similar rule with an escape hatch to allow local modules and packages to still be imported at function level, in case there is cyclic dependency between the module where the function is located and the imported module.
allow local modules and packages to still be imported at function level
How do you configure this?