Pylint: New check: import-outside-toplevel

Created on 15 Aug 2019  路  2Comments  路  Source: PyCQA/pylint

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?

checkers contributor friendly enhancement

All 2 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DGalt picture DGalt  路  3Comments

DevynCJohnson picture DevynCJohnson  路  3Comments

ethanchewy picture ethanchewy  路  3Comments

pylint-bot picture pylint-bot  路  3Comments

GergelyKalmar picture GergelyKalmar  路  3Comments