Skip to content

Commit b59a666

Browse files
authored
__repr__ in typing (#5870)
* ParamSpec.__repr__ * Fix more repr
1 parent 66a1138 commit b59a666

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Lib/test/test_typing.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,6 @@ class G2(Generic[Unpack[Ts]]): pass
14361436
with self.assertRaises(TypeError):
14371437
C[int, Unpack[Ts], Unpack[Ts]]
14381438

1439-
# TODO: RUSTPYTHON
1440-
@unittest.expectedFailure
14411439
def test_repr_is_correct(self):
14421440
Ts = TypeVarTuple('Ts')
14431441

@@ -1555,8 +1553,6 @@ class A(Generic[Unpack[Ts]]): pass
15551553
self.assertEndsWith(repr(K[float]), 'A[float, typing.Unpack[typing.Tuple[str, ...]]]')
15561554
self.assertEndsWith(repr(K[float, str]), 'A[float, str, typing.Unpack[typing.Tuple[str, ...]]]')
15571555

1558-
# TODO: RUSTPYTHON
1559-
@unittest.expectedFailure
15601556
def test_cannot_subclass(self):
15611557
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVarTuple'):
15621558
class C(TypeVarTuple): pass
@@ -3634,8 +3630,6 @@ def test_new_repr_complex(self):
36343630
'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]'
36353631
)
36363632

3637-
# TODO: RUSTPYTHON
3638-
@unittest.expectedFailure
36393633
def test_new_repr_bare(self):
36403634
T = TypeVar('T')
36413635
self.assertEqual(repr(Generic[T]), 'typing.Generic[~T]')
@@ -4300,8 +4294,6 @@ class Y(C[int]):
43004294
self.assertEqual(Y.__qualname__,
43014295
'GenericTests.test_repr_2.<locals>.Y')
43024296

4303-
# TODO: RUSTPYTHON
4304-
@unittest.expectedFailure
43054297
def test_repr_3(self):
43064298
T = TypeVar('T')
43074299
T1 = TypeVar('T1')

vm/src/stdlib/typing.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pub(crate) mod decl {
343343
infer_variance: bool,
344344
}
345345

346-
#[pyclass(flags(HAS_DICT), with(AsNumber, Constructor))]
346+
#[pyclass(flags(HAS_DICT), with(AsNumber, Constructor, Representable))]
347347
impl ParamSpec {
348348
#[pymethod]
349349
fn __mro_entries__(&self, _bases: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -555,6 +555,14 @@ pub(crate) mod decl {
555555
}
556556
}
557557

558+
impl Representable for ParamSpec {
559+
#[inline(always)]
560+
fn repr_str(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
561+
let name = zelf.__name__().str(vm)?;
562+
Ok(format!("~{name}"))
563+
}
564+
}
565+
558566
pub(crate) fn make_paramspec(name: PyObjectRef) -> ParamSpec {
559567
ParamSpec {
560568
name,
@@ -739,7 +747,7 @@ pub(crate) mod decl {
739747
#[inline(always)]
740748
fn repr_str(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
741749
let name = zelf.name.str(vm)?;
742-
Ok(format!("*{name}"))
750+
Ok(name.to_string())
743751
}
744752
}
745753

@@ -960,7 +968,7 @@ pub(crate) mod decl {
960968
}
961969

962970
#[pyattr]
963-
#[pyclass(name)]
971+
#[pyclass(name = "Generic", module = "typing")]
964972
#[derive(Debug, PyPayload)]
965973
#[allow(dead_code)]
966974
pub(crate) struct Generic {}

0 commit comments

Comments
 (0)