Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Clean up Python code using Pylint #85

Closed
wants to merge 2 commits into from
Closed

Clean up Python code using Pylint #85

wants to merge 2 commits into from

Conversation

vtomole
Copy link

@vtomole vtomole commented Sep 28, 2018

I've raised the Pylint scores a bit. I didn't do the docstrings because i wasn't sure about them. This is what we have now.

vtomole@vtomole:~/Quantum$ pylint Interoperability/python/qsharp/__init__.py
No config file found, using default configuration
************* Module qsharp.__init__
W:221, 0: TODO: extract operation signature! (fixme)
************* Module qsharp
C:  1, 0: Missing module docstring (missing-docstring)
E: 20, 0: Unable to import 'clr' (import-error)
E: 22, 0: Unable to import 'System.Threading.Tasks' (import-error)
E: 23, 0: Unable to import 'Microsoft.Quantum.Simulation.Simulators' (import-error)
W: 26, 0: Wildcard import qsharp.clr_wrapper (wildcard-import)
E: 39,18: Module 'qsharp' has no 'version' member (no-member)
W: 51, 0: No exception type(s) specified (bare-except)
W: 58, 0: No exception type(s) specified (bare-except)
W: 63, 0: No exception type(s) specified (bare-except)
E: 64, 4: function already defined line 26 (function-redefined)
C: 64, 4: Missing function docstring (missing-docstring)
C:125, 0: Missing function docstring (missing-docstring)
C:146, 4: Missing method docstring (missing-docstring)
R:141, 0: Too few public methods (1/2) (too-few-public-methods)
C:166, 0: Missing class docstring (missing-docstring)
C:175, 4: Missing method docstring (missing-docstring)
C:182, 0: Missing class docstring (missing-docstring)
C:193, 4: Missing method docstring (missing-docstring)
W:203, 8: Redefining built-in 'callable' (redefined-builtin)
C:196, 4: Missing method docstring (missing-docstring)
C:207, 0: Missing class docstring (missing-docstring)
C:214, 0: Missing class docstring (missing-docstring)
C: 31, 0: standard import "import os.path" should be placed before "import clr" (wrong-import-order)
C: 32, 0: standard import "import sys" should be placed before "import clr" (wrong-import-order)

------------------------------------------------------------------
Your code has been rated at 5.98/10 (previous run: 5.98/10, +0.00)
vtomole@vtomole:~/Quantum$ pylint Interoperability/python/qsharp/__main__.py
No config file found, using default configuration
************* Module qsharp.__main__
C:  1, 0: Missing module docstring (missing-docstring)
E: 13, 0: Unable to import 'Microsoft.Quantum.Primitive' (import-error)

------------------------------------------------------------------
Your code has been rated at 1.43/10 (previous run: 1.43/10, +0.00)
vtomole@vtomole:~/Quantum$ pylint Interoperability/python/qsharp/__main__.py
No config file found, using default configuration
************* Module qsharp.__main__
C:  1, 0: Missing module docstring (missing-docstring)
E: 13, 0: Unable to import 'Microsoft.Quantum.Primitive' (import-error)

------------------------------------------------------------------
Your code has been rated at 1.43/10 (previous run: 1.43/10, +0.00)

vtomole@vtomole:~/Quantum$ pylint Interoperability/python/qsharp/clr_wrapper.py
No config file found, using default configuration
************* Module qsharp.clr_wrapper
C:  1, 0: Missing module docstring (missing-docstring)
E: 16, 0: Unable to import 'System' (import-error)
C: 23, 4: Missing function docstring (missing-docstring)
C: 31, 0: Missing class docstring (missing-docstring)
R: 31, 0: Too few public methods (0/2) (too-few-public-methods)
C: 67, 0: Missing class docstring (missing-docstring)
C: 76, 4: Missing method docstring (missing-docstring)
C: 80, 4: Missing method docstring (missing-docstring)
W: 99, 8: No exception type(s) specified (bare-except)
W: 96,19: Missing format attribute 'FullName' in format specifier 'self.clr_type.FullName' (missing-format-attribute)
E:119,33: Class 'method' has no 'GetParameters' member (no-member)
W:181,-1: Missing format attribute 'FullName' in format specifier 'self.clr_type.FullName' (missing-format-attribute)
W:181,-1: Missing format attribute 'Assembly' in format specifier 'self.clr_type.Assembly' (missing-format-attribute)
C:195, 0: Missing function docstring (missing-docstring)

------------------------------------------------------------------
Your code has been rated at 5.22/10 (previous run: 5.22/10, +0.00)
vtomole@vtomole:~/Quantum$ pylint  Interoperability/python/qsharp/tests/test_version.py
No config file found, using default configuration

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
vtomole@vtomole:~/Quantum$ pylint  Interoperability/python/qsharp/tests/test_version.py
No config file found, using default configuration

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

vtomole@vtomole:~/Quantum$ pylint Interoperability/python/qsharp/tomography.py
No config file found, using default configuration
************* Module qsharp.tomography
C:  1, 0: Missing module docstring (missing-docstring)
E: 16, 0: Unable to import 'qinfer' (import-error)
C: 18, 0: Argument name "P" doesn't conform to snake_case naming style (invalid-name)
C: 18, 0: Missing function docstring (missing-docstring)
C: 22, 0: Missing function docstring (missing-docstring)
E: 23, 4: Unable to import 'Microsoft.Quantum.Canon' (import-error)
W: 33, 8: Unused variable 'idx_experiment' (unused-variable)

