__class_getitem__ (#5888)

This commit is contained in:
Jeong, YunWon authored and GitHub committed 2025-07-03 11:01:53 +09:00
1 parent b6acfe0f9d
commit fffa735868
15 files changed
+119 -27

No files matched your search

-4
View File
@@ -85,8 +85,6 @@ class BaseTest(unittest.TestCase):
if ctypes is not None:
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subscriptable(self):
for t in self.generic_types:
if t is None:
@@ -314,8 +312,6 @@ class BaseTest(unittest.TestCase):
for generic_alias_property in ("__origin__", "__args__", "__parameters__"):
self.assertIn(generic_alias_property, dir_of_gen_alias)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_weakref(self):
for t in self.generic_types:
if t is None:
+11 -2
View File
@@ -44,8 +44,8 @@ mod array {
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
builtins::{
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt,
PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
},
class_or_notimplemented,
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
@@ -1195,6 +1195,15 @@ mod array {
false
}
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
impl Comparable for PyArray {
+15 -6
View File
@@ -24,7 +24,7 @@ thread_local! {
mod _contextvars {
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
builtins::{PyStrRef, PyTypeRef},
builtins::{PyGenericAlias, PyStrRef, PyTypeRef},
class::StaticType,
common::hash::PyHash,
function::{ArgCallable, FuncArgs, OptionalArg},
@@ -478,11 +478,11 @@ mod _contextvars {
#[pyclassmethod]
fn __class_getitem__(
_cls: PyTypeRef,
_key: PyStrRef,
_vm: &VirtualMachine,
) -> PyResult<()> {
unimplemented!("ContextVar.__class_getitem__() is currently under construction")
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
@@ -571,6 +571,15 @@ mod _contextvars {
None => ContextTokenMissing::static_type().to_owned().into(),
}
}
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
impl Constructor for ContextToken {
+7 -2
View File
@@ -1,7 +1,7 @@
//! Implementation of the python bytearray object.
use super::{
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
PyType, PyTypeRef,
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef,
PyTuple, PyTupleRef, PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
@@ -571,6 +571,11 @@ impl PyByteArray {
fn reverse(&self) {
self.borrow_buf_mut().reverse();
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyclass]
+7 -1
View File
@@ -1,5 +1,6 @@
use super::{
PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
PositionIterInternal, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
@@ -481,6 +482,11 @@ impl PyBytes {
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
PyTuple::new_ref(param, &vm.ctx)
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyclass]
+6 -1
View File
@@ -1,4 +1,4 @@
use super::{PyBoundMethod, PyStr, PyType, PyTypeRef};
use super::{PyBoundMethod, PyGenericAlias, PyStr, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
@@ -170,6 +170,11 @@ impl PyClassMethod {
.set_attr("__isabstractmethod__", value, vm)?;
Ok(())
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
impl Representable for PyClassMethod {
+6 -1
View File
@@ -1,4 +1,4 @@
use super::{PyCode, PyStrRef, PyType};
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
@@ -72,6 +72,11 @@ impl PyCoroutine {
fn cr_origin(&self, _vm: &VirtualMachine) -> Option<(PyStrRef, usize, PyStrRef)> {
None
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyclass]
+6 -1
View File
@@ -2,7 +2,7 @@
* The mythical generator.
*/
use super::{PyCode, PyStrRef, PyType};
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
@@ -67,6 +67,11 @@ impl PyGenerator {
fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
self.inner.frame().yield_from_target()
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyclass]
+7 -2
View File
@@ -1,6 +1,6 @@
use super::{
PositionIterInternal, PyBytes, PyBytesRef, PyInt, PyListRef, PySlice, PyStr, PyStrRef, PyTuple,
PyTupleRef, PyType, PyTypeRef,
PositionIterInternal, PyBytes, PyBytesRef, PyGenericAlias, PyInt, PyListRef, PySlice, PyStr,
PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
@@ -550,6 +550,11 @@ impl Py<PyMemoryView> {
Representable
))]
impl PyMemoryView {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
#[pymethod]
pub fn release(&self) {
if self.released.compare_exchange(false, true).is_ok() {
+7 -1
View File
@@ -1,5 +1,6 @@
use super::{
PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter, tuple::tuple_hash,
PyGenericAlias, PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter,
tuple::tuple_hash,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
@@ -183,6 +184,11 @@ pub fn init(context: &Context) {
Representable
))]
impl PyRange {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
fn new(cls: PyTypeRef, stop: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyRange {
start: vm.ctx.new_pyref(0),
+6 -1
View File
@@ -1,6 +1,6 @@
// sliceobject.{h,c} in CPython
// spell-checker:ignore sliceobject
use super::{PyStrRef, PyTupleRef, PyType, PyTypeRef};
use super::{PyGenericAlias, PyStrRef, PyTupleRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
@@ -30,6 +30,11 @@ impl PyPayload for PySlice {
#[pyclass(with(Comparable, Representable, Hashable))]
impl PySlice {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
#[pygetset]
fn start(&self, vm: &VirtualMachine) -> PyObjectRef {
self.start.clone().to_pyobject(vm)
+6 -1
View File
@@ -1,4 +1,4 @@
use super::{PyStr, PyType, PyTypeRef};
use super::{PyGenericAlias, PyStr, PyType, PyTypeRef};
use crate::{
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
@@ -131,6 +131,11 @@ impl PyStaticMethod {
.set_attr("__isabstractmethod__", value, vm)?;
Ok(())
}
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
impl Callable for PyStaticMethod {
+10 -1
View File
@@ -2,7 +2,7 @@ use super::{genericalias, type_};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
builtins::{PyFrozenSet, PyStr, PyTuple, PyTupleRef, PyType},
builtins::{PyFrozenSet, PyGenericAlias, PyStr, PyTuple, PyTupleRef, PyType},
class::PyClassImpl,
common::hash,
convert::{ToPyObject, ToPyResult},
@@ -140,6 +140,15 @@ impl PyUnion {
fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
type_::or_(zelf, other, vm)
}
#[pyclassmethod]
fn __class_getitem__(
cls: crate::builtins::PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
pub fn is_unionable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
+15 -2
View File
@@ -1203,7 +1203,8 @@ pub(super) mod types {
use crate::{
AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
builtins::{
PyInt, PyStrRef, PyTupleRef, PyTypeRef, traceback::PyTracebackRef, tuple::IntoPyTuple,
PyGenericAlias, PyInt, PyStrRef, PyTupleRef, PyTypeRef, traceback::PyTracebackRef,
tuple::IntoPyTuple,
},
convert::ToPyResult,
function::{ArgBytesLike, FuncArgs},
@@ -1234,10 +1235,22 @@ pub(super) mod types {
#[derive(Debug)]
pub struct PySystemExit {}
#[pyexception(name, base = "PyBaseException", ctx = "base_exception_group", impl)]
#[pyexception(name, base = "PyBaseException", ctx = "base_exception_group")]
#[derive(Debug)]
pub struct PyBaseExceptionGroup {}
#[pyexception]
impl PyBaseExceptionGroup {
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
#[pyexception(name, base = "PyBaseExceptionGroup", ctx = "exception_group", impl)]
#[derive(Debug)]
pub struct PyExceptionGroup {}
+10 -1
View File
@@ -4,7 +4,7 @@ pub(crate) use _functools::make_module;
mod _functools {
use crate::{
Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyDict, PyTuple, PyTypeRef},
builtins::{PyDict, PyGenericAlias, PyTuple, PyTypeRef},
common::lock::PyRwLock,
function::{FuncArgs, KwArgs, OptionalArg},
object::AsObject,
@@ -190,6 +190,15 @@ mod _functools {
Ok(())
}
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}
impl Constructor for PyPartial {