Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Migrate direct __repr__ definitions to Representable trait
Move legacy #[pymethod] __repr__ to impl Representable for:
- PySSLContext, PySSLSocket (ssl.rs)
- BufferedReader, BufferedWriter, BufferedRandom (_io.rs)

Enable __repr__ guard in derive to prevent future direct definitions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
  • Loading branch information
moreal and claude committed Mar 16, 2026
commit 8ef8c6f969874c5cd00f909f549ba5ab76e23fa5
2 changes: 1 addition & 1 deletion crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ where
("__new__", "Constructor"),
("__init__", "Initializer"),
// Representable trait
// ("__repr__", "Representable"),
("__repr__", "Representable"),
// ("__str__", "???"), // allow __str__
// Hashable trait
("__hash__", "Hashable"),
Expand Down
28 changes: 16 additions & 12 deletions crates/stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,18 +857,13 @@ mod _ssl {
binary_form: OptionalArg<bool>,
}

#[pyclass(with(Constructor), flags(BASETYPE))]
#[pyclass(with(Constructor, Representable), flags(BASETYPE))]
impl PySSLContext {
// Helper method to convert DER certificate bytes to Python dict
fn cert_der_to_dict(&self, vm: &VirtualMachine, cert_der: &[u8]) -> PyResult<PyObjectRef> {
cert::cert_der_to_dict_helper(vm, cert_der)
}

#[pymethod]
fn __repr__(&self) -> String {
format!("<SSLContext(protocol={})>", self.protocol)
}

#[pygetset]
fn check_hostname(&self) -> bool {
*self.check_hostname.read()
Expand Down Expand Up @@ -2191,6 +2186,13 @@ mod _ssl {
}
}

impl Representable for PySSLContext {
#[inline]
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
Ok(format!("<SSLContext(protocol={})>", zelf.protocol))
}
}

impl Constructor for PySSLContext {
type Args = (i32,);

Expand Down Expand Up @@ -2374,7 +2376,7 @@ mod _ssl {
Completed, // unwrap() completed successfully
}

#[pyclass(with(Constructor), flags(BASETYPE))]
#[pyclass(with(Constructor, Representable), flags(BASETYPE))]
impl PySSLSocket {
// Check if this is BIO mode
pub(crate) fn is_bio_mode(&self) -> bool {
Expand Down Expand Up @@ -3021,11 +3023,6 @@ mod _ssl {
}
}

#[pymethod]
fn __repr__(&self) -> String {
"<SSLSocket>".to_string()
}

// Helper function to convert Python PROTO_* constants to rustls versions
fn get_rustls_versions(
minimum: i32,
Expand Down Expand Up @@ -4562,6 +4559,13 @@ mod _ssl {
}
}

impl Representable for PySSLSocket {
#[inline]
fn repr_str(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
Ok("<SSLSocket>".to_owned())
}
}

impl Constructor for PySSLSocket {
type Args = ();

Expand Down