How do I stop pyCharm from complaining about underscore strings?

喜你入骨 提交于 2020-06-25 09:14:13

问题


I'm programming in pyCharm making a wxPython project (Mostly generated from wxGlade). If I have some code which specifies a string e.g.:

value_label = wx.StaticText(self, wx.ID_ANY, _("Value"))

It then complains about Unresolved reference '_'. Is there any way to ignore only this unresolved reference?


回答1:


You can change the settings of the Unresolved reference inspection:

  • Open the Settings... menu
  • Select the Inspections page
  • Search for Unresolved references and click the inspection. On the bottom-right you should see a list widget titled Ignore references.
  • Add _ to the list.

Warning: this will ignore the unresolved reference _ in all source files of the project. This isn't usually a problem because it's highly unlikely that you'd use that kind of name for your functions. If you ever get a NameError about _ then you already know that you forgot to call gettext.install.

Alternatively:

  • Open the offending file and place the cursor at an occurence of _.
  • Press Alt+Enter to open the context menu (this may depend on the shortcuts you chose or configured).
  • Select Ignore unresolved reference module_name._
  • Select Fix all 'Unresolved references' problems

I just checked and it's possible to limit this to only a module. If you are using _ inside module a then add a._ to the list Ignore references and all usages of _ inside the a.py module will be ignored, while the warnings will be shown for misuses of _ in other modules.




回答2:


Another option is to include this above the class/method/function you are writing:

# noinspection PyUnresolvedReferences



回答3:


As the original question implies, this is a real problem for i18n'd code which ends up flagging every occurrence of a string marked with _(). My solution, which trades the hundreds of red flags for one, is to add this to the code:

    # Keep PyCharm happy.
    _ = _

The one red flag this causes is easy enough to ignore, and keeps reminding me that though I love PyCharm, it is not perfect :-).



来源:https://stackoverflow.com/questions/23019580/how-do-i-stop-pycharm-from-complaining-about-underscore-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!