Marking an import to prevent deleting it with "optimize imports"

In django I often use something like

in models.py or urls.py:

import signals

I don't use singlas later in code, this is just to run signals.py code with django. PyCharm marks this import as unused, and will delete it on every usage of "optimize import".

Is it possible to prevent PyCharm doing that? Any magic comment/annotation?

6
6 comments

Please try pressing Alt-Enter on the unused import, pressing the right arrow key to show the inspection options, and selecting "Suppress for statement".

7

Add a comment before the import...

# noinspection PyUnresolvedReferences

PyCharm 2019.3.3 does not offer a "Suppress for statement" option in the drop down, however it does hint at the solution as the inspection violation is "Unresolved References".  For a complete list of inspection suppression comments, see https://www.jetbrains.com/help/pycharm/disabling-and-enabling-inspections.html

3

Hi,

Following on from this... is there a way to mark a block of code like this, rather than do one line at a time?

 

So instead of this...

// noinspection ES6UnusedImports
import unsplash_001 from '../assets/unsplash-001.jpeg'
// noinspection ES6UnusedImports
import unsplash_002 from '../assets/unsplash-002.jpeg'

// noinspection ES6UnusedImports
import lorempixel_003 from '../assets/lorempixel-003.jpeg'
// noinspection ES6UnusedImports
import lorempixel_004 from '../assets/lorempixel-004.jpeg'
// noinspection ES6UnusedImports
import lorempixel_005 from '../assets/lorempixel-005.jpeg'
// noinspection ES6UnusedImports
import lorempixel_006 from '../assets/lorempixel-006.jpeg'


Something like

// noinspection ES6UnusedImports start
import unsplash_001 from '../assets/unsplash-001.jpeg'
import unsplash_002 from '../assets/unsplash-002.jpeg'

import lorempixel_003 from '../assets/lorempixel-003.jpeg'
import lorempixel_004 from '../assets/lorempixel-004.jpeg'
import lorempixel_005 from '../assets/lorempixel-005.jpeg'
import lorempixel_006 from '../assets/lorempixel-006.jpeg'
// noinspection ES6UnusedImports end

I can dream :)
1

It's actually very annoying they don't offer it as a hint anymore, and the name is completely misleading. So if you really need it (like models definition or whatever) it's bad user experience.

0

Please sign in to leave a comment.