Deprecate the psycopg2.tz module

Don't use its objects anymore in the tests (except for the tests for the
objects themselves).
This commit is contained in:
Daniele Varrazzo 2021-06-15 00:20:09 +01:00
parent f28502663f
commit 1d3d5e905f
5 changed files with 21 additions and 18 deletions

2
NEWS
View File

@ -22,6 +22,8 @@ Other changes:
- Dropped support for mx.DateTime. - Dropped support for mx.DateTime.
- Use `datetime.timezone` objects by default in datetime objects instead of - Use `datetime.timezone` objects by default in datetime objects instead of
`~psycopg2.tz.FixedOffsetTimezone`. `~psycopg2.tz.FixedOffsetTimezone`.
- The `psycopg2.tz` module is deprecated and scheduled to be dropped in the
next major release.
- Build system for Linux/MacOS binary packages moved to GitHub action, now - Build system for Linux/MacOS binary packages moved to GitHub action, now
providing :pep:`600`\-style wheels packages. providing :pep:`600`\-style wheels packages.

View File

@ -5,6 +5,10 @@
.. module:: psycopg2.tz .. module:: psycopg2.tz
.. deprecated:: 2.9
The module will be dropped in psycopg 2.10. Use `datetime.timezone`
instead.
This module holds two different tzinfo implementations that can be used as the This module holds two different tzinfo implementations that can be used as the
`tzinfo` argument to `~datetime.datetime` constructors, directly passed to `tzinfo` argument to `~datetime.datetime` constructors, directly passed to
Psycopg functions or used to set the `cursor.tzinfo_factory` attribute in Psycopg functions or used to set the `cursor.tzinfo_factory` attribute in

View File

@ -61,8 +61,6 @@ from psycopg2._psycopg import ( # noqa
__version__, __libpq_version__, __version__, __libpq_version__,
) )
from psycopg2 import tz # noqa
# Register default adapters. # Register default adapters.

View File

@ -188,7 +188,7 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.assertEqual(value.replace(tzinfo=None), base) self.assertEqual(value.replace(tzinfo=None), base)
# Conversion to UTC produces the expected offset. # Conversion to UTC produces the expected offset.
UTC = FixedOffsetTimezone(0, "UTC") UTC = timezone(timedelta(0))
value_utc = value.astimezone(UTC).replace(tzinfo=None) value_utc = value.astimezone(UTC).replace(tzinfo=None)
self.assertEqual(base - value_utc, timedelta(seconds=offset)) self.assertEqual(base - value_utc, timedelta(seconds=offset))
@ -317,7 +317,7 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.assertEqual(None, dt.tzinfo) self.assertEqual(None, dt.tzinfo)
def test_type_roundtrip_datetimetz(self): def test_type_roundtrip_datetimetz(self):
tz = FixedOffsetTimezone(8 * 60) tz = timezone(timedelta(minutes=8 * 60))
dt1 = datetime(2010, 5, 3, 10, 20, 30, tzinfo=tz) dt1 = datetime(2010, 5, 3, 10, 20, 30, tzinfo=tz)
dt2 = self._test_type_roundtrip(dt1) dt2 = self._test_type_roundtrip(dt1)
self.assertNotEqual(None, dt2.tzinfo) self.assertNotEqual(None, dt2.tzinfo)
@ -328,7 +328,7 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.assertEqual(None, tm.tzinfo) self.assertEqual(None, tm.tzinfo)
def test_type_roundtrip_timetz(self): def test_type_roundtrip_timetz(self):
tz = FixedOffsetTimezone(8 * 60) tz = timezone(timedelta(minutes=8 * 60))
tm1 = time(10, 20, 30, tzinfo=tz) tm1 = time(10, 20, 30, tzinfo=tz)
tm2 = self._test_type_roundtrip(tm1) tm2 = self._test_type_roundtrip(tm1)
self.assertNotEqual(None, tm2.tzinfo) self.assertNotEqual(None, tm2.tzinfo)
@ -345,7 +345,7 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
def test_type_roundtrip_datetimetz_array(self): def test_type_roundtrip_datetimetz_array(self):
self._test_type_roundtrip_array( self._test_type_roundtrip_array(
datetime(2010, 5, 3, 10, 20, 30, tzinfo=FixedOffsetTimezone(0))) datetime(2010, 5, 3, 10, 20, 30, tzinfo=timezone(timedelta(0))))
def test_type_roundtrip_time_array(self): def test_type_roundtrip_time_array(self):
self._test_type_roundtrip_array(time(10, 20, 30)) self._test_type_roundtrip_array(time(10, 20, 30))
@ -359,10 +359,10 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
self.assertEqual(t, time(0, 0)) self.assertEqual(t, time(0, 0))
t = self.execute("select '24:00+05'::timetz;") t = self.execute("select '24:00+05'::timetz;")
self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(300))) self.assertEqual(t, time(0, 0, tzinfo=timezone(timedelta(minutes=300))))
t = self.execute("select '24:00+05:30'::timetz;") t = self.execute("select '24:00+05:30'::timetz;")
self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330))) self.assertEqual(t, time(0, 0, tzinfo=timezone(timedelta(minutes=330))))
@skip_before_postgres(8, 1) @skip_before_postgres(8, 1)
def test_large_interval(self): def test_large_interval(self):
@ -430,11 +430,11 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
t = self.execute("select 'infinity'::timestamptz") t = self.execute("select 'infinity'::timestamptz")
self.assert_(t.tzinfo is not None) self.assert_(t.tzinfo is not None)
self.assert_(t > datetime(4000, 1, 1, tzinfo=FixedOffsetTimezone())) self.assert_(t > datetime(4000, 1, 1, tzinfo=timezone(timedelta(0))))
t = self.execute("select '-infinity'::timestamptz") t = self.execute("select '-infinity'::timestamptz")
self.assert_(t.tzinfo is not None) self.assert_(t.tzinfo is not None)
self.assert_(t < datetime(1000, 1, 1, tzinfo=FixedOffsetTimezone())) self.assert_(t < datetime(1000, 1, 1, tzinfo=timezone(timedelta(0))))
def test_redshift_day(self): def test_redshift_day(self):
# Redshift is reported returning 1 day interval as microsec (bug #558) # Redshift is reported returning 1 day interval as microsec (bug #558)
@ -473,7 +473,7 @@ class FromTicksTestCase(unittest.TestCase):
s = psycopg2.TimestampFromTicks(1273173119.99992) s = psycopg2.TimestampFromTicks(1273173119.99992)
self.assertEqual(s.adapted, self.assertEqual(s.adapted,
datetime(2010, 5, 6, 14, 11, 59, 999920, datetime(2010, 5, 6, 14, 11, 59, 999920,
tzinfo=FixedOffsetTimezone(-5 * 60))) tzinfo=timezone(timedelta(minutes=-5 * 60))))
def test_date_value_error_sec_59_99(self): def test_date_value_error_sec_59_99(self):
s = psycopg2.DateFromTicks(1273173119.99992) s = psycopg2.DateFromTicks(1273173119.99992)

View File

@ -20,7 +20,7 @@ import json
import uuid import uuid
import warnings import warnings
from decimal import Decimal from decimal import Decimal
from datetime import date, datetime from datetime import date, datetime, timedelta, timezone
from functools import wraps from functools import wraps
from pickle import dumps, loads from pickle import dumps, loads
@ -38,7 +38,6 @@ from psycopg2.extras import (
Inet, Json, NumericRange, Range, RealDictConnection, Inet, Json, NumericRange, Range, RealDictConnection,
register_composite, register_hstore, register_range, register_composite, register_hstore, register_range,
) )
from psycopg2.tz import FixedOffsetTimezone
class TypesExtrasTests(ConnectingTestCase): class TypesExtrasTests(ConnectingTestCase):
@ -1282,7 +1281,7 @@ class RangeTestCase(unittest.TestCase):
Date-Time ranges should return a human-readable string as well on Date-Time ranges should return a human-readable string as well on
string conversion. string conversion.
''' '''
tz = FixedOffsetTimezone(-5 * 60, "EST") tz = timezone(timedelta(minutes=-5 * 60), "EST")
r = DateTimeTZRange(datetime(2010, 1, 1, tzinfo=tz), r = DateTimeTZRange(datetime(2010, 1, 1, tzinfo=tz),
datetime(2011, 1, 1, tzinfo=tz)) datetime(2011, 1, 1, tzinfo=tz))
expected = '[2010-01-01 00:00:00-05:00, 2011-01-01 00:00:00-05:00)' expected = '[2010-01-01 00:00:00-05:00, 2011-01-01 00:00:00-05:00)'
@ -1377,9 +1376,9 @@ class RangeCasterTestCase(ConnectingTestCase):
def test_cast_timestamptz(self): def test_cast_timestamptz(self):
cur = self.conn.cursor() cur = self.conn.cursor()
ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts1 = datetime(2000, 1, 1, tzinfo=timezone(timedelta(minutes=600)))
ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, ts2 = datetime(2000, 12, 31, 23, 59, 59, 999,
tzinfo=FixedOffsetTimezone(600)) tzinfo=timezone(timedelta(minutes=600)))
cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2)) cur.execute("select tstzrange(%s, %s, '[]')", (ts1, ts2))
r = cur.fetchone()[0] r = cur.fetchone()[0]
self.assert_(isinstance(r, DateTimeTZRange)) self.assert_(isinstance(r, DateTimeTZRange))
@ -1465,9 +1464,9 @@ class RangeCasterTestCase(ConnectingTestCase):
self.assert_(isinstance(r1, DateTimeRange)) self.assert_(isinstance(r1, DateTimeRange))
self.assert_(r1.isempty) self.assert_(r1.isempty)
ts1 = datetime(2000, 1, 1, tzinfo=FixedOffsetTimezone(600)) ts1 = datetime(2000, 1, 1, tzinfo=timezone(timedelta(minutes=600)))
ts2 = datetime(2000, 12, 31, 23, 59, 59, 999, ts2 = datetime(2000, 12, 31, 23, 59, 59, 999,
tzinfo=FixedOffsetTimezone(600)) tzinfo=timezone(timedelta(minutes=600)))
r = DateTimeTZRange(ts1, ts2, '(]') r = DateTimeTZRange(ts1, ts2, '(]')
cur.execute("select %s", (r,)) cur.execute("select %s", (r,))
r1 = cur.fetchone()[0] r1 = cur.fetchone()[0]