From 0309b0a5a24cc53bda8a36a8fbe65048fcfcd3a3 Mon Sep 17 00:00:00 2001 From: xqt Date: Sat, 7 Dec 2024 18:37:21 +0100 Subject: [PATCH] IMPR: *precision* value 'millenia' is deprecated due to spelling mistake 'millennium' must be used instead (singular like for century, year etc.) Change-Id: Ia1e3c88d7fd7cfb6f633b66a1e987fffce0c0be8 --- pywikibot/_wbtypes.py | 86 +++++++++++++++++++++++++++++------------- tests/wbtypes_tests.py | 8 ++-- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py index 19317be18e..a0c0b5ddc3 100644 --- a/pywikibot/_wbtypes.py +++ b/pywikibot/_wbtypes.py @@ -11,14 +11,16 @@ import json import math import re +from collections.abc import Mapping from decimal import Decimal from typing import TYPE_CHECKING, Any import pywikibot from pywikibot import exceptions +from pywikibot.backports import Iterator from pywikibot.logging import warning from pywikibot.time import Timestamp -from pywikibot.tools import remove_last_args +from pywikibot.tools import issue_deprecation_warning, remove_last_args if TYPE_CHECKING: @@ -289,6 +291,43 @@ def get_globe_item(self, repo: DataSite | None = None, return pywikibot.ItemPage.from_entity_uri(repo, self.entity, lazy_load) +class _Precision(Mapping): + + """Wrapper for WbTime.PRECISION to deprecate 'millenia' key.""" + + PRECISION = { + '1000000000': 0, + '100000000': 1, + '10000000': 2, + '1000000': 3, + '100000': 4, + '10000': 5, + 'millennium': 6, + 'century': 7, + 'decade': 8, + 'year': 9, + 'month': 10, + 'day': 11, + 'hour': 12, + 'minute': 13, + 'second': 14, + } + + def __getitem__(self, key) -> int: + if key == 'millenia': + issue_deprecation_warning( + f'{key!r} key for precision', "'millennium'", since='10.0.0') + return self.PRECISION['millennium'] + + return self.PRECISION[key] + + def __iter__(self) -> Iterator[int]: + return iter(self.PRECISION) + + def __len__(self) -> int: + return len(self.PRECISION) + + class WbTime(WbRepresentation): """A Wikibase time representation. @@ -304,22 +343,7 @@ class WbTime(WbRepresentation): :class:`pywikibot.Timestamp` and :meth:`fromTimestamp`. """ - PRECISION = {'1000000000': 0, - '100000000': 1, - '10000000': 2, - '1000000': 3, - '100000': 4, - '10000': 5, - 'millenia': 6, - 'century': 7, - 'decade': 8, - 'year': 9, - 'month': 10, - 'day': 11, - 'hour': 12, - 'minute': 13, - 'second': 14 - } + PRECISION = _Precision() FORMATSTR = '{0:+012d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z' @@ -378,21 +402,30 @@ def __init__(self, the equality operator will return false if the timezone is different. - :param year: The year as a signed integer of between 1 and 16 digits. + .. deprecated:: 10.0 + *precision* value 'millenia' is deprecated; 'millennium' must + be used instead. + + :param year: The year as a signed integer of between 1 and 16 + digits. :param month: Month of the timestamp, if it exists. :param day: Day of the timestamp, if it exists. :param hour: Hour of the timestamp, if it exists. :param minute: Minute of the timestamp, if it exists. :param second: Second of the timestamp, if it exists. - :param precision: The unit of the precision of the time. - :param before: Number of units after the given time it could be, if - uncertain. The unit is given by the precision. - :param after: Number of units before the given time it could be, if - uncertain. The unit is given by the precision. + :param precision: The unit of the precision of the time. Must be + either an int in range 0 - 14 or one of '1000000000', + '100000000', '10000000', '1000000', '100000', '10000', + 'millennium', 'century', 'decade', 'year', month', 'day', + 'hour', 'minute' or 'second'. + :param before: Number of units after the given time it could be, + if uncertain. The unit is given by the precision. + :param after: Number of units before the given time it could be, + if uncertain. The unit is given by the precision. :param timezone: Timezone information in minutes. :param calendarmodel: URI identifying the calendar model. - :param site: The Wikibase site. If not provided, retrieves the data - repository from the default site from user-config.py. + :param site: The Wikibase site. If not provided, retrieves the + data repository from the default site from user-config.py. Only used if calendarmodel is not given. """ if year is None: @@ -441,7 +474,6 @@ def __init__(self, and precision in self.PRECISION.values()): self.precision = precision elif precision in self.PRECISION: - assert isinstance(precision, str) self.precision = self.PRECISION[precision] else: raise ValueError(f'Invalid precision: "{precision}"') @@ -647,7 +679,7 @@ def normalize(self) -> WbTime: # so we don't need to do anything complicated like the other # examples. year = round(year / power_of_10) * power_of_10 - elif self.precision == self.PRECISION['millenia']: + elif self.precision == self.PRECISION['millennium']: # Similar situation with centuries year_float = year / 1000 if year_float < 0: diff --git a/tests/wbtypes_tests.py b/tests/wbtypes_tests.py index 1a83216a21..dc672a9c15 100755 --- a/tests/wbtypes_tests.py +++ b/tests/wbtypes_tests.py @@ -258,7 +258,7 @@ def test_WbTime_normalization(self): # some aliases here decade = pywikibot.WbTime.PRECISION['decade'] century = pywikibot.WbTime.PRECISION['century'] - millenia = pywikibot.WbTime.PRECISION['millenia'] + millennium = pywikibot.WbTime.PRECISION['millennium'] t = pywikibot.WbTime(site=repo, year=2010, month=1, day=1, hour=12, minute=43, second=12) t2 = pywikibot.WbTime(site=repo, year=2010, month=1, day=1, hour=12, @@ -287,7 +287,7 @@ def test_WbTime_normalization(self): precision=century) t10 = pywikibot.WbTime(site=repo, year=2010, month=1, day=1, hour=12, minute=43, second=12, - precision=millenia) + precision=millennium) t11 = pywikibot.WbTime(site=repo, year=2010, month=1, day=1, hour=12, minute=43, second=12, timezone=-300, precision=pywikibot.WbTime.PRECISION['day']) @@ -324,10 +324,10 @@ def test_WbTime_normalization(self): precision=century).normalize()) self.assertEqual(t10.normalize(), pywikibot.WbTime(site=repo, year=3000, - precision=millenia)) + precision=millennium)) self.assertEqual(t10.normalize(), pywikibot.WbTime(site=repo, year=2010, - precision=millenia).normalize()) + precision=millennium).normalize()) t11_normalized = t11.normalize() t12_normalized = t12.normalize() self.assertEqual(t11_normalized.timezone, 0)