Skip to content

Commit eafa0c0

Browse files
committed
Fix int rounding
1 parent db01a1d commit eafa0c0

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

crates/vm/src/builtins/int.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::{
1212
},
1313
convert::{IntoPyException, ToPyObject, ToPyResult},
1414
function::{
15-
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, PyArithmeticValue, PyComparisonValue,
15+
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, OptionalOption, PyArithmeticValue,
16+
PyComparisonValue,
1617
},
1718
protocol::{PyNumberMethods, handle_bytes_to_int_err},
1819
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
@@ -381,10 +382,10 @@ impl PyInt {
381382
#[pymethod]
382383
fn __round__(
383384
zelf: PyRef<Self>,
384-
ndigits: OptionalArg<PyIntRef>,
385+
ndigits: OptionalOption<PyIntRef>,
385386
vm: &VirtualMachine,
386387
) -> PyResult<PyRef<Self>> {
387-
if let OptionalArg::Present(ndigits) = ndigits {
388+
if let Some(ndigits) = ndigits.flatten() {
388389
let ndigits = ndigits.as_bigint();
389390
// round(12345, -2) == 12300
390391
// If precision >= 0, then any integer is already rounded correctly

extra_tests/snippets/builtin_int.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ def __int__(self):
318318
assert isinstance((1).__round__(0), int)
319319
assert (0).__round__(0) == 0
320320
assert (1).__round__(0) == 1
321-
assert_raises(TypeError, lambda: (0).__round__(None))
322-
assert_raises(TypeError, lambda: (1).__round__(None))
321+
# Python 3.14+: __round__(None) is now allowed, same as __round__()
322+
assert (0).__round__(None) == 0
323+
assert (1).__round__(None) == 1
323324
assert_raises(TypeError, lambda: (0).__round__(0.0))
324325
assert_raises(TypeError, lambda: (1).__round__(0.0))
325326

0 commit comments

Comments
 (0)