9

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
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119

1 Answers1

2

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...

squirrel
  • 5,114
  • 4
  • 31
  • 43
  • Not a bad solution. Now PyCharm warns (not raises error) `PEP 8: E262 inline comment should start with '# '`. I think a warning is better than an error, good thinking. Unfortunately, my code is also part of a CI pipeline that includes `flake8`, which flags this tab character. This is indeed a workaround, unfortunately, it will not work for me. – Intrastellar Explorer Jun 22 '20 at 00:06
  • You can silence individual PEP 8 violation warnings in PyCharm as well as in your linter. In PyCharm, simply add E262 to inspection “PEP 8 coding style violations”. If you use flake8, you can add a “no quality assurance” comment like this: `# type: ignore[misc] # noqa: 262`, or run flake8 like this: `flake8 --ignore=E262` – squirrel Jun 22 '20 at 00:37
  • I am aware of using `noqa` comments and I agree that does the trick for `flake8`. PyCharm still complains with a weak warning, but I can look past that, or add in a `# noinspection` comment (though that'd be overkill). I just wish PyCharm would fix the bug... pasting in `# type: ignore[import] # noqa: E262` from an external source is a hassle. Thanks again for pointing this workaround out @squirrel! – Intrastellar Explorer Jun 22 '20 at 01:09