pylint 1.4 reports E1101(no-member) on all C extensions

时光怂恿深爱的人放手 提交于 2019-11-27 04:10:17

Shortly after posting my question, I found the answer. The change was in fact done on purpose as a security measure. Pylint imports modules to effectively identify valid methods and attributes. It was decided that importing c extensions that are not part of the python stdlib is a security risk and could introduce malicious code.

This was done in the release of Astroid 1.3.1 https://mail.python.org/pipermail/code-quality/2014-November/000394.html

Only C extensions from trusted sources (the standard library) are loaded into the examining Python process to build an AST from the live module.

There are four solutions if you want to use pylint on projects that import non-stdlib c extensions.

1) Disable safety using the --unsafe-load-any-extension=y command line option. This feature is undocumented and classified as a hidden option (https://mail.python.org/pipermail/code-quality/2014-November/000439.html).

2) Disable safety using the pylint.rc setting unsafe-load-any-extensions=yes. This is recommended over option 1 and includes full documentation in the default pylint.rc file (created with --generate-rcfile).

3) Specifically list packages or modules names that you trust to be loaded by pylint in the pylint.rc file using the extension-pkg-whitelist= option.

4) Create a plugin to manipulate the AST (I have no idea how to effect this -- but it's regularly discussed on on the pylint mailing list).

We opted for Option 3. We added the following line to our project pylint.rc file:

extension-pkg-whitelist=lxml

@user590028, thanks a lot for your answer! I just ran into this same problem with the libraries win32api, win32evtlog, win32file, win32gui, and win32process, and your solution worked.

I used another method I think is worth posting here, which is to call pylint and pass the whitelisted packages as a parameter:

pylint --extension-pkg-whitelist=win32api,win32evtlog,win32file,win32gui,win32process myfile.py

For those of you using VS Code, it's a bit tricky to find where to put the command as I couldn't find my executable.

In VS Code;

  1. click on File > Preferences > Settings.
  2. Scroll down to "Python Configurations" in the left window
  3. scroll down to "Python Linting: Mypy Args" in the right window
  4. click on "Edit in settings.json" link
  5. edit the json to include: "--extension-pkg-whitelist="

I had to do all this because PyLint isn't executable from my Windows command line...

If you're using VS Code for Mac, this is what you need to do in order to edit the settings.json file:

  1. Click on Code (i.e. the Visual Studio Code tab which is on the left of the 'File' tab) -> Preferences - > Settings
  2. Scroll down to Extensions and click on Python in the list.
  3. Click on any of the Edit in settings.json links. This opens up settings.json for editing.
  4. Add the line "python.linting.pylintArgs": ["----extension-pkg-whitelist=1xml"].
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!