Closed prjemian closed 11 months ago
There is another tool to help with naming convention compliance. I'll need to search for this.
pep8-naming will add such compliance-checking to flake8.
Add it with this command
conda install -c conda-forge pep8-naming
Use it with
flake8 python_file.py
Once installed, verify that it is ready to be used:
(bluesky_2023_2) prjemian@zap:~/.../gemviz23/ui_files$ flake8 --version
6.0.0 (mccabe: 0.7.0, pep8-naming: 0.13.3, pycodestyle: 2.10.0, pyflakes: 3.0.1) CPython 3.10.4 on Linux
Here is an example using flake8 with added naming convention checking:
(bluesky_2023_2) prjemian@zap:~/.../gemviz23/ui_files$ flake8 table_window.py
table_window.py:3:80: E501 line too long (88 > 79 characters)
table_window.py:7:1: F401 'typing' imported but unused
table_window.py:8:1: F401 'PyQt5.QtCore' imported but unused
table_window.py:8:1: F401 'PyQt5.QtWidgets' imported but unused
table_window.py:9:1: F403 'from PyQt5.QtWidgets import *' used; unable to detect undefined names
table_window.py:10:1: F403 'from PyQt5.QtCore import *' used; unable to detect undefined names
table_window.py:11:1: F403 'from PyQt5.QtGui import *' used; unable to detect undefined names
table_window.py:12:1: F401 'sys' imported but unused
table_window.py:15:1: F401 'PyQt5.QtWidgets.QWidget' imported but unused
table_window.py:18:8: N801 class name 'Ui_Form' should use CapWords convention
table_window.py:19:10: N802 function name 'setupUi' should be lowercase
table_window.py:19:24: N803 argument name 'Form' should be lowercase
table_window.py:23:26: F405 'QTableView' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:25:36: F405 'QRect' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:26:22: F405 'QFrame' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:29:32: F405 'QRect' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:30:34: F405 'QFrame' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:31:35: F405 'QFrame' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:37:9: F405 'QMetaObject' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
table_window.py:41:10: N802 function name 'retranslateUi' should be lowercase
table_window.py:41:30: N803 argument name 'Form' should be lowercase
table_window.py:42:29: F405 'QCoreApplication' may be undefined, or defined from star imports: PyQt5.QtCore, PyQt5.QtGui, PyQt5.QtWidgets
It is common to use pylint
to check compliance with naming and other coding conventions. Here is an example:
(bluesky_2023_2) jemian@otz ~/.../gemviz23/demo $ pylint textwindow.py
************* Module demo.textwindow
textwindow.py:5:0: E0401: Unable to import 'utils' (import-error)
textwindow.py:6:0: E0611: No name 'QWidget' in module 'PyQt5.QtWidgets' (no-name-in-module)
textwindow.py:26:4: C0116: Missing function or method docstring (missing-function-docstring)
textwindow.py:30:4: C0103: Method name "setText" doesn't conform to snake_case naming style (invalid-name)
textwindow.py:34:4: C0103: Method name "setTitle" doesn't conform to snake_case naming style (invalid-name)
textwindow.py:6:0: C0411: third party import "from PyQt5.QtWidgets import QWidget" should be placed before "import utils" (wrong-import-order)
-----------------------------------
Your code has been rated at 0.67/10
Ooo, brutal assessment.
After running isort
, compliance improves by a factor of 2:
(bluesky_2023_2) jemian@otz ~/.../gemviz23/demo $ isort ./textwindow.py
Fixing /home/beams1/JEMIAN/Documents/projects/BCDA-APS/gemviz23/gemviz23/demo/textwindow.py
(bluesky_2023_2) jemian@otz ~/.../gemviz23/demo $ pylint textwindow.py
************* Module demo.textwindow
textwindow.py:5:0: E0611: No name 'QWidget' in module 'PyQt5.QtWidgets' (no-name-in-module)
textwindow.py:7:0: E0401: Unable to import 'utils' (import-error)
textwindow.py:27:4: C0116: Missing function or method docstring (missing-function-docstring)
textwindow.py:31:4: C0103: Method name "setText" doesn't conform to snake_case naming style (invalid-name)
textwindow.py:35:4: C0103: Method name "setTitle" doesn't conform to snake_case naming style (invalid-name)
------------------------------------------------------------------
Your code has been rated at 1.33/10 (previous run: 0.67/10, +0.67)
I believe we are good enough. Qt naming and Python recommendations are not in agreement. We are trying to use Qt naming here, for some consistency.
Following PEP8, apply naming conventions to the widgets built in the .ui files and also to the Python code we use.