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

Code before:
    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."""
    fig, ax = plt.subplots()
    positions = [1.0, 2.0]
    widths = [0.5, 1.0]
    ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_mixed_positions_datetime_and_numeric_behaves():
    """Test that mixed datetime and numeric positions
    with float widths raise TypeError.
    """
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), 2.0]
    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_violinplot_bad_positions():
    ax = plt.axes()
    # First 9 digits of frac(sqrt(47))
    np.random.seed(855654600)
    data = [np.random.normal(size=100) for _ in range(4)]
    with pytest.raises(ValueError):
        ax.violinplot(data, positions=range(5))


def test_violinplot_bad_widths():
    ax = plt.axes()
    # First 9 digits of frac(sqrt(53))
    np.random.seed(280109889)
    data = [np.random.normal(size=100) for _ in range(4)]

Review comment (suggestion):
This test creates a figure but doesn't close it. While matplotlib typically handles this, it's better practice to either use a pytest fixture that manages figure lifecycle, or explicitly close the figure to avoid resource leaks in the test suite. Consider adding `plt.close(fig)` after the violin call or using a context manager.
```suggestion
    ax.violin(violin_plot_stats(), positions=positions, widths=widths)
    plt.close(fig)
```

Code after:
    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."""
    fig, ax = plt.subplots()
    positions = [1.0, 2.0]
    widths = [0.5, 1.0]
    ax.violin(violin_plot_stats(), positions=positions, widths=widths)


def test_mixed_positions_datetime_and_numeric_raises():
    """Test that mixed datetime and numeric positions
    with float widths raise TypeError.
    """
    fig, ax = plt.subplots()
    positions = [datetime.datetime(2020, 1, 1), 2.0]
    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_violinplot_bad_positions():
    ax = plt.axes()
    # First 9 digits of frac(sqrt(47))
    np.random.seed(855654600)
    data = [np.random.normal(size=100) for _ in range(4)]
    with pytest.raises(ValueError):
        ax.violinplot(data, positions=range(5))


def test_violinplot_bad_widths():