Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 704
Intrastellar Explorer Avatar asked May 18 '20 23:05

Intrastellar Explorer


People also ask

What is an unresolved reference?

To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you're typing in the file points to. It may be a function, a variable, or another construct of the language that you've yet to declare in the code.

How to solve unresolved references in PyCharm?

Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it. Install via PyCharm (works with Community Edition).

How is PyCharm interpreting my type [code] comments?

I believe PyCharm is interpreting my type: ignore [code] comments as a type comment, and reporting ignore as an unresolved reference. Also, PyCharm expects an expression within the brackets. 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.

How to fix PyCharm not responding in Python?

To fix this: 1 Open PyCharm settings 2 Navigate to Editor -> File Types 3 Find Python and add __init__.py to the list of python files

Why does PyCharm ignore relative imports from a project folder?

Relative imports while code root folder is not the same as the project folder. Solution: Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this: Show activity on this post.


1 Answers

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

like image 118
squirrel Avatar answered Oct 05 '22 22:10

squirrel