macxred / cashctrl_ledger

Implementation of the abstract pyledger.LegderEngine interface with CashCtrl accounting service.
MIT License
0 stars 0 forks source link

Implement automated Linting as a GitHub action #48

Closed AlexTheWizardL closed 4 weeks ago

AlexTheWizardL commented 3 months ago

This issue includes the following steps:

  1. Find a package that performs linting operations and is used across multiple open-source projects.
  2. Decide on code style and limiting rules we want our code to adhere to.
    • @AlexTheWizardL will research common linting rules and present in to @lasuk
  3. Implement linting rules.
    • Apply chosen linting rules to the package
    • Implement improvement, clarifications, and coherence in the code base, docstrings, and documentation.
  4. Create a GitHub action that will perform code checks.
    • Triggers for this action should be the same as for the "Testing" action.
    • "Linting" action should be performed at the first order and stop all remaining actions if it fails.
  5. Align code base with new rules
Here are some examples of open-source projects that use specific packages for linting, along with the rules they employ: ### 1. **Django** **Linter**: `flake8` **Rules**: - Django uses a custom `setup.cfg` configuration file for `flake8`. - Example configuration: ```ini [flake8] max-line-length = 119 exclude = .tox,build,dist ``` ### 2. **NumPy** **Linter**: `flake8` **Plugins**: `flake8-docstrings`, `flake8-bugbear` **Rules**: - Example configuration in `setup.cfg`: ```ini [flake8] max-line-length = 88 ignore = E203, E266, E501, W503, F403, F401 select = B,C,E,F,W,T4 docstring-convention = numpy ``` ### 3. **Pandas** **Linter**: `flake8` **Plugins**: `flake8-bugbear`, `flake8-comprehensions` **Rules**: - Example configuration in `setup.cfg`: ```ini [flake8] max-line-length = 88 ignore = E203, E501, W503 exclude = .git,__pycache__,docs/source/conf.py,old,build,dist ``` ### 4. **Scikit-learn** **Linter**: `flake8` **Plugins**: `flake8-docstrings`, `flake8-import-order` **Rules**: - Example configuration in `setup.cfg`: ```ini [flake8] max-line-length = 88 ignore = E203, E266, E501, W503 exclude = .git,__pycache__,docs/source/conf.py,old,build,dist select = B,C,E,F,W,T4 docstring-convention = numpy [flake8:import-order] application-import-names = sklearn ``` ### 5. **Requests** **Linter**: `flake8` **Rules**: - Example configuration in `setup.cfg`: ```ini [flake8] max-line-length = 120 exclude = docs/conf.py ``` ### Summary of Common Linter Configurations: 1. **max-line-length**: Varies from 88 to 120. 2. **ignore**: Commonly ignored rules include `E203`, `E266`, `E501`, `W503`. 3. **exclude**: Often excludes directories like `.git`, `__pycache__`, `build`, `dist`, and documentation configuration files. 4. **select**: Commonly selected rules include `B` (bugbear), `C` (complexity), `E` (errors), `F` (flake8-builtins), `W` (warnings), `T4` (type annotations). ### How to Configure Your Project You can create a `setup.cfg` or `.flake8` file at the root of your project and configure it similarly to the examples provided above. Adjust the rules and plugins based on your specific project needs and coding style preferences. Here's a template you can start with: ```ini [flake8] max-line-length = 88 ignore = E203, E266, E501, W503 exclude = .git,__pycache__,docs/source/conf.py,old,build,dist select = B,C,E,F,W,T4 docstring-convention = numpy [flake8:import-order] application-import-names = your_project_name ``` ### Running `flake8` To run `flake8` with your configuration, simply execute: ```bash flake8 ```

Linter should be included for the cashctrl_api and cashctrl_ledger packages

AlexTheWizardL commented 3 months ago

Decisions on the tools and rules:

AlexTheWizardL commented 3 months ago

Step-by-Step Guide for Implementing Google Style Guide with Flake8

Step 1: Install Flake8 and Required Plugins

Install Flake8 and the necessary plugins for linting:

pip install flake8 flake8-import-order flake8-docstrings flake8-bugbear

Motivation: These tools help enforce coding standards, detect errors, and improve code quality.

Step 2: Create a .flake8 Configuration File

Create a .flake8 file in the root directory of your project with the following content:

[flake8]
max-line-length = 100
ignore = D100,D101,D102,D103,D104,D105,D107,D202,D203,D204,D205,D400,D401,D402,D403,D404,D405,D406,D407,D408,D409,D410,D411,D412,D413,D414,E203,W503
import-order-style = google
docstring-convention = google
exclude = .git,__pycache__,.pytest_cache,*.egg-info

Motivation:

Step 3: Configure Development Dependencies

If using setup.py, add development dependencies for easy setup:

from setuptools import setup, find_packages

setup(
    name="cashctrl_api",
    version="0.0.1",
    description="Python client for the CashCtrl REST API",
    url="https://github.com/macxred/cashctrl_api",
    author="Lukas Elmiger",
    python_requires=">=3.9",
    install_requires=["requests", "pandas"],
    packages=find_packages(exclude=("tests", "examples")),
    extras_require={
        "dev": [
            "flake8",
            "flake8-import-order",
            "flake8-docstrings",
            "flake8-bugbear"
        ]
    }
)

Motivation: Ensures all developers have the necessary tools for linting and quality checks.

Step 4: Run Flake8

To check your code, run:

flake8

Motivation: Runs linting checks against your codebase to identify and fix style violations and potential errors.

Code example

Here is the commit that shows code aligned with provided linter rules: cashctrl_api#3887b60

Summary

By following these steps and implementing the suggested configurations, code will adhere to the Google Python Style Guide, promoting consistency, readability, and maintainability. This setup helps ensure that all contributors follow the same coding standards, making collaboration more efficient and the codebase more reliable.