Skip to content

Commit cb0dbd5

Browse files
authored
Merge branch 'main' into 1789-follow-up-xcal-support
2 parents 1a7035a + 60cdd8a commit cb0dbd5

7 files changed

Lines changed: 76 additions & 12 deletions

File tree

‎docs/contribute/index.rst‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Like most free and open source software, icalendar promotes the "free" part, mea
7474
- We don't assign issues.
7575
- Free other contributors from duplicating your effort by opening a pull request as soon as you can.
7676

77-
For people participating in programs to encourage first-time contributions, including `Hacktoberfest <https://hacktoberfest.com/>`_, `Good First Issue <https://goodfirstissue.dev/language/python#repo-2222138>`_, and `Up For Grabs <https://up-for-grabs.net/#/filters?tags=ical%2Cicalendar%2Cics%2Crfc5545>`_, you must comply with their terms and conditions to receive their recognition or rewards.
77+
For people participating in programs to encourage first-time contributions, `Good First Issue <https://goodfirstissue.dev/language/python#repo-2222138>`_ and `Up For Grabs <https://up-for-grabs.net/#/filters?tags=ical%2Cicalendar%2Cics%2Crfc5545>`_, you must comply with their terms and conditions to receive their recognition or rewards.
7878
Accordingly, you may request of the maintainers in your pull request that you would like recognition for your contribution.
7979

8080
.. seealso::
@@ -99,14 +99,6 @@ Accordingly, you may request of the maintainers in your pull request that you wo
9999

100100
Documentation
101101

102-
.. grid-item::
103-
104-
.. button-link:: https://github.com/PyCalendar/icalendar/issues?q=is%3Aissue%20state%3Aopen%20label%3Ahacktoberfest
105-
:color: warning
106-
:shadow:
107-
108-
Hacktoberfest
109-
110102

111103
.. _artificial-intelligence-policy:
112104

‎docs/styles/config/vocabularies/icalendar/accept.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defusedxml
1010
dicts
1111
[Dd]ocstrings?
1212
fuzzer
13-
Hacktoberfest
1413
Hanoka
1514
icalendar(\.[a-z]+)+
1615
iCal

‎news/2093.documentation‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove all mention of Hacktoberfest from documentation and repository labels. @stevepiercy

‎news/2136.bugfix‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :class:`~icalendar.prop.float.vFloat` and :class:`~icalendar.prop.geo.vGeo` to write very small and very large numbers without an exponent, instead of, for example, ``GEO:1e-05;-2e-05``. The FLOAT value type of :rfc:`5545#section-3.3.7` does not allow an exponent. @fermsp

‎src/icalendar/prop/float.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""FLOAT values from :rfc:`5545`."""
22

33
import math
4+
from decimal import Decimal
45
from typing import Any, ClassVar
56
from xml.etree.ElementTree import Element, SubElement
67

@@ -12,6 +13,25 @@
1213
from icalendar.parser.xcal.wrapper import from_xcal_wrapper
1314

1415

16+
def _float_to_ical(value: float) -> str:
17+
"""Convert a float to the ``FLOAT`` notation of :rfc:`5545#section-3.3.7`.
18+
19+
The ``FLOAT`` grammar has no exponent.
20+
Python writes very small and very large floats with one, such as ``1e-05``.
21+
Those values are written with all their digits instead.
22+
23+
Parameters:
24+
value: The float to convert.
25+
26+
Returns:
27+
The float as a string without an exponent.
28+
"""
29+
text = repr(float(value))
30+
if "e" in text:
31+
text = format(Decimal(text), "f")
32+
return text
33+
34+
1535
class vFloat(float):
1636
"""Float
1737
@@ -69,7 +89,7 @@ def __new__(
6989
return self
7090

7191
def to_ical(self) -> bytes:
72-
return str(self).encode("utf-8")
92+
return _float_to_ical(self).encode("utf-8")
7393

7494
@classmethod
7595
def from_ical(cls, ical: str | float) -> Self:

‎src/icalendar/prop/geo.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from icalendar.parser import Parameters
1111
from icalendar.parser.xcal.string import xsd_float_to_string
1212
from icalendar.parser.xcal.wrapper import from_xcal_wrapper
13+
from icalendar.prop.float import _float_to_ical
1314

1415
if TYPE_CHECKING:
1516
from icalendar.compatibility import Self
@@ -108,7 +109,7 @@ def __init__(
108109
self.params = Parameters(params)
109110

110111
def to_ical(self) -> str:
111-
return f"{self.latitude};{self.longitude}"
112+
return f"{_float_to_ical(self.latitude)};{_float_to_ical(self.longitude)}"
112113

113114
@property
114115
def ical_value(self) -> tuple[float, float]:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Regression tests for issue #2136: FLOAT values must not use an exponent."""
2+
3+
import re
4+
5+
import pytest
6+
7+
from icalendar import Event
8+
from icalendar.prop import vFloat, vGeo
9+
10+
RFC_5545_FLOAT = re.compile(r"[+-]?\d+(\.\d+)?")
11+
"""The FLOAT grammar of RFC 5545, section 3.3.7."""
12+
13+
VALUES = [0.00001, -0.00002, 1.5e-07, 1e16, -1e22, 5e-324, 1.7976931348623157e308]
14+
"""Floats that Python writes with an exponent."""
15+
16+
17+
@pytest.mark.parametrize("value", VALUES)
18+
def test_vfloat_follows_the_float_grammar(value):
19+
"""vFloat writes no exponent."""
20+
assert RFC_5545_FLOAT.fullmatch(vFloat(value).to_ical().decode())
21+
22+
23+
@pytest.mark.parametrize("value", VALUES)
24+
def test_vfloat_round_trip(value):
25+
"""The value is the same after writing and parsing it."""
26+
assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value
27+
28+
29+
def test_vfloat_examples():
30+
"""Small and large values are written with all their digits."""
31+
assert vFloat(0.00001).to_ical() == b"0.00001"
32+
assert vFloat(1e16).to_ical() == b"10000000000000000"
33+
34+
35+
def test_vgeo_writes_no_exponent():
36+
"""Coordinates close to zero are written with all their digits."""
37+
assert vGeo((0.00001, -0.00002)).to_ical() == "0.00001;-0.00002"
38+
39+
40+
def test_geo_of_an_event():
41+
"""The GEO property of an event has no exponent."""
42+
event = Event()
43+
event.add("GEO", (0.00001, -0.00002))
44+
assert b"GEO:0.00001;-0.00002" in event.to_ical()
45+
46+
47+
def test_other_values_stay_the_same():
48+
"""Values that had no exponent are written as before."""
49+
assert vFloat(3.1415).to_ical() == b"3.1415"
50+
assert vGeo((37.386013, -122.082932)).to_ical() == "37.386013;-122.082932"

0 commit comments

Comments
 (0)