Skip to content

__repr__ in typing #5870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,6 @@ class G2(Generic[Unpack[Ts]]): pass
with self.assertRaises(TypeError):
C[int, Unpack[Ts], Unpack[Ts]]

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repr_is_correct(self):
Ts = TypeVarTuple('Ts')

Expand Down Expand Up @@ -1555,8 +1553,6 @@ class A(Generic[Unpack[Ts]]): pass
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
Expand Down Expand Up @@ -3634,8 +3630,6 @@ def test_new_repr_complex(self):
'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]')
Expand Down Expand Up @@ -4300,8 +4294,6 @@ class Y(C[int]):
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')
Expand Down
14 changes: 11 additions & 3 deletions vm/src/stdlib/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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 {}
Expand Down
Loading