Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/utils/test_deprecation.py
8424 views
1
from __future__ import annotations
2
3
import inspect
4
from typing import TYPE_CHECKING, Any, get_args
5
6
import pytest
7
8
from polars._typing import DeprecationType
9
from polars._utils.deprecation import (
10
deprecate_nonkeyword_arguments,
11
deprecate_parameter_as_multi_positional,
12
deprecate_renamed_parameter,
13
deprecated,
14
identify_deprecations,
15
issue_deprecation_warning,
16
)
17
18
if TYPE_CHECKING:
19
import sys
20
21
if sys.version_info >= (3, 13):
22
from warnings import deprecated
23
else:
24
from typing_extensions import deprecated # noqa: TC004
25
26
27
def test_issue_deprecation_warning() -> None:
28
with pytest.deprecated_call(match=r"\(Deprecated in version 0\.1\.2\)"):
29
issue_deprecation_warning("deprecated function", version="0.1.2")
30
31
32
def test_deprecate_function() -> None:
33
@deprecated("`hello` is deprecated.")
34
def hello() -> None: ...
35
36
with pytest.deprecated_call():
37
hello()
38
39
40
def test_deprecate_renamed_parameter(recwarn: Any) -> None:
41
@deprecate_renamed_parameter("foo", "oof", version="1.2.3")
42
@deprecate_renamed_parameter("bar", "rab", version="4.5.6")
43
def hello(oof: str, rab: str, ham: str) -> None: ...
44
45
hello(foo="x", bar="y", ham="z") # type: ignore[call-arg]
46
47
assert len(recwarn) == 2
48
assert "oof" in str(recwarn[0].message)
49
assert "rab" in str(recwarn[1].message)
50
51
52
class Foo: # noqa: D101
53
@deprecate_nonkeyword_arguments(allowed_args=["self", "baz"], version="1.0.0")
54
def bar(
55
self, baz: str, ham: str | None = None, foobar: str | None = None
56
) -> None: ...
57
58
59
def test_deprecate_nonkeyword_arguments_method_signature() -> None:
60
# Note the added star indicating keyword-only arguments after 'baz'
61
expected = "(self, baz: 'str', *, ham: 'str | None' = None, foobar: 'str | None' = None) -> 'None'"
62
assert str(inspect.signature(Foo.bar)) == expected
63
64
65
def test_deprecate_nonkeyword_arguments_method_warning() -> None:
66
msg = (
67
r"all arguments of Foo\.bar except for \'baz\' will be keyword-only in the next breaking release."
68
r" Use keyword arguments to silence this warning."
69
)
70
with pytest.deprecated_call(match=msg):
71
Foo().bar("qux", "quox")
72
73
74
def test_deprecate_parameter_as_multi_positional(recwarn: Any) -> None:
75
@deprecate_parameter_as_multi_positional("foo")
76
def hello(*foo: str) -> tuple[str, ...]:
77
return foo
78
79
with pytest.deprecated_call():
80
result = hello(foo="x") # type: ignore[call-arg]
81
assert result == hello("x")
82
83
with pytest.deprecated_call():
84
result = hello(foo=["x", "y"]) # type: ignore[call-arg, arg-type]
85
assert result == hello("x", "y")
86
87
88
def test_deprecate_parameter_as_multi_positional_existing_arg(recwarn: Any) -> None:
89
@deprecate_parameter_as_multi_positional("foo")
90
def hello(bar: int, *foo: str) -> tuple[int, tuple[str, ...]]:
91
return bar, foo
92
93
with pytest.deprecated_call():
94
result = hello(5, foo="x") # type: ignore[call-arg]
95
assert result == hello(5, "x")
96
97
with pytest.deprecated_call():
98
result = hello(5, foo=["x", "y"]) # type: ignore[call-arg, arg-type]
99
assert result == hello(5, "x", "y")
100
101
102
@pytest.mark.slow
103
def test_identify_deprecations() -> None:
104
dep = identify_deprecations()
105
assert isinstance(dep, dict)
106
107
valid_args = get_args(DeprecationType)
108
assert all(key in valid_args for key in dep)
109
110
with pytest.raises(
111
ValueError,
112
match="unrecognised deprecation type 'bitterballen'",
113
):
114
identify_deprecations("bitterballen") # type: ignore[arg-type]
115
116