Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_axes.py (Python)

Code before:
    return [{
        'coords': datetimes,
        'vals': [1.2, 2.8, 1.5],
        'mean': 1.84,
        'median': 1.5,
        'min': 1.2,
        'max': 2.8,
        'quantiles': [1.2, 1.5, 2.8]
    }, {
        'coords': datetimes,
        'vals': [0.8, 1.1, 0.9],
        'mean': 0.94,
        'median': 0.9,
        'min': 0.8,
        'max': 1.1,
        'quantiles': [0.8, 0.9, 1.1]
    }]


def test_datetime_positions_with_datetime64():
    """Test that datetime positions with float widths raise TypeError."""
    fig, ax = plt.subplots()
    positions = [np.datetime64('2020-01-01'), np.datetime64('2021-01-01')]
    widths = [0.5, 1.0]
    with pytest.raises(TypeError,
    match="np.datetime64 'position' values, require np.timedelta64 'widths'"):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_datetime_positions_with_float_widths_raises():
    """Test that datetime positions with float widths raise TypeError."""
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)]
    widths = [0.5, 1.0]
    with pytest.raises(TypeError,
    match="datetime/date 'position' values, require timedelta 'widths'"):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_datetime_positions_with_scalar_float_width_raises():
    """Test that datetime positions with scalar float width raise TypeError."""
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)]
    widths = 0.75
    with pytest.raises(TypeError,
    match="datetime/date 'position' values, require timedelta 'widths'"):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_numeric_positions_with_float_widths_ok():
    """Test that numeric positions with float widths work."""

Review comment (refactor):
The match string for pytest.raises is split across two lines without proper line continuation formatting. This could lead to issues if formatting changes or the line is refactored. Consider using proper line continuation with parentheses or joining into a single line for better maintainability and consistency with pytest best practices.

Code after:
    return [{
        'coords': datetimes,
        'vals': [1.2, 2.8, 1.5],
        'mean': 1.84,
        'median': 1.5,
        'min': 1.2,
        'max': 2.8,
        'quantiles': [1.2, 1.5, 2.8]
    }, {
        'coords': datetimes,
        'vals': [0.8, 1.1, 0.9],
        'mean': 0.94,
        'median': 0.9,
        'min': 0.8,
        'max': 1.1,
        'quantiles': [0.8, 0.9, 1.1]
    }]


def test_datetime_positions_with_datetime64():
    """Test that datetime positions with float widths raise TypeError."""
    fig, ax = plt.subplots()
    positions = [np.datetime64('2020-01-01'), np.datetime64('2021-01-01')]
    widths = [0.5, 1.0]
    with pytest.raises(TypeError,
                       match=("np.datetime64 'position' values require "
                              "np.timedelta64 'widths'")):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_datetime_positions_with_float_widths_raises():
    """Test that datetime positions with float widths raise TypeError."""
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)]
    widths = [0.5, 1.0]
    with pytest.raises(TypeError,
                       match=("datetime/date 'position' values require "
                              "timedelta 'widths'")):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_datetime_positions_with_scalar_float_width_raises():
    """Test that datetime positions with scalar float width raise TypeError."""
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)]
    widths = 0.75
    with pytest.raises(TypeError,
                       match=("datetime/date 'position' values require "
                              "timedelta 'widths'")):
        ax.violin(violin_plot_stats(), positions=positions, widths=widths)