------------------------------------------------------------------
Your code has been rated at 3.18/10 (previous run: 3.18/10, +0.00)
vtomole@vtomole:~/Quantum$ pylint Interoperability/python/setup.py
No config file found, using default configuration
************* Module setup
C:  1, 0: Missing module docstring (missing-docstring)
C: 18, 0: Function name "pkgPlatform" doesn't conform to snake_case naming style (invalid-name)
C: 18, 0: Missing function docstring (missing-docstring)
E: 30,10: Instance of 'PurePath' has no 'resolve' member (no-member)
C: 38, 0: Missing class docstring (missing-docstring)
C: 59, 0: Missing class docstring (missing-docstring)
W: 83, 8: Attribute 'packages_dir' defined outside __init__ (attribute-defined-outside-init)
C:113, 0: Missing function docstring (missing-docstring)
C:114,43: Variable name "f" doesn't conform to snake_case naming style (invalid-name)
C:121, 0: Missing class docstring (missing-docstring)

------------------------------------------------------------------
Your code has been rated at 8.06/10 (previous run: 8.06/10, +0.00)

@msftclas
Copy link

msftclas commented Sep 28, 2018

CLA assistant check
All CLA requirements met.

@@ -47,27 +49,24 @@
SIMULATOR_COLOR = ca.Fore.BLUE + ca.Back.WHITE
RESET_COLOR = ca.Style.RESET_ALL
except:
ca = None
CA = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casing change ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, yes, but this is a constant providing a fallback for a missing package. PEP8 itself encourages breaking other PEP8 rules if there is a functional purpose served by doing so: https://www.python.org/dev/peps/pep-0008/#id15

Here, we can check ca is None before using colorama features.

SIMULATOR_COLOR = ""
RESET_COLOR = ""

try:
import qutip as qt
except:
qt = None
QT = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Per pep-8 standard: https://www.python.org/dev/peps/pep-0008/#constants constants should be uppercase. This might cause a problem down the road because there won't be a reference to qt when the exception is caught, but the program was going to fail anyway because qt was None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, QuTiP isn't used for all features, so guarding in this way lets us generate more sensible error messages when QuTiP isn't present.


try:
from IPython.display import display
except:
def display(value):
def display():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intention of this piece of code seems to be making the method silent in case the import fails. This change breaks the code if the import fails. Did you test this scenario ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't, but i see why this wouldn't work. Procedures should not be defined with unused arguments. In my opinion, we should not catch anything if from IPython.display import display fails. Maybe put this in a class specifically for jupyter notebooks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @RolfHuisman, this is meant as a fallback to make sure that calls to display fail silently when run outside a Jupyter environment.

@anpaz anpaz requested a review from cgranade October 17, 2018 20:16
@@ -16,6 +16,15 @@
from abc import ABCMeta, abstractproperty
from random import randrange

# CLR Imports #
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I recognize PEP8's and pylint's guidance on import orderings, we must have the append to sys.path appear before import clr, otherwise the DLLs in this module will not be found by the CLR's assembly loader.

@@ -47,27 +49,24 @@
SIMULATOR_COLOR = ca.Fore.BLUE + ca.Back.WHITE
RESET_COLOR = ca.Style.RESET_ALL
except:
ca = None
CA = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, yes, but this is a constant providing a fallback for a missing package. PEP8 itself encourages breaking other PEP8 rules if there is a functional purpose served by doing so: https://www.python.org/dev/peps/pep-0008/#id15

Here, we can check ca is None before using colorama features.

SIMULATOR_COLOR = ""
RESET_COLOR = ""

try:
import qutip as qt
except:
qt = None
QT = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, QuTiP isn't used for all features, so guarding in this way lets us generate more sensible error messages when QuTiP isn't present.


try:
from IPython.display import display
except:
def display(value):
def display():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @RolfHuisman, this is meant as a fallback to make sure that calls to display fail silently when run outside a Jupyter environment.

estimation_results = single_qubit_process_tomography(qsim, noise_channel, n_measurements=2000)
QSIM = qsharp.QuantumSimulator()
NOISE_CHANNEL = QSIM.get(I)
ESTIMATION_RESULTS = single_qubit_process_tomography(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The estimation results are definitely not a constant, and should not be treated as such.

import System

# External Python Imports #

try:
from IPython.display import display
except ImportError:
def display(value):
def display():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here.

@vtomole vtomole closed this Dec 25, 2018
jwittner pushed a commit to jwittner/Quantum that referenced this pull request Dec 16, 2019
jwittner pushed a commit to jwittner/Quantum that referenced this pull request Dec 16, 2019
)

* Updating QDK to latest version. (microsoft#53)

* Fixing doc gen call (microsoft#39)

* fixing doc gen call

* adding a test for docs generation

* need to generate docs for source files only such that we can build the docs for muliple dlls without interferences

* forgot to check if the dictionary exists before clearning it in test

* Updating qdk to latest version

* Fixing vs code extension build script on linux (microsoft#56)

* fixing a stackoverflow for large files (microsoft#55)

* Build VS Code dependencies from either pwsh or powershell. (microsoft#59)

* Add automatic indentation to Visual Studio extension (microsoft#60)

* Add automatic indentation to Visual Studio extension

* Find the indentation of the last non-empty line

The previous line can be empty, which would reset the indentation to 0 if e.g. you pressed enter twice in a row. Loop backwards to find the last non-empty line and use that line's indentation instead.

* Remove assumption from GetLastNonEmptyLine

* Move QsSmartIndentProvider to the top of the file

* Flip order of ternary operator in GetIndentation

* Add doc comment to GetDesiredIndentation

* Add note about nullable return type of GetDesiredIndentation

* Show signature help for the first argument (microsoft#63)

We also trigger signature help on "("

* Always log exceptions from file queries in debug mode (microsoft#70)

* Handling specialization redeclarations (microsoft#85)

* Add install template and new project commands to VS Code extension. (microsoft#79)

* Compile time errors for overflowing int and double literals (microsoft#87)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants