'PyCharm raising Unresolved reference + expression expected for mypy ignore based on error code

I am trying to silence mypy errors based on error codes. This is done using something like this:

from foolib import foo  # type: ignore[attr-defined]

I believe PyCharm is interpreting my type: ignore[code] comments as a type comment, and reporting ignore as an unresolved reference.

Unresolved reference on type comment

Also, PyCharm expects an expression within the brackets.

Expression expected within type ignore comment

mypy error I'm trying to suppress:

pylint_ignore.py:8: error: Skipping analyzing 'pylint.utils': found module but no type hints or library stubs  [import]

And yes, I know I can just say type: ignore, and not include a code, or specify to ignore this particular import in a config file. However, I would like to specify the error codes, because I think it's a good feature.

How can I get PyCharm not to complain about this?


Research

This answer to How do I stop pyCharm from complaining about underscore strings?

Helped me realize under Preferences --> Editor --> Inspections --> Python --> Unresolved references, I can add a fully qualified symbol name to be ignored.

I believe this is officially documented here.

I tried adding *.ignore.* (since I don't want to have to build up a per-module ignore list), but this didn't work.

If this is the right approach, can you help me figure out the right syntax?


Versions

mypy==0.770
python==3.6.5
PyCharm PE 2020.1


Solution 1:[1]

Apparently if you put a literal tab character between the # and type:, PyCharm will treat it as a regular comment. As PyCharm normally converts tab to spaces, you will have to copy it in from somewhere, but hey...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 squirrel