Merge pull request #492 from collective/487-regression-dddtype-not-hashable-in-icalendar-5

implement hash and __eq__ for vDDDTypes
pull/494/head
Jaca 2022-11-17 17:48:17 +01:00 zatwierdzone przez GitHub
commit b4b30ab92f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 43 dodań i 26 usunięć

Wyświetl plik

@ -14,11 +14,11 @@ Breaking changes:
New features:
- ...
- vDDDTypes is hashable #487 #492 [niccokunzmann]
Bug fixes:
- ...
- vDDDTypes' equality also checks the dt attribute #497 #492 [niccokunzmann]
5.0.2 (2022-11-03)
------------------

Wyświetl plik

@ -320,9 +320,12 @@ class vDDDTypes:
def __eq__(self, other):
if isinstance(other, vDDDTypes):
return self.params == other.params
return self.params == other.params and self.dt == other.dt
return False
def __hash__(self):
return hash(self.dt)
@classmethod
def from_ical(cls, ical, timezone=None):
if isinstance(ical, cls):

Wyświetl plik

@ -4,10 +4,12 @@ from datetime import time
from datetime import timedelta
from icalendar.parser import Parameters
import unittest
from icalendar.prop import vDatetime
from icalendar.prop import vDatetime, vDDDTypes
from icalendar.windows_to_olson import WINDOWS_TO_OLSON
import pytest
import pytz
from copy import deepcopy
from dateutil import tz
class TestProp(unittest.TestCase):
@ -112,27 +114,6 @@ class TestProp(unittest.TestCase):
# Bad input
self.assertRaises(ValueError, vDDDTypes, 42)
def test_vDDDTypes_equivalance(self):
from ..prop import vDDDTypes
from copy import deepcopy
vDDDTypes = [
vDDDTypes(pytz.timezone('US/Eastern').localize(datetime(year=2022, month=7, day=22, hour=12, minute=7))),
vDDDTypes(datetime(year=2022, month=7, day=22, hour=12, minute=7)),
vDDDTypes(date(year=2022, month=7, day=22)),
vDDDTypes(time(hour=22, minute=7, second=2))]
for v_type in vDDDTypes:
self.assertEqual(v_type, deepcopy(v_type))
for other in vDDDTypes:
if other is v_type:
continue
self.assertNotEqual(v_type, other)
# see if equivalnce fails for other types
self.assertNotEqual(v_type, 42)
self.assertNotEqual(v_type, 'test')
def test_prop_vDate(self):
from ..prop import vDate
@ -518,6 +499,39 @@ class TestProp(unittest.TestCase):
)
vDDDTypes_list = [
vDDDTypes(pytz.timezone('US/Eastern').localize(datetime(year=2022, month=7, day=22, hour=12, minute=7))),
vDDDTypes(datetime(year=2022, month=7, day=22, hour=12, minute=7)),
vDDDTypes(datetime(year=2022, month=7, day=22, hour=12, minute=7, tzinfo=tz.UTC)),
vDDDTypes(date(year=2022, month=7, day=22)),
vDDDTypes(date(year=2022, month=7, day=23)),
vDDDTypes(time(hour=22, minute=7, second=2))
]
def identity(x):
return x
@pytest.mark.parametrize("map", [
deepcopy,
identity,
hash,
])
@pytest.mark.parametrize("v_type", vDDDTypes_list)
@pytest.mark.parametrize("other", vDDDTypes_list)
def test_vDDDTypes_equivalance(map, v_type, other):
if v_type is other:
assert map(v_type) == map(other), f"identity implies equality: {map.__name__}()"
assert not (map(v_type) != map(other)), f"identity implies equality: {map.__name__}()"
else:
assert map(v_type) != map(other), f"expected inequality: {map.__name__}()"
assert not (map(v_type) == map(other)), f"expected inequality: {map.__name__}()"
@pytest.mark.parametrize("v_type", vDDDTypes_list)
def test_inequality_with_different_types(v_type):
assert v_type != 42
assert v_type != 'test'
class TestPropertyValues(unittest.TestCase):
def test_vDDDLists_timezone(self):