Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2136.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :class:~icalendar.prop.float.vFloat and :class:~icalendar.prop.geo.vGeo writing very small and very large numbers with an exponent, such as GEO:1e-05;-2e-05. The FLOAT value type of :rfc:5545#section-3.3.7 does not allow an exponent. @fermsp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your quick response and contribution.

Let's polish up the reStructuredText in this news fragment so that it links. You can check how it renders in the pull request preview under the CI check for docs/readthedocs.org:icalendar under Reference > Change log.

Suggested change
Fixed :class:~icalendar.prop.float.vFloat and :class:~icalendar.prop.geo.vGeo writing very small and very large numbers with an exponent, such as GEO:1e-05;-2e-05. The FLOAT value type of :rfc:5545#section-3.3.7 does not allow an exponent. @fermsp
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

Otherwise this looks good to me, and I'll pass it over to a maintainer for final review.

22 changes: 21 additions & 1 deletion src/icalendar/prop/float.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""FLOAT values from :rfc:`5545`."""

import math
from decimal import Decimal
from typing import Any, ClassVar
from xml.etree.ElementTree import Element, SubElement

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


def _float_to_ical(value: float) -> str:
"""Convert a float to the ``FLOAT`` notation of :rfc:`5545#section-3.3.7`.

The ``FLOAT`` grammar has no exponent.
Python writes very small and very large floats with one, such as ``1e-05``.
Those values are written with all their digits instead.

Parameters:
value: The float to convert.

Returns:
The float as a string without an exponent.
"""
text = repr(float(value))
if "e" in text:
text = format(Decimal(text), "f")
return text


class vFloat(float):
"""Float

Expand Down Expand Up @@ -69,7 +89,7 @@ def __new__(
return self

def to_ical(self) -> bytes:
return str(self).encode("utf-8")
return _float_to_ical(self).encode("utf-8")

@classmethod
def from_ical(cls, ical: str | float) -> Self:
Expand Down
3 changes: 2 additions & 1 deletion src/icalendar/prop/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from icalendar.parser import Parameters
from icalendar.parser.xcal.string import xsd_float_to_string
from icalendar.parser.xcal.wrapper import from_xcal_wrapper
from icalendar.prop.float import _float_to_ical

if TYPE_CHECKING:
from icalendar.compatibility import Self
Expand Down Expand Up @@ -108,7 +109,7 @@ def __init__(
self.params = Parameters(params)

def to_ical(self) -> str:
return f"{self.latitude};{self.longitude}"
return f"{_float_to_ical(self.latitude)};{_float_to_ical(self.longitude)}"

@property
def ical_value(self) -> tuple[float, float]:
Expand Down
50 changes: 50 additions & 0 deletions src/icalendar/tests/test_issue_2136_float_exponent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Regression tests for issue #2136: FLOAT values must not use an exponent."""

import re

import pytest

from icalendar import Event
from icalendar.prop import vFloat, vGeo

RFC_5545_FLOAT = re.compile(r"[+-]?\d+(\.\d+)?")
"""The FLOAT grammar of RFC 5545, section 3.3.7."""

VALUES = [0.00001, -0.00002, 1.5e-07, 1e16, -1e22, 5e-324, 1.7976931348623157e308]
"""Floats that Python writes with an exponent."""


@pytest.mark.parametrize("value", VALUES)
def test_vfloat_follows_the_float_grammar(value):
"""vFloat writes no exponent."""
assert RFC_5545_FLOAT.fullmatch(vFloat(value).to_ical().decode())


@pytest.mark.parametrize("value", VALUES)
def test_vfloat_round_trip(value):
"""The value is the same after writing and parsing it."""
assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value


def test_vfloat_examples():
"""Small and large values are written with all their digits."""
assert vFloat(0.00001).to_ical() == b"0.00001"
assert vFloat(1e16).to_ical() == b"10000000000000000"


def test_vgeo_writes_no_exponent():
"""Coordinates close to zero are written with all their digits."""
assert vGeo((0.00001, -0.00002)).to_ical() == "0.00001;-0.00002"


def test_geo_of_an_event():
"""The GEO property of an event has no exponent."""
event = Event()
event.add("GEO", (0.00001, -0.00002))
assert b"GEO:0.00001;-0.00002" in event.to_ical()


def test_other_values_stay_the_same():
"""Values that had no exponent are written as before."""
assert vFloat(3.1415).to_ical() == b"3.1415"
assert vGeo((37.386013, -122.082932)).to_ical() == "37.386013;-122.082932"
Loading