Skip to content

Commit d61ef66

Browse files
committed
__class_getitem__
1 parent b6acfe0 commit d61ef66

File tree

15 files changed

+119
-27
lines changed

15 files changed

+119
-27
lines changed

Lib/test/test_genericalias.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class BaseTest(unittest.TestCase):
8585
if ctypes is not None:
8686
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))
8787

88-
# TODO: RUSTPYTHON
89-
@unittest.expectedFailure
9088
def test_subscriptable(self):
9189
for t in self.generic_types:
9290
if t is None:
@@ -314,8 +312,6 @@ def test_dir(self):
314312
for generic_alias_property in ("__origin__", "__args__", "__parameters__"):
315313
self.assertIn(generic_alias_property, dir_of_gen_alias)
316314

317-
# TODO: RUSTPYTHON
318-
@unittest.expectedFailure
319315
def test_weakref(self):
320316
for t in self.generic_types:
321317
if t is None:

stdlib/src/array.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ mod array {
4444
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
4545
atomic_func,
4646
builtins::{
47-
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt,
48-
PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
47+
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
48+
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
4949
},
5050
class_or_notimplemented,
5151
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
@@ -1195,6 +1195,15 @@ mod array {
11951195

11961196
false
11971197
}
1198+
1199+
#[pyclassmethod]
1200+
fn __class_getitem__(
1201+
cls: PyTypeRef,
1202+
args: PyObjectRef,
1203+
vm: &VirtualMachine,
1204+
) -> PyGenericAlias {
1205+
PyGenericAlias::from_args(cls, args, vm)
1206+
}
11981207
}
11991208

12001209
impl Comparable for PyArray {

stdlib/src/contextvars.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ thread_local! {
2424
mod _contextvars {
2525
use crate::vm::{
2626
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
27-
builtins::{PyStrRef, PyTypeRef},
27+
builtins::{PyGenericAlias, PyStrRef, PyTypeRef},
2828
class::StaticType,
2929
common::hash::PyHash,
3030
function::{ArgCallable, FuncArgs, OptionalArg},
@@ -478,11 +478,11 @@ mod _contextvars {
478478

479479
#[pyclassmethod]
480480
fn __class_getitem__(
481-
_cls: PyTypeRef,
482-
_key: PyStrRef,
483-
_vm: &VirtualMachine,
484-
) -> PyResult<()> {
485-
unimplemented!("ContextVar.__class_getitem__() is currently under construction")
481+
cls: PyTypeRef,
482+
args: PyObjectRef,
483+
vm: &VirtualMachine,
484+
) -> PyGenericAlias {
485+
PyGenericAlias::from_args(cls, args, vm)
486486
}
487487
}
488488

@@ -571,6 +571,15 @@ mod _contextvars {
571571
None => ContextTokenMissing::static_type().to_owned().into(),
572572
}
573573
}
574+
575+
#[pyclassmethod]
576+
fn __class_getitem__(
577+
cls: PyTypeRef,
578+
args: PyObjectRef,
579+
vm: &VirtualMachine,
580+
) -> PyGenericAlias {
581+
PyGenericAlias::from_args(cls, args, vm)
582+
}
574583
}
575584

576585
impl Constructor for ContextToken {

vm/src/builtins/bytearray.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation of the python bytearray object.
22
use super::{
3-
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
4-
PyType, PyTypeRef,
3+
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef,
4+
PyTuple, PyTupleRef, PyType, PyTypeRef,
55
};
66
use crate::{
77
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
@@ -571,6 +571,11 @@ impl PyByteArray {
571571
fn reverse(&self) {
572572
self.borrow_buf_mut().reverse();
573573
}
574+
575+
#[pyclassmethod]
576+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
577+
PyGenericAlias::from_args(cls, args, vm)
578+
}
574579
}
575580

576581
#[pyclass]

vm/src/builtins/bytes.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
2+
PositionIterInternal, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
3+
PyType, PyTypeRef,
34
};
45
use crate::{
56
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
@@ -481,6 +482,11 @@ impl PyBytes {
481482
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
482483
PyTuple::new_ref(param, &vm.ctx)
483484
}
485+
486+
#[pyclassmethod]
487+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
488+
PyGenericAlias::from_args(cls, args, vm)
489+
}
484490
}
485491

486492
#[pyclass]

vm/src/builtins/classmethod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyBoundMethod, PyStr, PyType, PyTypeRef};
1+
use super::{PyBoundMethod, PyGenericAlias, PyStr, PyType, PyTypeRef};
22
use crate::{
33
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
44
class::PyClassImpl,
@@ -170,6 +170,11 @@ impl PyClassMethod {
170170
.set_attr("__isabstractmethod__", value, vm)?;
171171
Ok(())
172172
}
173+
174+
#[pyclassmethod]
175+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
176+
PyGenericAlias::from_args(cls, args, vm)
177+
}
173178
}
174179

175180
impl Representable for PyClassMethod {

vm/src/builtins/coroutine.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyCode, PyStrRef, PyType};
1+
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
22
use crate::{
33
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
44
class::PyClassImpl,
@@ -72,6 +72,11 @@ impl PyCoroutine {
7272
fn cr_origin(&self, _vm: &VirtualMachine) -> Option<(PyStrRef, usize, PyStrRef)> {
7373
None
7474
}
75+
76+
#[pyclassmethod]
77+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
78+
PyGenericAlias::from_args(cls, args, vm)
79+
}
7580
}
7681

7782
#[pyclass]

vm/src/builtins/generator.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* The mythical generator.
33
*/
44

5-
use super::{PyCode, PyStrRef, PyType};
5+
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
66
use crate::{
77
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
88
class::PyClassImpl,
@@ -67,6 +67,11 @@ impl PyGenerator {
6767
fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
6868
self.inner.frame().yield_from_target()
6969
}
70+
71+
#[pyclassmethod]
72+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
73+
PyGenericAlias::from_args(cls, args, vm)
74+
}
7075
}
7176

7277
#[pyclass]

vm/src/builtins/memory.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
PositionIterInternal, PyBytes, PyBytesRef, PyInt, PyListRef, PySlice, PyStr, PyStrRef, PyTuple,
3-
PyTupleRef, PyType, PyTypeRef,
2+
PositionIterInternal, PyBytes, PyBytesRef, PyGenericAlias, PyInt, PyListRef, PySlice, PyStr,
3+
PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
44
};
55
use crate::{
66
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
@@ -550,6 +550,11 @@ impl Py<PyMemoryView> {
550550
Representable
551551
))]
552552
impl PyMemoryView {
553+
#[pyclassmethod]
554+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
555+
PyGenericAlias::from_args(cls, args, vm)
556+
}
557+
553558
#[pymethod]
554559
pub fn release(&self) {
555560
if self.released.compare_exchange(false, true).is_ok() {

vm/src/builtins/range.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter, tuple::tuple_hash,
2+
PyGenericAlias, PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter,
3+
tuple::tuple_hash,
34
};
45
use crate::{
56
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
@@ -183,6 +184,11 @@ pub fn init(context: &Context) {
183184
Representable
184185
))]
185186
impl PyRange {
187+
#[pyclassmethod]
188+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
189+
PyGenericAlias::from_args(cls, args, vm)
190+
}
191+
186192
fn new(cls: PyTypeRef, stop: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
187193
PyRange {
188194
start: vm.ctx.new_pyref(0),

0 commit comments

Comments
 (0)