cosimoNigro / agnpy

Modelling jetted Active Galactic Nuclei radiative processes with python
https://agnpy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
47 stars 32 forks source link

Linting and flaking #106

Closed cosimoNigro closed 2 years ago

cosimoNigro commented 2 years ago

Dear @pawel21,

thanks for helping me with linting and flaking our code. From a bit of reading it seems that there is some overlap between the checks that pylint and flake8 perform.

From a superficial reading, it seems to me that pylint is the more complete, so I would suggest use it as reference.

As you rightly pointed out though, there are a lot of error and warning messages printed, but you can avoid some of them by specifying some settings. For example, by default, variable, attributes and method should all follow the snake_case notation, so there should be no upper case letters. In our case this is a bit difficult as we try to "mimic" the math notation (e.g. the magnetic field is always upper case) or, for example, there is a clear difference between a lower and upper case Lorentz factor gamma. I disabled some of this warning through a .pylintrc file that you can find in this PR. pylint will automatically detect and use it. After that, most of the warnings left were due to trailing whitespaces in the comments - I suggest you use some feature of your text editor to spot and remove them.

To start the global linting I used pylint on the emission_regions/blob.py module. After some corrections this is the output of pylint that I get by running

~/work/agnpy pylint_and_flake8 ❯ pylint agnpy/emission_regions/blob.py                                                                                           Py base 12:33:17
************* Module agnpy.emission_regions.blob
agnpy/emission_regions/blob.py:109:0: C0301: Line too long (101/100) (line-too-long)
agnpy/emission_regions/blob.py:131:0: C0301: Line too long (133/100) (line-too-long)
agnpy/emission_regions/blob.py:133:0: C0301: Line too long (119/100) (line-too-long)
agnpy/emission_regions/blob.py:135:0: C0301: Line too long (124/100) (line-too-long)
agnpy/emission_regions/blob.py:281:0: C0301: Line too long (109/100) (line-too-long)
agnpy/emission_regions/blob.py:290:0: C0301: Line too long (109/100) (line-too-long)
agnpy/emission_regions/blob.py:80:0: R0902: Too many instance attributes (21/7) (too-many-instance-attributes)
agnpy/emission_regions/blob.py:141:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
agnpy/emission_regions/blob.py:141:4: R0913: Too many arguments (11/5) (too-many-arguments)
agnpy/emission_regions/blob.py:183:8: W0201: Attribute 'gamma_size' defined outside __init__ (attribute-defined-outside-init)
agnpy/emission_regions/blob.py:194:8: W0201: Attribute 'gamma_size' defined outside __init__ (attribute-defined-outside-init)
agnpy/emission_regions/blob.py:184:8: W0201: Attribute 'gamma' defined outside __init__ (attribute-defined-outside-init)
agnpy/emission_regions/blob.py:196:8: W0201: Attribute 'gamma' defined outside __init__ (attribute-defined-outside-init)
agnpy/emission_regions/blob.py:192:8: W0201: Attribute 'gamma_min' defined outside __init__ (attribute-defined-outside-init)
agnpy/emission_regions/blob.py:193:8: W0201: Attribute 'gamma_max' defined outside __init__ (attribute-defined-outside-init)

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

much more reduced with respect to initial list of warnings we were getting. I think it's a good score, the warning left are due to too long comment lines (that cannot be split because they contain math expressions or will break the itemising in sphinx. Then there are some warnings on the number of arguments and attributes of a class and where they should be initialised, but I think the last word on these should be on the developers. So I will not disable these warnings.

As a bonus we get also a decent score with flake8. Only warning about the length of the comments are left

