Skip to content

Deprecation warning fix #2

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ def test_boolcontext(self):
def test_conjugate(self):
self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constructor(self):
class NS:
def __init__(self, value): self.value = value
Expand Down
5 changes: 2 additions & 3 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,8 @@ impl Compiler {
SymbolScope::Cell => {
cache = &mut info.cellvar_cache;
NameOpType::Deref
}
// // TODO: is this right?
// SymbolScope::Unknown => NameOpType::Global,
} // TODO: is this right?
// SymbolScope::Unknown => NameOpType::Global,
};

if NameUsage::Load == usage && name == "__debug__" {
Expand Down
34 changes: 26 additions & 8 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
},
identifier,
protocol::PyNumberMethods,
stdlib::warnings,
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
Expand Down Expand Up @@ -59,14 +60,31 @@ impl PyObjectRef {
}
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
let result = method?.call((), vm)?;
// TODO: returning strict subclasses of complex in __complex__ is deprecated
return match result.payload::<PyComplex>() {
Some(complex_obj) => Ok(Some((complex_obj.value, true))),
None => Err(vm.new_type_error(format!(
"__complex__ returned non-complex (type '{}')",
result.class().name()
))),
};

let ret_class = result.class().to_owned();
if let Some(ret) = result.downcast_ref::<PyComplex>() {
warnings::warn(
vm.ctx.exceptions.deprecation_warning,
format!(
"__complex__ returned non-complex (type {}). \
The ability to return an instance of a strict subclass of complex \
is deprecated, and may be removed in a future version of Python.",
ret_class
),
1,
vm,
)?;

return Ok(Some((ret.value, true)));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Ok(Some((ret.value, true)));
Ok(Some((ret.value, true)))

} else {
return match result.payload::<PyComplex>() {
Some(complex_obj) => Ok(Some((complex_obj.value, true))),
None => Err(vm.new_type_error(format!(
"__complex__ returned non-complex (type '{}')",
result.class().name()
))),
};
}
}
// `complex` does not have a `__complex__` by default, so subclasses might not either,
// use the actual stored value in this case
Expand Down