Git Product home page Git Product logo

py-code-standards-eval's Introduction

For formatting according to PEP 8.

example.py is the original, unformatted file

example0.py is the file formatted with autopep8 -i

example1.py is the file formatted with autopep8 -i -a (aggressiveness level 1)

example2.py is the file formatted with autopep8 -i -a -a (aggressiveness level 2)

example3.py is the file formatted with autopep8 -i -a -a -a (aggressiveness level 3)

unsafe*.py follows the same setup. You can see how different aggressiveness levels affect logic.

My preference is formatting with aggressiveness level 3 due to how it shortens lines. However, this comes with the risk of making inadvertent logical changes, e.g. in numpy/pandas, True == np.bool_(True) but not True is np.bool_(True) (see unsafe*.py for more examples). To prevent this from happening, we should ignore certain rules like E711 and E712. Currenlty, I am ignoring E401 and E402 via pyproject.toml because these rules affect imports, which is handled (and much better) by isort.

Relevant links:

For formatting docstrings.

It can also be used to check if docstrings are properly formatted, but pydocstyle is better suited for that purpose.

Does not yet fully support formatting according to conventions such as numpy or Google.

docstrings.py is the original, unformatted file

docstring_fixed.py is the file formatted with:

docformatter -i --wrap-summaries 88 --wrap-descriptions 88

88 matches the max line length configured for autopep8 and flake8.

For checking compliance with docstring conventions.

I have configured pydocstyle to use the google convention. In PyCharm, you can set the docstring convention with Tools > Python Integrated Tools > Docstrings > Docstring format.

Can be used via flake8 with flake8-docstrings, but then the configuration in pyproject.toml will be ignored. Presumably, the configuration would have to be moved to the .flake8 config file.

Relevant links:

For checking PEP 8 compliance.

autopep8 uses the pycodestyle linting tool to determine what parts of the code needs to be formatted. pycodestyle is what you would use to check your code against the style defined by PEP 8. flake8 is just a wrapper around pycodestyle and 2 other checkers: pyflakes and mccabe. It will also automatically use pep8-naming (which checks against PEP 8 naming conventions) when that library is installed:

± flake8 --version
5.0.4 (mccabe: 0.7.0, pep8-naming: 0.13.2, pycodestyle: 2.9.1, pyflakes: 2.5.0) CPython 3.10.6 on Darwin

With this configuration, flake8 passes for example2.py and example3.py.

We may be able to integrate flake8 with SonarCloud. If we cannot, using a different CI tool such as Circle CI or Github Actions for running these checks is the alternative.

Relevant Links:

For formatting imports AND checking if imports are properly formatted.

The configuration in pyproject.toml is based off of the PyCharm profile.

imports.py is the original, unformatted file

imports_sorted.py is the original file formatted with isort

For static type checking.

This is the official type checker for Python. It will warn you when type hints (PEP 484) are being used incorrectly.

While there is a plugin for PyCharm, it does not yet support the latest version of the IDE and PyCharm integrates its own implementation of PEP 484.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.