helpers icon indicating copy to clipboard operation
helpers copied to clipboard

Debug why Linter is only partially making formatting changes

Open indrayudd opened this issue 7 months ago • 1 comments

The following code is not being fully formatted by Linter:

from typing import Any, Dict, List, Optional, Tuple

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon

# Notebook styling.
sns.set_theme(style="whitegrid")


# #############################################################################
# Section Header with no gap after
# #############################################################################
def plot_choropleth_map(
    style: str = "ggplot",
    figsize: Tuple[int, int] = (15, 10),
    dpi: int = 100,
) -> Tuple[matplotlib.figure.Figure, matplotlib.axes.Axes]:
    """
    Plot a choropleth given a list of patch objects and corresponding values.

    :param style: style name for plot
    :param figsize: figure dimensions
    :param dpi: dots per inch resolution
    :return: choropleth map
    """
    if style:
        plt.style.use(style)
    fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
    # <-- Imagine a lot of code here --->


    # Intentional blank space above.
    ax.set_aspect("equal")
    ax.axis("off")
    plt.tight_layout()
    return fig, ax

On running Linter, the script changes to:

from typing import Tuple

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

# Notebook styling.
sns.set_theme(style="whitegrid")


# #############################################################################
# Section Header with no gap after
# #############################################################################
def plot_choropleth_map(
    style: str = "ggplot",
    figsize: Tuple[int, int] = (15, 10),
    dpi: int = 100,
) -> Tuple[matplotlib.figure.Figure, matplotlib.axes.Axes]:
    """
    Plot a choropleth given a list of patch objects and corresponding values.

    :param style: style name for plot
    :param figsize: figure dimensions
    :param dpi: dots per inch resolution
    :return: choropleth map
    """
    if style:
        plt.style.use(style)
    fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
    # <-- Imagine a lot of code here --->

    # Intentional blank space above.
    ax.set_aspect("equal")
    ax.axis("off")
    plt.tight_layout()
    return fig, ax

The irrelevant imports are being removed, which means some editing is happening; but the other formatting steps don't seem to be working.

Here's the terminal output:

Linting file: 'bugged.py'
////////////////////////////////////////////////////////////////////////////////
./linter_warnings.txt
////////////////////////////////////////////////////////////////////////////////
cmd line='./linters/base.py --files bugged.py --num_threads serial'
file_paths=1 ['bugged.py']
actions=25 ['add_python_init_files', 'add_toc_to_notebook', 'fix_md_links', 'lint_md', 'check_md_toc_headers', 'autoflake', 'fix_whitespaces', 'doc_formatter', 'isort', 'class_method_order', 'normalize_imports', 'format_separating_line', 'add_class_frames', 'remove_empty_lines_in_function', 'black', 'process_jupytext', 'check_file_size', 'check_filename', 'check_merge_conflict', 'check_import', 'warn_incorrectly_formatted_todo', 'check_md_reference', 'flake8', 'pylint', 'mypy']
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

FYI @sonniki

indrayudd avatar May 02 '25 02:05 indrayudd

  • The empty line should be removed by the amp_remove_empty_lines_in_function step. We should add a test for it reproducing this use case and debug.

  • Not sure which step is responsible for inserting an empty line in here:

# #############################################################################
def plot_choropleth_map(

Probably one of the 3rd-party tool wrappers, like amp_black. We should also add a test case (can be in the general Linter test file, linters/test/test_amp_dev_scripts.py) to confirm the behavior and debug. If we find that this case is genuinely not being formatted, we can take care of it in amp_format_separating_line.py.

sonniki avatar May 02 '25 06:05 sonniki