Skip to content

Commit 59fa807

Browse files
Merge pull request #785 from Horisyre/Adding-params-to-init
added params argument to class constructors
2 parents 742415e + 58bfda2 commit 59fa807

File tree

4 files changed

+140
-32
lines changed

4 files changed

+140
-32
lines changed

CHANGES.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
Changelog
22
=========
3+
6.1.2 (unreleased 2025-03-19)
4+
------------------
5+
6+
Minor changes:
7+
8+
- Set default value for ``params`` as ``params={}`` in mulitple constructors in ``icalendar.prop`` to improve usability.
9+
- Improve object initialization performance in ``icalendar.prop``.
10+
- Add type hint for ``params`` in multiple constructors in ``icalendar.prop``.
11+
12+
Breaking changes:
13+
14+
- ...
15+
16+
New features:
17+
18+
- ...
19+
20+
Bug fixes:
21+
22+
- ...
323

424
6.1.2 (unreleased)
525
------------------

docs/credits.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ icalendar contributors
7878
- `Soham Dutta <https://github.com/NP-compete>`_
7979
- `Serif OZ <https://github.com/SerifOZ>`_
8080
- David Venhoff <https://github.com/david-venhoff>
81+
- `Tariq <https://github.com/Horisyre>`_
8182

8283
Find out who contributed::
8384

src/icalendar/prop.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class vBoolean(int):
140140

141141
BOOL_MAP = CaselessDict({'true': True, 'false': False})
142142

143-
def __new__(cls, *args, **kwargs):
143+
def __new__(cls, *args,params={}, **kwargs):
144144
self = super().__new__(cls, *args, **kwargs)
145-
self.params = Parameters()
145+
self.params =Parameters(params)
146146
return self
147147

148148
def to_ical(self):
@@ -162,11 +162,11 @@ class vText(str):
162162

163163
params: Parameters
164164

165-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
165+
def __new__(cls, value, encoding=DEFAULT_ENCODING,params={}):
166166
value = to_unicode(value, encoding=encoding)
167167
self = super().__new__(cls, value)
168168
self.encoding = encoding
169-
self.params = Parameters()
169+
self.params=Parameters(params)
170170
return self
171171

172172
def __repr__(self) -> str:
@@ -222,10 +222,10 @@ class vCalAddress(str):
222222

223223
params: Parameters
224224

225-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
225+
def __new__(cls, value, encoding=DEFAULT_ENCODING,params={}):
226226
value = to_unicode(value, encoding=encoding)
227227
self = super().__new__(cls, value)
228-
self.params = Parameters()
228+
self.params =Parameters(params)
229229
return self
230230

231231
def __repr__(self):
@@ -302,9 +302,9 @@ class vFloat(float):
302302

303303
params: Parameters
304304

305-
def __new__(cls, *args, **kwargs):
305+
def __new__(cls,*args, params={}, **kwargs):
306306
self = super().__new__(cls, *args, **kwargs)
307-
self.params = Parameters()
307+
self.params = Parameters(params)
308308
return self
309309

310310
def to_ical(self):
@@ -369,9 +369,9 @@ class vInt(int):
369369

370370
params: Parameters
371371

372-
def __new__(cls, *args, **kwargs):
372+
def __new__(cls, *args,params={}, **kwargs):
373373
self = super().__new__(cls, *args, **kwargs)
374-
self.params = Parameters()
374+
self.params = Parameters(params)
375375
return self
376376

377377
def to_ical(self) -> bytes:
@@ -430,11 +430,11 @@ class vCategory:
430430

431431
params: Parameters
432432

433-
def __init__(self, c_list):
433+
def __init__(self, c_list, params={}):
434434
if not hasattr(c_list, '__iter__') or isinstance(c_list, str):
435435
c_list = [c_list]
436436
self.cats = [vText(c) for c in c_list]
437-
self.params = Parameters()
437+
self.params = Parameters(params)
438438

439439
def __iter__(self):
440440
return iter(vCategory.from_ical(self.to_ical()))
@@ -624,9 +624,9 @@ class vDatetime(TimeBase):
624624

625625
params: Parameters
626626

627-
def __init__(self, dt):
627+
def __init__(self, dt, params={}):
628628
self.dt = dt
629-
self.params = Parameters()
629+
self.params = Parameters(params)
630630

631631
def to_ical(self):
632632
dt = self.dt
@@ -761,11 +761,11 @@ class vDuration(TimeBase):
761761

762762
params: Parameters
763763

764-
def __init__(self, td):
764+
def __init__(self, td,params={}):
765765
if not isinstance(td, timedelta):
766766
raise ValueError('Value MUST be a timedelta instance')
767767
self.td = td
768-
self.params = Parameters()
768+
self.params = Parameters(params)
769769

