Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated datetime.datetime.utcnow() #3365

Merged
merged 1 commit into from
Mar 21, 2024
Merged

Conversation

m-horky
Copy link
Contributor

@m-horky m-horky commented Jan 11, 2024

This function is deprecated since Python 3.12.
datetime.datetime.now(datetime.UTC) is a timezone-aware equivalent.

@cnsnyder cnsnyder requested review from a team and DuckBoss and removed request for a team January 11, 2024 12:46
Copy link

github-actions bot commented Jan 11, 2024

Coverage

Coverage (computed on Fedora latest) •
FileStmtsMissCoverMissing
rhsm
   certificate2.py4235088%76–81, 105–108, 110, 143–147, 458–460, 522, 544–548, 575, 578–579, 581–584, 593, 631–632, 634, 661–666, 777, 779, 870, 888, 918, 926, 936, 940
   certificate.py59930249%90, 93, 96, 110–111, 134–135, 138–141, 143–145, 147–148, 150, 158, 169, 177, 185, 196–201, 209–214, 224, 233, 240–243, 245–247, 256–260, 266–267, 269, 277, 280, 283–287, 290–298, 310–311, 314, 317–318, 326, 333–339, 356–364, 372–380, 383–384, 387–397, 406, 408–415, 417, 423–424, 427–429, 431, 439, 447, 457–465, 474–482, 486, 490, 493–496, 499–505, 523–528, 555–560, 566–567, 569, 572, 615–617, 629, 633, 636, 646, 670–673, 751–753, 756–759, 773, 804–806, 888, 912, 915, 918, 921, 924, 927, 930, 933, 936, 939, 942, 952, 955, 958, 961, 964, 967, 970, 973, 976–996, 1002–1009, 1012, 1015, 1018, 1021, 1024, 1027, 1030, 1033, 1036–1046, 1049, 1072, 1075, 1078, 1081, 1084, 1087, 1090, 1093, 1096, 1099, 1105–1118, 1121, 1124, 1129, 1132, 1135, 1138–1143, 1146
   connection.py102247054%48–49, 53, 55–56, 81, 95, 106, 147, 281, 312, 378–383, 387–396, 457, 459, 561, 564, 571–577, 582, 638, 673–677, 679, 692, 719, 722–723, 725–726, 728, 739–743, 747, 751, 753–754, 773, 776, 780–781, 786, 789–790, 805, 809, 811–812, 839–840, 842, 845, 850–851, 854–855, 857, 859–863, 865–866, 869–876, 878–888, 890, 892–893, 904–906, 908–910, 912–914, 916–918, 920, 923–929, 931–932, 934–935, 937, 948–950, 952–953, 955–957, 959, 971–974, 979, 1043, 1045–1050, 1052, 1057–1061, 1067–1070, 1072–1077, 1081–1086, 1093, 1130, 1132, 1137, 1148, 1157–1160, 1164, 1166–1168, 1172–1173, 1175–1182, 1184, 1186, 1189–1196, 1199–1203, 1206–1211, 1218, 1220, 1272, 1289–1292, 1316, 1338, 1368, 1373, 1376, 1379–1380, 1385, 1388, 1393, 1396, 1439–1443, 1450–1451, 1453, 1461–1462, 1464, 1481, 1494–1496, 1499, 1512, 1519, 1523, 1551–1553, 1558–1559, 1561–1562, 1564–1565, 1567–1581, 1583–1585, 1587–1598, 1600, 1617–1619, 1621–1623, 1625–1627, 1632, 1637–1639, 1644, 1671, 1703–1731, 1736–1737, 1739–1741, 1744–1745, 1748–1749, 1752–1753, 1772–1773, 1782–1783, 1793–1794, 1801–1802, 1808–1811, 1817–1820, 1826–1827, 1833–1834, 1854–1855, 1864–1868, 1876–1877, 1903–1906, 1931–1932, 1941–1942, 1950–1951, 1972, 1974–1976, 1978, 1980, 1983, 1985–1998, 2000–2001, 2010–2012, 2024–2025, 2034–2035, 2037, 2039–2041, 2048–2050, 2059–2061, 2069–2070, 2081, 2083–2084, 2086, 2088–2091, 2093–2095, 2098, 2100, 2107–2108, 2115–2116, 2126–2127, 2137–2140, 2150–2156, 2163–2166
rhsmlib/facts
   hwprobe.py41410175%43, 85, 194–196, 220–221, 237–238, 260, 265, 270–272, 274–277, 279, 324–328, 330–332, 335–338, 340–342, 369, 386, 405, 407, 439, 465, 583, 601–603, 612, 620–623, 633, 636, 638–639, 641, 648, 652–654, 658–660, 664–666, 677–678, 682, 706, 708–709, 711, 713, 715, 717–720, 724–726, 728–729, 732, 742–746, 748–749, 751–752, 754–760, 762–763
TOTAL18227462674% 

Tests Skipped Failures Errors Time
2640 14 💤 0 ❌ 0 🔥 41.638s ⏱️

@m-horky m-horky force-pushed the mhorky/datetime-utcnow branch from 4fd7332 to fc04b59 Compare January 11, 2024 13:07
Copy link
Contributor

@ptoscano ptoscano left a comment

Choose a reason for hiding this comment

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

most of the changes seems OK at a quick glance, one potential issue spotted

@@ -95,7 +95,7 @@ def drift_check(utc_time_string: str, hours: int = 1) -> bool:
utc_datetime = dateutil.parser.parse(utc_time_string)
# This should not have a timezone, but we know it will be utc.
# We need our timezones to match in order to compare
local_datetime = datetime.datetime.utcnow().replace(tzinfo=utc_datetime.tzinfo)
Copy link
Contributor

Choose a reason for hiding this comment

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

be careful here: this local_datetime will have the tzinfo object not from datetime but from dateutil, which helps when comparing it against utc_datetime (returned by the dateutil.parser.parse() above)

I'm afraid the tzinfo object still needs to be replaced

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. In theory we could get rid of the whole dateutil dependency, but that would be a separate card.

@m-horky m-horky force-pushed the mhorky/datetime-utcnow branch from fc04b59 to 0efd8d4 Compare January 12, 2024 08:30
Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

LGTM, I have only few small requests and comments/questions.

# dateutil has its own tzinfo object representing UTC
timestamp = timestamp.replace(tzinfo=datetime.timezone.utc)

now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Small detail... Why do you replace microsecond with 0 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to clean up the sub-second delta. The server sends its timestamp with precision in seconds, it doesn't make sense to be more specific than that.

return False

# The time has format of 'Fri, 12 Jan 2024 08:10:46 GMT'
timestamp: datetime.datetime = dateutil.parser.parse(timestamp)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we encapsulate dateutil.parser.parse(timestamp) by try-except since we use drift_check() only for printing warning message. Server in theory can return any nonsense.

src/rhsm/connection.py Outdated Show resolved Hide resolved
@m-horky m-horky force-pushed the mhorky/datetime-utcnow branch 2 times, most recently from 41713ab to 97e8580 Compare January 19, 2024 12:25
`utcnow()` is deprecated since Python 3.12.
`datetime.datetime.now(datetime.timezone.utc)` is a timezone-aware equivalent.

connection.py's `drift_check` logic has been extended a bit.
@m-horky m-horky force-pushed the mhorky/datetime-utcnow branch from 97e8580 to 8069e5c Compare February 12, 2024 19:31
Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks for update

@jirihnidek jirihnidek merged commit 66a176d into main Mar 21, 2024
16 checks passed
@jirihnidek jirihnidek deleted the mhorky/datetime-utcnow branch March 21, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants