Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup with pylint #3203

Merged
merged 5 commits into from
Oct 21, 2019
Merged

Code cleanup with pylint #3203

merged 5 commits into from
Oct 21, 2019

Conversation

jngrad
Copy link
Member

@jngrad jngrad commented Sep 25, 2019

Follow-up to #3194

Description of changes:

  • remove unused imports
  • rewrite wildcard imports as explicit imports
  • replace mutable optional parameters by immutable equivalents
  • fix LB thermostat checkpointing mechanism
  • cleanup and simplify conditional statements
  • refactor code with numpy and new espresso methods
  • remove unused variables/arguments
  • rename unused loop variables from i to _
  • fix broken numpy commands in parts of the testsuite that aren't executed
  • remove trailling whitespaces

For wildcard imports, use the tuple import syntax (PEP 328).
Pylint rules: W0401,W0611,W0614
Remove unused variables/arguments and superfluous parentheses,
merge chained calls to `isinstance()`, merge value checks via
`in`, make optional argument values immutable, iterate dict keys
directly, class methods take `cls` instead of `self` as first
argument, use raw strings in regex patterns, etc. Pylint rules:
R1701,R1714,C0201,C0202,C0325,C1801,W0102,W0612,W0613,W1401,W1505
Remove trailing whitespaces and fix indentation.
Pylint rules: W0311,C0303,C0321,C0330
@jngrad jngrad added the Python label Sep 25, 2019
@jngrad jngrad added this to the Espresso 5 milestone Sep 25, 2019
@codecov
Copy link

codecov bot commented Sep 25, 2019

Codecov Report

Merging #3203 into python will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           python   #3203   +/-   ##
======================================
  Coverage      85%     85%           
======================================
  Files         530     530           
  Lines       25795   25795           
======================================
  Hits        22168   22168           
  Misses       3627    3627

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 94d51f5...1ef1aeb. Read the comment docs.

@KaiSzuttor
Copy link
Member

are you sure we want to defer to 5.0? I think backporting bugfixes after the 4.1 release could be harder...

@RudolfWeeber
Copy link
Contributor

RudolfWeeber commented Sep 25, 2019 via email

@jngrad
Copy link
Member Author

jngrad commented Sep 26, 2019

Yes we should defer it to espresso 5. The bugs found here are not executed in our CI pipeline otherwise we would have got syntax errors. I've also not fixed bugs that require more than a one-liner change.

In espresso 4.1, the sources of syntax errors and runtime errors are:

src/python/espressomd/MDA_ESP/__init__.py:194:18:
    E0602: Undefined function 'triclinic_vectors()'
src/config/defines.py:24:4: W0102:
    Dangerous default value [] as argument
testsuite/python/data/gen_coulomb_2d_ref_data.py:18:0:
    W0622: Redefining built-in 'int', 'float', 'object', 'sum', 'any', 'range', ...

edit: fixes for the following bugs have been backported to 4.1 in f8e36f0:

testsuite/python/observables.py:81:32:
    E0602: Undefined function 'average()'
testsuite/python/observables.py:83:32:
    : Incorrect call to function 'sum()'
testsuite/python/tests_common.py:87:0:
    W0102: Dangerous default value [] as argument
testsuite/python/virtual_sites_tracers_common.py:37:4:
    W0102: Dangerous default value [] as argument

jngrad added a commit to jngrad/espresso that referenced this pull request Sep 26, 2019
Patch potential sources of syntax error and runtime errors already
fixed in espresso 5. Pylint rules: E0602,W0102.
@jngrad jngrad mentioned this pull request Sep 27, 2019
@RudolfWeeber
Copy link
Contributor

I'm not opposed to the code changes in this PR.
I'd like to make a general remark though.
I think, the benefit of code beautification not done by automated formatting scripts needs to be weighed really carefully against the hassle caused in terms of merge conflicts.
I'm in favor of removing dangerous stuff such as wildcard imports into the current namespace.
In my opinion, stuff like isinstance refactoring or import sorting does not provide enough benefit, though.
The fact that something is in the pep rules doesn't necessarily mean following the rules is the best use of limited develpoer time.

This is particularly so for future autmated lint checking in PRs. There, rules should be added on an individual basis, only.

@mkuron
Copy link
Member

mkuron commented Sep 28, 2019

I'm in favor of removing dangerous stuff such as wildcard imports into the current namespace.
In my opinion, stuff like isinstance refactoring or import sorting does not provide enough benefit, though.

I agree. New formatting rules should have limited impact on existing code. However, the changes in this pull request are rather small, increase code legibility and unlikely to cause major merge conflicts. So I think it should be merged.

@jngrad
Copy link
Member Author

jngrad commented Sep 29, 2019

