False Unused Import Statement in PyCharm? False Unused Import Statement in PyCharm? python python

False Unused Import Statement in PyCharm?


You can actually use the PyUnresolvedReferences marker to deactivate the inspection for your import statement:

# noinspection PyUnresolvedReferencesimport A

Reference: PyCharm bug PY-2240


As far as I can tell this behaviour is not handled as an inspection or some other configurable option, which means there is no #noinspection UnusedImport (or equivalent) that can be placed before the imports.

If you don't want to define an unused block where you use those variables there's an other simple and probably better way to achieve what you want:

#b.py codeimport A# [...] your code__all__ = ['A', ...]  # *all* the names you want to export

PyCharm is smart enough to look at __all__ and avoid removing A as unused import.However there's a limitation that __all__ must be a simple list literal. You cannot do things like:

__all__ = ['A'] + [name for name in iterable if condition(name)]

Not even:

x = 'b'__all__ = ['A', x]

Defining __all__ is a best-practice to make your module *-import safe anyway, so is something you should already do.


from C import A, B_ = (A, B); del _

Works for me. I don't like

# noinspection PyUnresolvedReferences

as it would give false negatives in case A cannot be imported. And

__all__ = ['A', 'B', ...]

is cryptic and is not convenient for refactoring.