770770
def to_ical(self):
771771
sign = ""
@@ -988,7 +988,7 @@ class vWeekday(str):
988988
"SU": 0, "MO": 1, "TU": 2, "WE": 3, "TH": 4, "FR": 5, "SA": 6,
989989
})
990990

991-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
991+
def __new__(cls, value, encoding=DEFAULT_ENCODING,params={}):
992992
value = to_unicode(value, encoding=encoding)
993993
self = super().__new__(cls, value)
994994
match = WEEKDAY_RULE.match(self)
@@ -1004,7 +1004,7 @@ def __new__(cls, value, encoding=DEFAULT_ENCODING):
10041004
self.relative = relative and int(relative) or None
10051005
if sign == '-' and self.relative:
10061006
self.relative *= -1
1007-
self.params = Parameters()
1007+
self.params = Parameters(params)
10081008
return self
10091009

10101010
def to_ical(self):
@@ -1034,12 +1034,12 @@ class vFrequency(str):
10341034
"YEARLY": "YEARLY",
10351035
})
10361036

1037-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
1037+
def __new__(cls, value, encoding=DEFAULT_ENCODING, params={}):
10381038
value = to_unicode(value, encoding=encoding)
10391039
self = super().__new__(cls, value)
10401040
if self not in vFrequency.frequencies:
10411041
raise ValueError(f'Expected frequency, got: {self}')
1042-
self.params = Parameters()
1042+
self.params = Parameters(params)
10431043
return self
10441044

10451045
def to_ical(self):
@@ -1083,7 +1083,7 @@ class vMonth(int):
10831083

10841084
params: Parameters
10851085

1086-
def __new__(cls, month:Union[str, int]):
1086+
def __new__(cls, month:Union[str, int], params={}):
10871087
if isinstance(month, vMonth):
10881088
return cls(month.to_ical().decode())
10891089
if isinstance(month, str):
@@ -1100,7 +1100,7 @@ def __new__(cls, month:Union[str, int]):
11001100
month_index = int(month)
11011101
self = super().__new__(cls, month_index)
11021102
self.leap = leap
1103-
self.params = Parameters()
1103+
self.params = Parameters(params)
11041104
return self
11051105

11061106
def to_ical(self) -> bytes:
@@ -1249,12 +1249,12 @@ class vRecur(CaselessDict):
12491249
'SKIP': vSkip,
12501250
})
12511251

1252-
def __init__(self, *args, **kwargs):
1252+
def __init__(self, *args,params={}, **kwargs):
12531253
for k, v in kwargs.items():
12541254
if not isinstance(v, SEQUENCE_TYPES):
12551255
kwargs[k] = [v]
12561256
super().__init__(*args, **kwargs)
1257-
self.params = Parameters()
1257+
self.params = Parameters(params)
12581258

12591259
def to_ical(self):
12601260
result = []
@@ -1477,10 +1477,10 @@ class vUri(str):
14771477

14781478
params: Parameters
14791479

1480-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
1480+
def __new__(cls, value, encoding=DEFAULT_ENCODING, params={}):
14811481
value = to_unicode(value, encoding=encoding)
14821482
self = super().__new__(cls, value)
1483-
self.params = Parameters()
1483+
self.params = Parameters(params)
14841484
return self
14851485

14861486
def to_ical(self):
@@ -1555,7 +1555,7 @@ class vGeo:
15551555

15561556
params: Parameters
15571557

