Skip to content

Commit 40f1944

Browse files
Merge pull request #834 from niccokunzmann/issue-816
Deprecate the UID Generator
2 parents aae698b + 1d49f01 commit 40f1944

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We use `Semantic Versioning <https://semver.org>`_.
1212

1313
Minor changes:
1414

15-
- Deprecate the UIDGenerator class. See `Issue 816 <https://github.com/collective/icalendar/issues/816>`.
15+
- Deprecate ``icalendar.UIDGenerator``. See `Issue 816 <https://github.com/collective/icalendar/issues/816>`_.
1616

1717
Breaking changes:
1818

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ norecursedirs = [
189189
"src/icalendar/tests/hypothesis",
190190
"build",
191191
]
192+
filterwarnings = [
193+
"ignore::DeprecationWarning",
194+
]
192195

193196
[dependency-groups]
194197
dev = [

src/icalendar/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from icalendar.error import (
2020
ComponentEndMissing,
2121
ComponentStartMissing,
22+
FeatureWillBeRemovedInFutureVersion,
2223
IncompleteAlarmInformation,
2324
IncompleteComponent,
2425
InvalidCalendar,
@@ -121,4 +122,5 @@
121122
"vSkip",
122123
"RELTYPE",
123124
"ROLE",
125+
"FeatureWillBeRemovedInFutureVersion"
124126
]

src/icalendar/error.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,24 @@ class ComponentStartMissing(IncompleteAlarmInformation):
4444
Use Alarms.set_start().
4545
"""
4646

47+
class FeatureWillBeRemovedInFutureVersion(DeprecationWarning):
48+
"""This feature will be removed in a future version."""
49+
50+
51+
class WillBeRemovedInVersion7(FeatureWillBeRemovedInFutureVersion):
52+
"""This feature will be removed in icalendar version 7.
53+
54+
Suppress FeatureWillBeRemovedInFutureVersion instead.
55+
"""
4756

4857
__all__ = [
4958
"InvalidCalendar",
5059
"IncompleteComponent",
5160
"IncompleteAlarmInformation",
5261
"LocalTimezoneMissing",
5362
"ComponentEndMissing",
63+
5464
"ComponentStartMissing",
65+
"FeatureWillBeRemovedInFutureVersion",
66+
"WillBeRemovedInVersion7",
5567
]

src/icalendar/tests/test_unit_tools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ def test_uid_generator_issue_345(args, kw, split, expected):
7979
"""
8080
uid = UIDGenerator.uid(*args, **kw)
8181
assert uid.split(split)[1] == expected
82+
83+
84+
def test_warning():
85+
with pytest.warns(DeprecationWarning):
86+
UIDGenerator.uid()
87+
with pytest.warns(DeprecationWarning):
88+
UIDGenerator.rnd_string()

src/icalendar/tools.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
1+
"""Utility functions for icalendar."""
2+
13
from __future__ import annotations
4+
25
import random
36
from datetime import date, datetime, tzinfo
47
from string import ascii_letters, digits
8+
from warnings import warn
59

610
from icalendar.parser_tools import to_unicode
711

12+
from .error import WillBeRemovedInVersion7
13+
814

915
class UIDGenerator:
10-
"""If you are too lazy to create real uid's."""
16+
"""Use this only if you're too lazy to create real UUIDs.
17+
18+
.. deprecated:: 6.2.1
19+
20+
Use the Python standard library's :func:`uuid.uuid4` instead.
21+
"""
1122

1223
chars = list(ascii_letters + digits)
1324

1425
@staticmethod
15-
def rnd_string(length=16):
16-
"""Generates a string with random characters of length."""
26+
def rnd_string(length=16) -> str:
27+
"""Generates a string with random characters of length.
28+
29+
.. deprecated:: 6.2.1
30+
31+
Use the Python standard library's :func:`uuid.uuid4` instead.
32+
"""
33+
warn(
34+
"Use https://docs.python.org/3/library/uuid.html#uuid.uuid4 instead.",
35+
WillBeRemovedInVersion7,
36+
stacklevel=1
37+
)
1738
return "".join([random.choice(UIDGenerator.chars) for _ in range(length)])
1839

1940
@staticmethod
2041
def uid(host_name="example.com", unique=""):
21-
"""Generates a unique id consisting of:
22-
datetime-uniquevalue@host.
23-
Like:
24-
42+
"""Generates a unique ID consisting of ``datetime-uniquevalue@host``.
43+
44+
For example:
45+
46+
.. code-block:: text
47+
48+
49+
50+
.. deprecated:: 6.2.1
51+
52+
Use the Python standard library's :func:`uuid.uuid5` instead.
2553
"""
2654
from icalendar.prop import vDatetime, vText
55+
warn(
56+
"Use https://docs.python.org/3/library/uuid.html#uuid.uuid5 instead.",
57+
WillBeRemovedInVersion7,
58+
stacklevel=1
59+
)
2760

2861
host_name = to_unicode(host_name)
2962
unique = unique or UIDGenerator.rnd_string()

0 commit comments

Comments
 (0)