@RudolfWeeber thanks for your feedback! Here is my perspective:

  • rules should be added on an individual basis: I strongly support this idea. Hopefully the dedicated issue Python3 linter #3194 will attract feedback that can be used in es meetings to decide on the rules to enforce. Whenever the C++ core is refactored/cleaned-up based on compiler warnings and Clang-format / Clang-tidy rules, we add those to the CMake logic to prevent the re-introduction of bad code. We can do the same with Python, progressively if need be. In Python3 linter #3194 (comment) I break down which of the pylint rules used in this PR should be enforced, and which ones shouldn't.
  • import sorting: I have no strong opinion on whether to enforce it or not. We already enforce it automatically in C++, and it can be done automatically in Python too, so there shouldn't be any loss of developer time if we choose to do it. We should just make sure that espressomd.has_features don't get moved below the import statements, that parenthesis imports don't get transformed to a list of single-import statements and that pylint comments don't get removed.
  • refactoring isinstance and friends: there are several spots in the code, especially in the testsuite, where the same code logic was used despite being inefficient, longer and less legible that the refactored version. Most likely these came from copy-pasting blocks of code "that just work". This is one aspect of technical debt. On quite a few occasions I lost time figuring out the intent of the original author in convoluted code blocks, only to realize from the git history it was just the result of minor fixes piling up in the same spot.
  • code indentation and formatting: these ones cannot be enforced, since pylint does not apply them automatically. They should be done by autopep8, unfortunately it is not a perfect tool. My plan is to manually apply pylint formatting rules to the files already touched by fix_style.sh when creating a commit named "Formatting", so as to avoid wasting time in code reviews.
  • merge conflicts: I agree it's a major issue with such large scale refactors. I'm not a big fan of them, and whenever I do one, I locally merge the most recent PRs to detect merge conflicts, and either reduce the scope of my refactor to exclude the conflicting files, or if I can't, communicate about it in the es meetings (ex: "if any merge conflict occurs in PRs, @jngrad can help" in Vector3d refactor, August 6). Sometimes conflicts go under the radar, recently a bors merge failed because of an extra newline character, and a pointer refactor caused conflicts in your PR and introduced a regression in a lambda. Managing large scale refactors is difficult and time-consuming. At the same time, we need them to improve the global state of espresso. Large-scale formatting PRs like this one are the worst in terms of added value divided by conflict resolving time, that's why it is best to do them after a code freeze. Most of the work has been done, now we just have to select a small subset of those rules we think should be enforced. The other rules can be checked by maintainers periodically, because the issues they detect tend to concentrate in poorly maintained parts of the code like OIF.

@KaiSzuttor
Copy link
Member

maybe we should add a .pylintrc after the discussion is finished.

@jngrad jngrad added the BugFix label Oct 1, 2019
@jngrad
Copy link
Member Author

jngrad commented Oct 1, 2019

I suggest we merge this PR in priority after the end of the summer school.

@jngrad jngrad added the Next label Oct 1, 2019
@jngrad jngrad modified the milestones: Espresso 5, Espresso 4.1.1 Oct 5, 2019
@jngrad
Copy link
Member Author

jngrad commented Oct 10, 2019

@KaiSzuttor @christophlohrmann the NPT thermostat checkpointing might be broken too:

if thmst["type"] == "NPT_ISO":
self.set_npt(kT=thmst["kT"], p_diff=thmst["p_diff"],
piston=thmst["piston"])

does not match the function signature:
def set_npt(self, kT=None, gamma0=None, gammav=None):

@jngrad
Copy link
Member Author

jngrad commented Oct 11, 2019

alright, fixing NPT revealed other bugs in the checkpointing mechanism for integrators. I'll have to open a separate PR, here is not the right place.

@jngrad jngrad modified the milestones: Espresso 4.1.1, Espresso 5 Oct 11, 2019
@jngrad
Copy link
Member Author

jngrad commented Oct 11, 2019

The checkpointing bugfixes have their own PR now. The only relevant bugfix commit left in this PR for 4.1.1 (3b716e5) was cherry-picked in the release candidate. This is now ready for review.

@jngrad
Copy link
Member Author

jngrad commented Oct 21, 2019

bors r=KaiSzuttor

bors bot added a commit that referenced this pull request Oct 21, 2019
3203: Code cleanup with pylint r=KaiSzuttor a=jngrad

Follow-up to #3194

Description of changes:
- remove unused imports
- rewrite wildcard imports as explicit imports
- replace mutable optional parameters by immutable equivalents
- fix LB thermostat checkpointing mechanism
- cleanup and simplify conditional statements
- refactor code with numpy and new espresso methods
- remove unused variables/arguments
- rename unused loop variables from `i` to `_`
- fix broken numpy commands in parts of the testsuite that aren't executed
- remove trailling whitespaces


Co-authored-by: Jean-Noël Grad <[email protected]>
Co-authored-by: Kai Szuttor <[email protected]>
@bors
Copy link
Contributor

bors bot commented Oct 21, 2019

Build succeeded

@bors bors bot merged commit 1ef1aeb into espressomd:python Oct 21, 2019
@jngrad jngrad modified the milestones: Espresso 5, Espresso 4.2 Nov 20, 2019
@jngrad jngrad deleted the pylint-1 branch January 18, 2022 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants