Skip to content

Commit aa78e5f

Browse files
Copilotm-aciek
andcommitted
Add __class_getitem__ only to datetime class as requested
Co-authored-by: m-aciek <9288014+m-aciek@users.noreply.github.com>
1 parent 2f5b00a commit aa78e5f

3 files changed

Lines changed: 12 additions & 49 deletions

File tree

‎Lib/_pydatetime.py‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,6 @@ def _getstate(self):
952952
def __reduce__(self):
953953
return (self.__class__, self._getstate())
954954

955-
__class_getitem__ = classmethod(_types.GenericAlias)
956-
957955
timedelta.min = timedelta(-999999999)
958956
timedelta.max = timedelta(days=999999999, hours=23, minutes=59, seconds=59,
959957
microseconds=999999)
@@ -1300,8 +1298,6 @@ def __setstate(self, string):
13001298
def __reduce__(self):
13011299
return (self.__class__, self._getstate())
13021300

1303-
__class_getitem__ = classmethod(_types.GenericAlias)
1304-
13051301
_date_class = date # so functions w/ args named "date" can get at the class
13061302

13071303
date.min = date(1, 1, 1)
@@ -1369,8 +1365,6 @@ def __reduce__(self):
13691365
args = ()
13701366
return (self.__class__, args, self.__getstate__())
13711367

1372-
__class_getitem__ = classmethod(_types.GenericAlias)
1373-
13741368

13751369
class IsoCalendarDate(tuple):
13761370

@@ -1778,8 +1772,6 @@ def __reduce_ex__(self, protocol):
17781772
def __reduce__(self):
17791773
return self.__reduce_ex__(2)
17801774

1781-
__class_getitem__ = classmethod(_types.GenericAlias)
1782-
17831775
_time_class = time # so functions w/ args named "time" can get at the class
17841776

17851777
time.min = time(0, 0, 0)
@@ -2573,8 +2565,6 @@ def _name_from_offset(delta):
25732565
return f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}'
25742566
return f'UTC{sign}{hours:02d}:{minutes:02d}'
25752567

2576-
__class_getitem__ = classmethod(_types.GenericAlias)
2577-
25782568
UTC = timezone.utc = timezone._create(timedelta(0))
25792569

25802570
# bpo-37642: These attributes are rounded to the nearest minute for backwards

‎Lib/test/datetimetester.py‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,22 @@ def test_divide_and_round(self):
126126
self.assertEqual(dar(6, -4), -2)
127127
self.assertEqual(dar(-6, -4), 2)
128128

129-
def test_class_getitem(self):
130-
"""Test that datetime types support subscripting via __class_getitem__."""
129+
def test_datetime_class_getitem(self):
130+
"""Test that datetime class supports subscripting via __class_getitem__."""
131131
from types import GenericAlias
132132
datetime = datetime_module
133133

134-
# Test all datetime types can be subscripted
135-
types_to_test = [
136-
(datetime.date, 'date'),
137-
(datetime.time, 'time'),
138-
(datetime.datetime, 'datetime'),
139-
(datetime.timedelta, 'timedelta'),
140-
(datetime.tzinfo, 'tzinfo'),
141-
(datetime.timezone, 'timezone'),
142-
]
134+
# Test datetime class can be subscripted
135+
alias = datetime.datetime[int]
136+
self.assertIsInstance(alias, GenericAlias)
137+
self.assertIs(alias.__origin__, datetime.datetime)
138+
self.assertEqual(alias.__args__, (int,))
143139

144-
for cls, name in types_to_test:
145-
with self.subTest(name=name):
146-
# Test single type argument
147-
alias = cls[int]
148-
self.assertIsInstance(alias, GenericAlias)
149-
self.assertIs(alias.__origin__, cls)
150-
self.assertEqual(alias.__args__, (int,))
151-
152-
# Test multiple type arguments
153-
alias_multi = cls[int, str]
154-
self.assertIsInstance(alias_multi, GenericAlias)
155-
self.assertIs(alias_multi.__origin__, cls)
156-
self.assertEqual(alias_multi.__args__, (int, str))
140+
# Test multiple type arguments
141+
alias_multi = datetime.datetime[int, str]
142+
self.assertIsInstance(alias_multi, GenericAlias)
143+
self.assertIs(alias_multi.__origin__, datetime.datetime)
144+
self.assertEqual(alias_multi.__args__, (int, str))
157145

158146

159147
#############################################################################

‎Modules/_datetimemodule.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,9 +3052,6 @@ static PyMethodDef delta_methods[] = {
30523052
{"__reduce__", delta_reduce, METH_NOARGS,
30533053
PyDoc_STR("__reduce__() -> (cls, state)")},
30543054

3055-
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
3056-
PyDoc_STR("See PEP 585")},
3057-
30583055
{NULL, NULL},
30593056
};
30603057

@@ -4018,9 +4015,6 @@ static PyMethodDef date_methods[] = {
40184015
{"__reduce__", date_reduce, METH_NOARGS,
40194016
PyDoc_STR("__reduce__() -> (cls, state)")},
40204017

4021-
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
4022-
PyDoc_STR("See PEP 585")},
4023-
40244018
{NULL, NULL}
40254019
};
40264020

@@ -4257,9 +4251,6 @@ static PyMethodDef tzinfo_methods[] = {
42574251
{"__reduce__", tzinfo_reduce, METH_NOARGS,
42584252
PyDoc_STR("-> (cls, state)")},
42594253

4260-
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
4261-
PyDoc_STR("See PEP 585")},
4262-
42634254
{NULL, NULL}
42644255
};
42654256

@@ -4508,9 +4499,6 @@ static PyMethodDef timezone_methods[] = {
45084499
{"__getinitargs__", timezone_getinitargs, METH_NOARGS,
45094500
PyDoc_STR("pickle support")},
45104501

4511-
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
4512-
PyDoc_STR("See PEP 585")},
4513-
45144502
{NULL, NULL}
45154503
};
45164504

@@ -5303,9 +5291,6 @@ static PyMethodDef time_methods[] = {
53035291
DATETIME_TIME___REDUCE_EX___METHODDEF
53045292
DATETIME_TIME___REDUCE___METHODDEF
53055293

5306-
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
5307-
PyDoc_STR("See PEP 585")},
5308-
53095294
{NULL, NULL}
53105295
};
53115296

0 commit comments

Comments
 (0)