Path: blob/main/py-polars/tests/unit/utils/test_deprecation.py
8424 views
from __future__ import annotations12import inspect3from typing import TYPE_CHECKING, Any, get_args45import pytest67from polars._typing import DeprecationType8from polars._utils.deprecation import (9deprecate_nonkeyword_arguments,10deprecate_parameter_as_multi_positional,11deprecate_renamed_parameter,12deprecated,13identify_deprecations,14issue_deprecation_warning,15)1617if TYPE_CHECKING:18import sys1920if sys.version_info >= (3, 13):21from warnings import deprecated22else:23from typing_extensions import deprecated # noqa: TC004242526def test_issue_deprecation_warning() -> None:27with pytest.deprecated_call(match=r"\(Deprecated in version 0\.1\.2\)"):28issue_deprecation_warning("deprecated function", version="0.1.2")293031def test_deprecate_function() -> None:32@deprecated("`hello` is deprecated.")33def hello() -> None: ...3435with pytest.deprecated_call():36hello()373839def test_deprecate_renamed_parameter(recwarn: Any) -> None:40@deprecate_renamed_parameter("foo", "oof", version="1.2.3")41@deprecate_renamed_parameter("bar", "rab", version="4.5.6")42def hello(oof: str, rab: str, ham: str) -> None: ...4344hello(foo="x", bar="y", ham="z") # type: ignore[call-arg]4546assert len(recwarn) == 247assert "oof" in str(recwarn[0].message)48assert "rab" in str(recwarn[1].message)495051class Foo: # noqa: D10152@deprecate_nonkeyword_arguments(allowed_args=["self", "baz"], version="1.0.0")53def bar(54self, baz: str, ham: str | None = None, foobar: str | None = None55) -> None: ...565758def test_deprecate_nonkeyword_arguments_method_signature() -> None:59# Note the added star indicating keyword-only arguments after 'baz'60expected = "(self, baz: 'str', *, ham: 'str | None' = None, foobar: 'str | None' = None) -> 'None'"61assert str(inspect.signature(Foo.bar)) == expected626364def test_deprecate_nonkeyword_arguments_method_warning() -> None:65msg = (66r"all arguments of Foo\.bar except for \'baz\' will be keyword-only in the next breaking release."67r" Use keyword arguments to silence this warning."68)69with pytest.deprecated_call(match=msg):70Foo().bar("qux", "quox")717273def test_deprecate_parameter_as_multi_positional(recwarn: Any) -> None:74@deprecate_parameter_as_multi_positional("foo")75def hello(*foo: str) -> tuple[str, ...]:76return foo7778with pytest.deprecated_call():79result = hello(foo="x") # type: ignore[call-arg]80assert result == hello("x")8182with pytest.deprecated_call():83result = hello(foo=["x", "y"]) # type: ignore[call-arg, arg-type]84assert result == hello("x", "y")858687def test_deprecate_parameter_as_multi_positional_existing_arg(recwarn: Any) -> None:88@deprecate_parameter_as_multi_positional("foo")89def hello(bar: int, *foo: str) -> tuple[int, tuple[str, ...]]:90return bar, foo9192with pytest.deprecated_call():93result = hello(5, foo="x") # type: ignore[call-arg]94assert result == hello(5, "x")9596with pytest.deprecated_call():97result = hello(5, foo=["x", "y"]) # type: ignore[call-arg, arg-type]98assert result == hello(5, "x", "y")99100101@pytest.mark.slow102def test_identify_deprecations() -> None:103dep = identify_deprecations()104assert isinstance(dep, dict)105106valid_args = get_args(DeprecationType)107assert all(key in valid_args for key in dep)108109with pytest.raises(110ValueError,111match="unrecognised deprecation type 'bitterballen'",112):113identify_deprecations("bitterballen") # type: ignore[arg-type]114115116