__repr__ in typing (#5870)

* ParamSpec.__repr__

* Fix more repr
This commit is contained in:
Jeong, YunWon
2025-06-30 10:36:10 +09:00
committed by GitHub
parent 66a1138c66
commit b59a6666fe
2 changed files with 11 additions and 11 deletions

View File

@@ -1436,8 +1436,6 @@ class TypeVarTupleTests(BaseTestCase):
with self.assertRaises(TypeError):
C[int, Unpack[Ts], Unpack[Ts]]
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repr_is_correct(self):
Ts = TypeVarTuple('Ts')
@@ -1555,8 +1553,6 @@ class TypeVarTupleTests(BaseTestCase):
self.assertEndsWith(repr(K[float]), 'A[float, typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[float, str]), 'A[float, str, typing.Unpack[typing.Tuple[str, ...]]]')
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVarTuple'):
class C(TypeVarTuple): pass
@@ -3634,8 +3630,6 @@ class GenericTests(BaseTestCase):
'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]'
)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_new_repr_bare(self):
T = TypeVar('T')
self.assertEqual(repr(Generic[T]), 'typing.Generic[~T]')
@@ -4300,8 +4294,6 @@ class GenericTests(BaseTestCase):
self.assertEqual(Y.__qualname__,
'GenericTests.test_repr_2.<locals>.Y')
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repr_3(self):
T = TypeVar('T')
T1 = TypeVar('T1')

View File

@@ -343,7 +343,7 @@ pub(crate) mod decl {
infer_variance: bool,
}
#[pyclass(flags(HAS_DICT), with(AsNumber, Constructor))]
#[pyclass(flags(HAS_DICT), with(AsNumber, Constructor, Representable))]
impl ParamSpec {
#[pymethod]
fn __mro_entries__(&self, _bases: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -555,6 +555,14 @@ pub(crate) mod decl {
}
}
impl Representable for ParamSpec {
#[inline(always)]
fn repr_str(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
let name = zelf.__name__().str(vm)?;
Ok(format!("~{name}"))
}
}
pub(crate) fn make_paramspec(name: PyObjectRef) -> ParamSpec {
ParamSpec {
name,
@@ -739,7 +747,7 @@ pub(crate) mod decl {
#[inline(always)]
fn repr_str(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
let name = zelf.name.str(vm)?;
Ok(format!("*{name}"))
Ok(name.to_string())
}
}
@@ -960,7 +968,7 @@ pub(crate) mod decl {
}
#[pyattr]
#[pyclass(name)]
#[pyclass(name = "Generic", module = "typing")]
#[derive(Debug, PyPayload)]
#[allow(dead_code)]
pub(crate) struct Generic {}