add missing `__class_getitem__` from cpython 3.10 · sheeeng/rustpython-rustpython@b2eff4a · GitHub
Skip to content

Commit b2eff4a

Browse files
committed
add missing __class_getitem__ from cpython 3.10
Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent 32ba09c commit b2eff4a

15 files changed

Lines changed: 203 additions & 0 deletions

File tree

Lib/_collections_abc.py

Lines changed: 153 additions & 0 deletions

Lib/_weakrefset.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# by abc.py to load everything else at startup.
44

55
from _weakref import ref
6+
from types import GenericAlias
67

78
__all__ = ['WeakSet']
89

@@ -197,3 +198,5 @@ def isdisjoint(self, other):
197198

198199
def __repr__(self):
199200
return repr(self.data)
201+
202+
__class_getitem__ = classmethod(GenericAlias)

Lib/asyncio/futures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ def __del__(self):
184184
context['source_traceback'] = self._source_traceback
185185
self._loop.call_exception_handler(context)
186186

187+
def __class_getitem__(cls, type):
188+
return cls
189+
187190
def cancel(self):
188191
"""Cancel the future and schedule callbacks.
189192

Lib/asyncio/queues.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def __repr__(self):
8181
def __str__(self):
8282
return '<{} {}>'.format(type(self).__name__, self._format())
8383

84+
def __class_getitem__(cls, type):
85+
return cls
86+
8487
def _format(self):
8588
result = 'maxsize={!r}'.format(self._maxsize)
8689
if getattr(self, '_queue', None):

Lib/concurrent/futures/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import threading
99
import time
10+
import types
1011

1112
FIRST_COMPLETED = 'FIRST_COMPLETED'
1213
FIRST_EXCEPTION = 'FIRST_EXCEPTION'
@@ -544,6 +545,8 @@ def set_exception(self, exception):
544545
self._condition.notify_all()
545546
self._invoke_callbacks()
546547

548+
__class_getitem__ = classmethod(types.GenericAlias)
549+
547550
class Executor(object):
548551
"""This is an abstract base class for concrete asynchronous executors."""
549552

Lib/concurrent/futures/thread.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import itertools
1111
import queue
1212
import threading
13+
import types
1314
import weakref
1415
import os
1516

@@ -62,6 +63,8 @@ def run(self):
6263
else:
6364
self.future.set_result(result)
6465

66+
__class_getitem__ = classmethod(types.GenericAlias)
67+
6568

6669
def _worker(executor_reference, work_queue, initializer, initargs):
6770
if initializer is not None:

Lib/dataclasses.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import builtins
88
import functools
99
import _thread
10+
from types import GenericAlias
1011

1112

1213
__all__ = ['dataclass',
@@ -217,6 +218,8 @@ def __repr__(self):
217218
type_name = repr(self.type)
218219
return f'dataclasses.InitVar[{type_name}]'
219220

221+
def __class_getitem__(cls, type):
222+
return InitVar(type)
220223

221224
# Instances of Field are only ever created from within this module,
222225
# and only from the field() function, although Field instances are
@@ -285,6 +288,8 @@ def __set_name__(self, owner, name):
285288
# it.
286289
func(self.default, owner, name)
287290

291+
__class_getitem__ = classmethod(GenericAlias)
292+
288293

289294
class _DataclassParams:
290295
__slots__ = ('init',

Lib/difflib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from heapq import nlargest as _nlargest
3434
from collections import namedtuple as _namedtuple
35+
from types import GenericAlias
3536

3637
Match = _namedtuple('Match', 'a b size')
3738

@@ -685,6 +686,8 @@ def real_quick_ratio(self):
685686
# shorter sequence
686687
return _calculate_ratio(min(la, lb), la + lb)
687688

689+
__class_getitem__ = classmethod(GenericAlias)
690+
688691
def get_close_matches(word, possibilities, n=3, cutoff=0.6):
689692
"""Use SequenceMatcher to return list of the best "good enough" matches.
690693

Lib/functools.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from _thread import RLock
2323
except ModuleNotFoundError:
2424
from _dummy_thread import RLock
25+
from types import GenericAlias
2526

2627

2728
################################################################################
@@ -427,6 +428,8 @@ def __get__(self, obj, cls=None):
427428
def __isabstractmethod__(self):
428429
return getattr(self.func, "__isabstractmethod__", False)
429430

431+
__class_getitem__ = classmethod(GenericAlias)
432+
430433
# Helper functions
431434

432435
def _unwrap_partial(func):
@@ -977,3 +980,5 @@ def __get__(self, instance, owner=None):
977980
)
978981
raise TypeError(msg) from None
979982
return val
983+
984+
__class_getitem__ = classmethod(GenericAlias)

Lib/multiprocessing/managers.py

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)