Skip to content

Commit

Permalink
disallow untyped defs in twisted.python.test.test_tzhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Aug 19, 2023
1 parent 28990ad commit cca6e3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ module = [
'twisted.python.test.test_runtime',
'twisted.python.test.test_setup',
'twisted.python.test.test_textattributes',
'twisted.python.test.test_tzhelper',
'twisted.python.zippath',
'twisted.runner.inetdtap',
'twisted.runner.inetdconf',
Expand Down Expand Up @@ -651,4 +650,5 @@ module = [
'twisted.python.test.test_zippath',
'twisted.python.test.test_win32',
'twisted.python.test.test_versions',
'twisted.python.test.test_tzhelper',
]
34 changes: 20 additions & 14 deletions src/twisted/python/test/test_tzhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Tests for L{twisted.python._tzhelper}.
"""

from __future__ import annotations

from os import environ

try:
Expand All @@ -14,7 +16,7 @@
else:
tzset = _tzset

from datetime import timedelta
from datetime import datetime, timedelta
from time import mktime as mktime_real

from twisted.python._tzhelper import FixedOffsetTimeZone
Expand All @@ -26,7 +28,7 @@
# just a platform bug, so let's work around it. -glyph


def mktime(t9):
def mktime(t9: tuple[int, int, int, int, int, int, int, int, int]) -> float:
"""
Call L{mktime_real}, and if it raises L{OverflowError}, catch it and raise
SkipTest instead.
Expand All @@ -43,7 +45,7 @@ def mktime(t9):
raise SkipTest(f"Platform cannot construct time zone for {t9!r}")


def setTZ(name):
def setTZ(name: str | None) -> None:
"""
Set time zone.
Expand All @@ -63,7 +65,7 @@ def setTZ(name):
tzset()


def addTZCleanup(testCase):
def addTZCleanup(testCase: TestCase) -> None:
"""
Add cleanup hooks to a test case to reset timezone to original value.
Expand All @@ -73,7 +75,7 @@ def addTZCleanup(testCase):
tzIn = environ.get("TZ", None)

@testCase.addCleanup
def resetTZ():
def resetTZ() -> None:
setTZ(tzIn)


Expand All @@ -82,30 +84,34 @@ class FixedOffsetTimeZoneTests(TestCase):
Tests for L{FixedOffsetTimeZone}.
"""

def test_tzinfo(self):
def test_tzinfo(self) -> None:
"""
Test that timezone attributes respect the timezone as set by the
standard C{TZ} environment variable and L{tzset} API.
"""
if tzset is None:
raise SkipTest("Platform cannot change timezone; unable to verify offsets.")

def testForTimeZone(name, expectedOffsetDST, expectedOffsetSTD):
def testForTimeZone(
name: str, expectedOffsetDST: str, expectedOffsetSTD: str
) -> None:
setTZ(name)

localDST = mktime((2006, 6, 30, 0, 0, 0, 4, 181, 1))
localDSTdt = datetime.fromtimestamp(localDST)
localSTD = mktime((2007, 1, 31, 0, 0, 0, 2, 31, 0))
localSTDdt = datetime.fromtimestamp(localSTD)

tzDST = FixedOffsetTimeZone.fromLocalTimeStamp(localDST)
tzSTD = FixedOffsetTimeZone.fromLocalTimeStamp(localSTD)

self.assertEqual(tzDST.tzname(localDST), f"UTC{expectedOffsetDST}")
self.assertEqual(tzSTD.tzname(localSTD), f"UTC{expectedOffsetSTD}")
self.assertEqual(tzDST.tzname(localDSTdt), f"UTC{expectedOffsetDST}")
self.assertEqual(tzSTD.tzname(localSTDdt), f"UTC{expectedOffsetSTD}")

self.assertEqual(tzDST.dst(localDST), timedelta(0))
self.assertEqual(tzSTD.dst(localSTD), timedelta(0))
self.assertEqual(tzDST.dst(localDSTdt), timedelta(0))
self.assertEqual(tzSTD.dst(localSTDdt), timedelta(0))

def timeDeltaFromOffset(offset):
def timeDeltaFromOffset(offset: str) -> timedelta:
assert len(offset) == 5

sign = offset[0]
Expand All @@ -121,10 +127,10 @@ def timeDeltaFromOffset(offset):
return timedelta(hours=hours, minutes=minutes)

self.assertEqual(
tzDST.utcoffset(localDST), timeDeltaFromOffset(expectedOffsetDST)
tzDST.utcoffset(localDSTdt), timeDeltaFromOffset(expectedOffsetDST)
)
self.assertEqual(
tzSTD.utcoffset(localSTD), timeDeltaFromOffset(expectedOffsetSTD)
tzSTD.utcoffset(localSTDdt), timeDeltaFromOffset(expectedOffsetSTD)
)

addTZCleanup(self)
Expand Down

0 comments on commit cca6e3a

Please sign in to comment.