8000 Deprecation warning fix for __complex__ (#5152) · RustPython/RustPython@3eda1cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 3eda1cf

Browse files
authored
Deprecation warning fix for __complex__ (#5152)
1 parent 6917b4c commit 3eda1cf

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Lib/test/test_complex.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,6 @@ def test_boolcontext(self):
340340
def test_conjugate(self):
341341
self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
342342

343-
# TODO: RUSTPYTHON
344-
@unittest.expectedFailure
345343
def test_constructor(self):
346344
class NS:
347345
def __init__(self, value): self.value = value

vm/src/builtins/complex.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
},
1010
identifier,
1111
protocol::PyNumberMethods,
12+
stdlib::warnings,
1213
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
1314
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1415
};
@@ -59,14 +60,31 @@ impl PyObjectRef {
5960
}
6061
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
6162
let result = method?.call((), vm)?;
62-
// TODO: returning strict subclasses of complex in __complex__ is deprecated
63-
return match result.payload::<PyComplex>() {
64-
Some(complex_obj) => Ok(Some((complex_obj.value, true))),
65-
None => Err(vm.new_type_error(format!(
66-
"__complex__ returned non-complex (type '{}')",
67-
result.class().name()
68-
))),
69-
};
63+
64+
let ret_class = result.class().to_owned();
65+
if let Some(ret) = result.downcast_ref::<PyComplex>() {
66+
warnings::warn(
67+
vm.ctx.exceptions.deprecation_warning,
68+
format!(
69+
"__complex__ returned non-complex (type {}). \
70+
The ability to return an instance of a strict subclass of complex \
71+
is deprecated, and may be removed in a future version of Python.",
72+
ret_class
73+
),
74+
1,
75+
vm,
76+
)?;
77+
78+
return Ok(Some((ret.value, true)));
79+
} else {
80+
return match result.payload::<PyComplex>() {
81+
Some(complex_obj) => Ok(Some((complex_obj.value, true))),
82+
None => Err(vm.new_type_error(format!(
83+
"__complex__ returned non-complex (type '{}')",
84+
result.class().name()
85+
))),
86+
};
87+
}
7088
}
7189
// `complex` does not have a `__complex__` by default, so subclasses might not either,
7290
// use the actual stored value in this case

0 commit comments

Comments
 (0)
0