~/work/agnpy pylint_and_flake8 ❯ flake8 agnpy/emission_regions/blob.py                                                                                        6s Py base 12:45:15
agnpy/emission_regions/blob.py:3:80: E501 line too long (80 > 79 characters)
agnpy/emission_regions/blob.py:16:80: E501 line too long (81 > 79 characters)
agnpy/emission_regions/blob.py:46:80: E501 line too long (84 > 79 characters)
agnpy/emission_regions/blob.py:48:80: E501 line too long (96 > 79 characters)
agnpy/emission_regions/blob.py:73:80: E501 line too long (84 > 79 characters)
agnpy/emission_regions/blob.py:75:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:83:80: E501 line too long (89 > 79 characters)
agnpy/emission_regions/blob.py:108:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:109:80: E501 line too long (101 > 79 characters)
agnpy/emission_regions/blob.py:131:80: E501 line too long (133 > 79 characters)
agnpy/emission_regions/blob.py:133:80: E501 line too long (119 > 79 characters)
agnpy/emission_regions/blob.py:135:80: E501 line too long (124 > 79 characters)
agnpy/emission_regions/blob.py:178:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:228:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:263:80: E501 line too long (98 > 79 characters)
agnpy/emission_regions/blob.py:272:80: E501 line too long (98 > 79 characters)
agnpy/emission_regions/blob.py:281:80: E501 line too long (109 > 79 characters)
agnpy/emission_regions/blob.py:290:80: E501 line too long (109 > 79 characters)
agnpy/emission_regions/blob.py:318:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:327:80: E501 line too long (80 > 79 characters)
agnpy/emission_regions/blob.py:330:80: E501 line too long (87 > 79 characters)
agnpy/emission_regions/blob.py:339:80: E501 line too long (86 > 79 characters)
agnpy/emission_regions/blob.py:348:80: E501 line too long (97 > 79 characters)
agnpy/emission_regions/blob.py:351:80: E501 line too long (98 > 79 characters)
agnpy/emission_regions/blob.py:356:80: E501 line too long (98 > 79 characters)
agnpy/emission_regions/blob.py:364:80: E501 line too long (82 > 79 characters)
agnpy/emission_regions/blob.py:381:80: E501 line too long (87 > 79 characters)

@pawel can you continue on this branch with linting the rest of the code? I think a score around 8 is acceptable.

sourcery-ai[bot] commented 2 years ago

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.18%.

Quality metrics Before After Change
Complexity 2.19 ⭐ 2.03 ⭐ -0.16 👍
Method Length 49.68 ⭐ 49.63 ⭐ -0.05 👍
Working memory 12.00 😞 11.98 😞 -0.02 👍
Quality 70.54% 🙂 70.72% 🙂 0.18% 👍
Other metrics Before After Change
Lines 310 311 1
Changed files Quality Before Quality After Quality Change
agnpy/emission_regions/blob.py 70.54% 🙂 70.72% 🙂 0.18% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
agnpy/emission_regions/blob.py Blob.__init__ 0 ⭐ 180 😞 26 ⛔ 45.27% 😞 Try splitting into smaller methods. Extract out complex expressions
agnpy/emission_regions/blob.py init_spectrum_norm_dict 11 🙂 135 😞 14 😞 47.35% 😞 Try splitting into smaller methods. Extract out complex expressions
agnpy/emission_regions/blob.py Blob.__str__ 0 ⭐ 105 🙂 26 ⛔ 53.57% 🙂 Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

codecov[bot] commented 2 years ago

Codecov Report

Merging #106 (c67a592) into master (0113497) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #106   +/-   ##
=======================================
  Coverage   96.33%   96.33%           
=======================================
  Files          30       30           
  Lines        2208     2208           
=======================================
  Hits         2127     2127           
  Misses         81       81           
Flag Coverage Δ
unittests 96.33% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
agnpy/emission_regions/blob.py 88.28% <100.00%> (ø)

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 0113497...c67a592. Read the comment docs.

pawel21 commented 2 years ago

@pawel can you continue on this branch with linting the rest of the code?

Yes, I will continue working on this branch.

pawel21 commented 2 years ago

Sorry, maybe a trivial question but I lost. How can I push my change to this branch? I tried git push upstream pylint_and_flake8 where upstream https://github.com/cosimoNigro/agnpy.git and I got the following error: remote: Permission to cosimoNigro/agnpy.git denied to pawel21. fatal: unable to access 'https://github.com/cosimoNigro/agnpy.git/': The requested URL returned error: 403 before I got branch by git checkout -b pylint_and_flake8 upstream/pylint_and_flake8

cosimoNigro commented 2 years ago

Hey @pawel21,

two things come to my mind: 1) if you are working on your fork did you try to update the fork? i.e. did you try to pull the branch pylint_and_flake8 from the upstream? I think this should have been achieved by your git checkout -b pylint_and_flake8 upstream/pylint_and_flake8 but I am not sure, I always synch my origin with the upstream before checking out the branch.

2) try to use ssh instead of https. Since they updated git to work with double authentication I have an ssh key and I use a ssh connection instead of the http.

Let me know if you manage and if I can help you. Cosimo