1558-
def __init__(self, geo: tuple[float|str|int, float|str|int]):
1558+
def __init__(self, geo: tuple[float|str|int, float|str|int], params={}):
15591559
"""Create a new vGeo from a tuple of (latitude, longitude).
15601560
15611561
Raises:
@@ -1570,7 +1570,7 @@ def __init__(self, geo: tuple[float|str|int, float|str|int]):
15701570
"latitude and longitude") from e
15711571
self.latitude = latitude
15721572
self.longitude = longitude
1573-
self.params = Parameters()
1573+
self.params = Parameters(params)
15741574

15751575
def to_ical(self):
15761576
return f"{self.latitude};{self.longitude}"
@@ -1646,11 +1646,11 @@ class vUTCOffset:
16461646
# it, rather than let the exception
16471647
# propagate upwards
16481648

1649-
def __init__(self, td):
1649+
def __init__(self, td, params={}):
16501650
if not isinstance(td, timedelta):
16511651
raise ValueError('Offset value MUST be a timedelta instance')
16521652
self.td = td
1653-
self.params = Parameters()
1653+
self.params = Parameters(params)
16541654

16551655
def to_ical(self):
16561656

@@ -1711,10 +1711,10 @@ class vInline(str):
17111711

17121712
params: Parameters
17131713

1714-
def __new__(cls, value, encoding=DEFAULT_ENCODING):
1714+
def __new__(cls, value, encoding=DEFAULT_ENCODING, params={}):
17151715
value = to_unicode(value, encoding=encoding)
17161716
self = super().__new__(cls, value)
1717-
self.params = Parameters()
1717+
self.params = Parameters(params)
17181718
return self
17191719

17201720
def to_ical(self):
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
from icalendar.prop import vBoolean,vInline , vUTCOffset, vCategory, vCalAddress, vWeekday, vDuration, vFloat, vGeo, vInt, vText, vMonth, vUTCOffset, vFrequency, vRecur, vDatetime, vUri
3+
import datetime
4+
5+
def test_param_vCategory():
6+
obj = vCategory(["Work", "Personal"], params={"SOME_PARAM":"VALUE"})
7+
assert isinstance(obj, vCategory)
8+
assert obj.params["SOME_PARAM"]=="VALUE"
9+
10+
def test_param_vCalAddress():
11+
obj = vCalAddress('mailto:[email protected]',params={"SOME_PARAM":"VALUE"})
12+
assert isinstance(obj, vCalAddress)
13+
assert obj.params["SOME_PARAM"]=="VALUE"
14+
15+
def test_param_vWeekday():
16+
obj = vWeekday("2FR",params={"SOME_PARAM":"VALUE"})
17+
assert isinstance(obj, vWeekday)
18+
assert obj.params["SOME_PARAM"]=="VALUE"
19+
20+
def test_param_vBoolean():
21+
22+
obj = vBoolean(True, params={"SOME_PARAM":"VALUE"})
23+
assert isinstance(obj, vBoolean)
24+
assert obj.params["SOME_PARAM"]=="VALUE"
25+
26+
def test_param_vDuration():
27+
td = datetime.timedelta(days=15, seconds=18020)
28+
obj = vDuration(td, params={"SOME_PARAM":"VALUE"})
29+
assert isinstance(obj, vDuration)
30+
assert obj.params["SOME_PARAM"]=="VALUE"
31+
32+
def test_param_vFloat():
33+
obj = vFloat('1.333',params={"SOME_PARAM":"VALUE"})
34+
assert isinstance(obj, vFloat)
35+
assert obj.params["SOME_PARAM"]=="VALUE"
36+
37+
def test_param_vGeo():
38+
obj = vGeo((37.386013, -122.082932),params={"SOME_PARAM":"VALUE"})
39+
assert isinstance(obj, vGeo)
40+
assert obj.params["SOME_PARAM"]=="VALUE"
41+
42+
def test_param_vInt():
43+
obj = vInt('87',params={"SOME_PARAM":"VALUE"})
44+
assert isinstance(obj, vInt)
45+
assert obj.params["SOME_PARAM"]=="VALUE"
46+
47+
def test_param_vInline():
48+
obj = vInline("sometxt", params={"SOME_PARAM":"VALUE"})
49+
assert isinstance(obj, vInline)
50+
assert obj.params["SOME_PARAM"]=="VALUE"
51+
52+
def test_param_vText():
53+
obj = vText("sometxt", params={"SOME_PARAM":"VALUE"})
54+
assert isinstance(obj, vText)
55+
assert obj.params["SOME_PARAM"]=="VALUE"
56+
57+
def test_param_vMonth():
58+
obj = vMonth(1,params={"SOME_PARAM":"VALUE"})
59+
assert isinstance(obj, vMonth)
60+
assert obj.params["SOME_PARAM"]=="VALUE"
61+
62+
def test_param_vUTCOffset():
63+
obj = vUTCOffset(datetime.timedelta(days=-1, seconds=68400),params={"SOME_PARAM":"VALUE"})
64+
assert isinstance(obj, vUTCOffset)
65+
assert obj.params["SOME_PARAM"]=="VALUE"
66+
67+
def test_param_vFrequency():
68+
obj = vFrequency("DAILY",params={"SOME_PARAM":"VALUE"})
69+
assert isinstance(obj, vFrequency)
70+
assert obj.params["SOME_PARAM"]=="VALUE"
71+
72+
73+
def test_param_vRecur():
74+
obj = vRecur({'FREQ': ['DAILY'], 'COUNT': [10]}, params={"SOME_PARAM":"VALUE"})
75+
assert isinstance(obj,vRecur)
76+
assert obj.params["SOME_PARAM"]=="VALUE"
77+
78+
def test_param_vDatetime():
79+
dt = datetime.datetime(2025, 3, 16, 14, 30, 0, tzinfo=datetime.timezone.utc)
80+
obj = vDatetime(dt,params={"SOME_PARAM":"VALUE"})
81+
assert isinstance(obj, vDatetime)
82+
assert obj.params["SOME_PARAM"]=="VALUE"
83+
84+
def test_param_vUri():
85+
obj = vUri("WWW.WESBITE.COM",params={"SOME_PARAM":"VALUE"})
86+
assert isinstance(obj, vUri)
87+
assert obj.params["SOME_PARAM"]=="VALUE"

0 commit comments

Comments
 (0)