Skip to content

Commit 03ffc32

Browse files
committed
fix deprecation notice
1 parent feac20b commit 03ffc32

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

can/interfaces/ixxat/canlib.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,16 @@ class IXXATBus(BusABC):
398398
@deprecated_args_alias(
399399
deprecation_start="4.3.0",
400400
deprecation_end="5.0.0",
401-
fd="timing",
402-
data_bitrate="timing",
403-
sjw_abr="timing",
404-
tseg1_abr="timing",
405-
tseg2_abr="timing",
406-
sjw_dbr="timing",
407-
tseg1_dbr="timing",
408-
tseg2_dbr="timing",
409-
ssp_dbr="timing",
401+
deprecation_info="Use the `can.BitTimingFd` class and the `timing` parameter instead.",
402+
fd=None,
403+
data_bitrate=None,
404+
sjw_abr=None,
405+
tseg1_abr=None,
406+
tseg2_abr=None,
407+
sjw_dbr=None,
408+
tseg1_dbr=None,
409+
tseg2_dbr=None,
410+
ssp_dbr=None,
410411
)
411412
@deprecated_args_alias(
412413
deprecation_start="4.0.0",

can/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ def channel2int(channel: Optional[typechecking.Channel]) -> Optional[int]:
334334
def deprecated_args_alias(
335335
deprecation_start: str,
336336
deprecation_end: Optional[str] = None,
337+
deprecation_info: Optional[str] = None,
337338
**aliases: Optional[str],
338339
) -> Callable[[Callable[P1, T1]], Callable[P1, T1]]:
339340
"""Allows to rename/deprecate a function kwarg(s) and optionally
@@ -358,6 +359,8 @@ def library_function(new_arg):
358359
The *python-can* version, that introduced the :class:`DeprecationWarning`.
359360
:param deprecation_end:
360361
The *python-can* version, that marks the end of the deprecation period.
362+
:param deprecation_info:
363+
Additional information, which will be added to the deprecation warning..
361364
:param aliases:
362365
keyword arguments, that map the deprecated argument names
363366
to the new argument names or ``None``.
@@ -371,6 +374,7 @@ def wrapper(*args: P1.args, **kwargs: P1.kwargs) -> T1:
371374
func_name=f.__name__,
372375
start=deprecation_start,
373376
end=deprecation_end,
377+
info=deprecation_info,
374378
kwargs=kwargs,
375379
aliases=aliases,
376380
)
@@ -385,6 +389,7 @@ def _rename_kwargs(
385389
func_name: str,
386390
start: str,
387391
end: Optional[str],
392+
info: Optional[str],
388393
kwargs: P1.kwargs,
389394
aliases: Dict[str, Optional[str]],
390395
) -> None:
@@ -410,6 +415,9 @@ def _rename_kwargs(
410415
)
411416
kwargs[new] = value
412417

418+
if info:
419+
deprecation_notice += " " + info.strip()
420+
413421
warnings.warn(deprecation_notice, DeprecationWarning)
414422

415423

test/test_util.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import unittest
44
import warnings
5+
from typing import Optional
56

67
import pytest
78

@@ -20,12 +21,14 @@
2021
class RenameKwargsTest(unittest.TestCase):
2122
expected_kwargs = dict(a=1, b=2, c=3, d=4)
2223

23-
def _test(self, start: str, end: str, kwargs, aliases):
24+
def _test(
25+
self, start: str, end: Optional[str], note: Optional[str], kwargs, aliases
26+
):
2427
# Test that we do get the DeprecationWarning when called with deprecated kwargs
2528
with self.assertWarnsRegex(
2629
DeprecationWarning, "is deprecated.*?" + start + ".*?" + end
2730
):
28-
_rename_kwargs("unit_test", start, end, kwargs, aliases)
31+
_rename_kwargs("unit_test", start, end, note, kwargs, aliases)
2932

3033
# Test that the aliases contains the deprecated values and
3134
# the obsolete kwargs have been removed
@@ -37,30 +40,30 @@ def _test(self, start: str, end: str, kwargs, aliases):
3740
# Cause all warnings to always be triggered.
3841
warnings.simplefilter("error", DeprecationWarning)
3942
try:
40-
_rename_kwargs("unit_test", start, end, kwargs, aliases)
43+
_rename_kwargs("unit_test", start, end, note, kwargs, aliases)
4144
finally:
4245
warnings.resetwarnings()
4346

4447
def test_rename(self):
4548
kwargs = dict(old_a=1, old_b=2, c=3, d=4)
4649
aliases = {"old_a": "a", "old_b": "b"}
47-
self._test("1.0", "2.0", kwargs, aliases)
50+
self._test("1.0", "2.0", None, kwargs, aliases)
4851

4952
def test_obsolete(self):
5053
kwargs = dict(a=1, b=2, c=3, d=4, z=10)
5154
aliases = {"z": None}
52-
self._test("1.0", "2.0", kwargs, aliases)
55+
self._test("1.0", "2.0", None, kwargs, aliases)
5356

5457
def test_rename_and_obsolete(self):
5558
kwargs = dict(old_a=1, old_b=2, c=3, d=4, z=10)
5659
aliases = {"old_a": "a", "old_b": "b", "z": None}
57-
self._test("1.0", "2.0", kwargs, aliases)
60+
self._test("1.0", "2.0", None, kwargs, aliases)
5861

5962
def test_with_new_and_alias_present(self):
6063
kwargs = dict(old_a=1, a=1, b=2, c=3, d=4, z=10)
6164
aliases = {"old_a": "a", "old_b": "b", "z": None}
6265
with self.assertRaises(TypeError):
63-
self._test("1.0", "2.0", kwargs, aliases)
66+
self._test("1.0", "2.0", "Random note.", kwargs, aliases)
6467

6568

6669
class DeprecatedArgsAliasTest(unittest.TestCase):
@@ -130,6 +133,20 @@ def _test_func3(a):
130133
warnings.simplefilter("error")
131134
_test_func3(a=1)
132135

136+
@deprecated_args_alias(
137+
"1.0.0", deprecation_info="Because why not?", old_a="a"
138+
)
139+
def _test_func4(a):
140+
pass
141+
142+
with pytest.warns(DeprecationWarning) as record:
143+
_test_func4(old_a=1)
144+
assert len(record) == 1
145+
assert (
146+
record[0].message.args[0]
147+
== "The 'old_a' argument is deprecated since python-can v1.0.0. Use 'a' instead. Because why not?"
148+
)
149+
133150

134151
class TestBusConfig(unittest.TestCase):
135152
base_config = dict(interface="socketcan", bitrate=500_000)

0 commit comments

Comments
 (0)