From 191e0890decb7914876d532d35db8ff9ff85c016 Mon Sep 17 00:00:00 2001 From: Gatsik <74517072+Gatsik@users.noreply.github.com> Date: Thu, 4 Jul 2024 21:47:42 +0300 Subject: [PATCH] DateAxisItem: Add type annotations to newly added functions fix its tests: use C locale, which seems to be available on Windows, Linux and MacOS --- pyqtgraph/graphicsItems/DateAxisItem.py | 31 +++++++++++++++++------- tests/graphicsItems/test_DateAxisItem.py | 9 ++++--- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/pyqtgraph/graphicsItems/DateAxisItem.py b/pyqtgraph/graphicsItems/DateAxisItem.py index af76e4e86c..2747ef302a 100644 --- a/pyqtgraph/graphicsItems/DateAxisItem.py +++ b/pyqtgraph/graphicsItems/DateAxisItem.py @@ -152,7 +152,9 @@ def __init__(self, tickSpecs, exampleText): self.utcOffset = None self.exampleText = exampleText - def extendTimeRangeForSpacing(self, spacing, minVal, maxVal): + def extendTimeRangeForSpacing( + self, spacing: int, minVal: int | float, maxVal: int | float, + ) -> tuple[int | float, int | float]: if spacing < HOUR_SPACING: return minVal, maxVal @@ -160,7 +162,9 @@ def extendTimeRangeForSpacing(self, spacing, minVal, maxVal): extendedMin = minVal - abs(getPreferredOffsetFromUtc(minVal, self.utcOffset)) return extendedMin, extendedMax - def moveTicksToLocalTimeCoords(self, ticks, spacing, skipFactor): + def moveTicksToLocalTimeCoords( + self, ticks: np.ndarray, spacing: int, skipFactor: int, + ) -> np.ndarray: if len(ticks) == 0: return ticks @@ -232,36 +236,42 @@ def tickValues(self, minVal, maxVal, minSpc): ], "99:99:99") -def fromSecsSinceEpoch(timestamp): +def fromSecsSinceEpoch(timestamp: float | int) -> QDateTime: try: return QDateTime.fromSecsSinceEpoch(round(timestamp)) except OverflowError: return QDateTime() -def calculateUtcOffset(timestamp): +def calculateUtcOffset(timestamp: float | int) -> int: return -fromSecsSinceEpoch(timestamp).offsetFromUtc() -def getPreferredOffsetFromUtc(timestamp, preferred_offset=None): +def getPreferredOffsetFromUtc( + timestamp: float | int, + preferred_offset: int | None = None, +) -> int: """Retrieve the utc offset respecting the daylight saving time""" if preferred_offset is not None: return preferred_offset return calculateUtcOffset(timestamp) -def adjustTimestampToPreferredUtcOffset(timestamp, offest=None): +def adjustTimestampToPreferredUtcOffset( + timestamp: float | int, + offest: int | None = None, +) -> int | float: return timestamp - getPreferredOffsetFromUtc(timestamp, offest) -def offsetToLocalHour(timestamp): +def offsetToLocalHour(timestamp: float | int) -> int: local = fromSecsSinceEpoch(timestamp) roundedToHour = local.time() roundedToHour.setHMS(roundedToHour.hour(), 0, 0) return -roundedToHour.secsTo(local.time()) -def applyOffsetFromUtc(timestamp): +def applyOffsetFromUtc(timestamp: float | int) -> int: """ UTC+4 1970-01-02 02:00 (local) == 1970-01-01 22:00 (UTC) -> 1970-01-01 22:00 (local) @@ -276,7 +286,10 @@ def applyOffsetFromUtc(timestamp): return repositioned.toSecsSinceEpoch() -def applyOffsetToUtc(timestamp, preferred_offset=None): +def applyOffsetToUtc( + timestamp: float | int, + preferred_offset: int | None = None, +) -> int: delocalized = applyOffsetFromUtc(timestamp) return getPreferredOffsetFromUtc(delocalized, preferred_offset) diff --git a/tests/graphicsItems/test_DateAxisItem.py b/tests/graphicsItems/test_DateAxisItem.py index 39536c1d74..a712bfe255 100644 --- a/tests/graphicsItems/test_DateAxisItem.py +++ b/tests/graphicsItems/test_DateAxisItem.py @@ -20,6 +20,7 @@ SEC_PER_YEAR, SECOND_SPACING, WEEK_SPACING, + YEAR_MONTH_ZOOM_LEVEL, YEAR_SPACING, ZoomLevel, applyOffsetFromUtc, @@ -35,7 +36,7 @@ def makeDateAxis(): axis = pg.DateAxisItem() axis.fontMetrics = QFontMetrics(QFont()) - axis.zoomLevel = None + axis.zoomLevel = YEAR_MONTH_ZOOM_LEVEL return axis @@ -85,8 +86,8 @@ def dateAxis(): @pytest.fixture(autouse=True) -def use_en_us_locale(): - locale.setlocale(locale.LC_TIME, "en_US") +def use_c_locale(): + locale.setlocale(locale.LC_TIME, "C") def test_preferred_utc_offset_respects_chosen_offset(): @@ -108,7 +109,7 @@ def test_utc_offset_works_with_float_timestamp(): assert -16 * 3600 <= calculateUtcOffset(123456.0734) <= 16 * 3600 -def test_shift_local_time_to_utc_time_does_what_it_promises_to_do(): +def test_applyOffsetFromUtc_does_what_it_promises_to_do(): timeZone = QTimeZone(b"UTC+4") startDate = QDateTime(QDate(1970, 1, 2), QTime(2, 0), timeZone)