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

Code before:
    with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
        fig2 = plt.figure(1, figsize=np.array([3, 4]))
    with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
        plt.figure(fig, figsize=np.array([5, 6]))
    assert fig is fig2
    fig3 = plt.figure(1)  # Checks for false warnings
    assert fig is fig3


def test_close_all_warning():
    fig1 = plt.figure()

    # Check that the warning is issued when 'all' is passed to plt.figure
    with pytest.warns(UserWarning, match="closes all existing figures"):
        fig2 = plt.figure("all")


def test_matshow():
    fig = plt.figure()
    arr = [[0, 1], [1, 2]]

    # Smoke test that matshow does not ask for a new figsize on the existing figure
    plt.matshow(arr, fignum=fig.number)


def assert_signatures_identical(plt_meth, original_meth, remove_self_param=False):
    def get_src(meth):
        meth_src = Path(inspect.getfile(meth))
        meth_stub = meth_src.with_suffix(".pyi")
        return meth_stub if meth_stub.exists() else meth_src

    def tree_loop(tree, name, class_):
        for item in tree.body:
            if class_ and isinstance(item, ast.ClassDef) and item.name == class_:
                return tree_loop(item, name, None)

            if isinstance(item, ast.FunctionDef) and item.name == name:
                return item

        raise ValueError(f"Cannot find {class_}.{name} in ast")

    def get_signature(meth):
        qualname = meth.__qualname__
        class_ = None if "." not in qualname else qualname.split(".")[-2]
        path = get_src(meth)
        tree = ast.parse(path.read_text())
        node = tree_loop(tree, meth.__name__, class_)

        params = dict(inspect.signature(meth).parameters)
        args = node.args
        allargs = (

Review comment (performance):
I don’t have time to check the details, but this functions seems quite heavy-weight now that we only check the really simple function set_loglevel. Do we really bread/want that complexity?

Code after:
        fig2 = plt.figure(1, figsize=np.array([3, 4]))
    with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
        plt.figure(fig, figsize=np.array([5, 6]))
    assert fig is fig2
    fig3 = plt.figure(1)  # Checks for false warnings
    assert fig is fig3


def test_close_all_warning():
    fig1 = plt.figure()

    # Check that the warning is issued when 'all' is passed to plt.figure
    with pytest.warns(UserWarning, match="closes all existing figures"):
        fig2 = plt.figure("all")


def test_matshow():
    fig = plt.figure()
    arr = [[0, 1], [1, 2]]

    # Smoke test that matshow does not ask for a new figsize on the existing figure
    plt.matshow(arr, fignum=fig.number)


def assert_same_signature(func1, func2):
    """
    Assert that `func1` and `func2` have the same arguments,
    i.e. same parameter count, names and kinds.

    :param func1: First function to check
    :param func2: Second function to check
    """
    params1 = inspect.signature(func1).parameters
    params2 = inspect.signature(func2).parameters

    assert len(params1) == len(params2)
    assert all([
        params1[p].name == params2[p].name and
        params1[p].kind == params2[p].kind
        for p in params1
    ])


def test_setloglevel_signature():
    assert_same_signature(plt.set_loglevel, mpl.set_loglevel)