Fix Python 3 round() signature (#2107)

In Python 3, the ndigits argument of round() defaults to None. If
ndigits is excluded or explicitly None, the result is always an int. If
ndigits is not None, the result should be the same type as the number.

$ cat test.py
from fractions import Fraction

print(type(round(0.1)))
print(type(round(0.1, None)))
print(type(round(0.1, 0)))

print(type(round(Fraction(1, 10))))
print(type(round(Fraction(1, 10), None)))
print(type(round(Fraction(1, 10), 0)))
$ python3 ./test.py
<class 'int'>
<class 'int'>
<class 'float'>
<class 'int'>
<class 'int'>
<class 'fractions.Fraction'>

Update the signatures to allow for an ndigits of None.
This commit is contained in:
Omar Sandoval
2018-05-11 11:02:09 -07:00
committed by Jelle Zijlstra
parent aab1b20c80
commit 0a30d22f0d

View File

@@ -143,7 +143,7 @@ class int:
def __neg__(self) -> int: ...
def __pos__(self) -> int: ...
def __invert__(self) -> int: ...
def __round__(self, ndigits: int = ...) -> int: ...
def __round__(self, ndigits: Optional[int] = ...) -> int: ...
def __eq__(self, x: object) -> bool: ...
def __ne__(self, x: object) -> bool: ...
@@ -186,6 +186,8 @@ class float:
@overload
def __round__(self) -> int: ...
@overload
def __round__(self, ndigits: None) -> int: ...
@overload
def __round__(self, ndigits: int) -> float: ...
def __eq__(self, x: object) -> bool: ...
@@ -912,9 +914,13 @@ def repr(o: object) -> str: ...
@overload
def round(number: float) -> int: ...
@overload
def round(number: float, ndigits: int) -> float: ... # Always return a float if given ndigits.
def round(number: float, ndigits: None) -> int: ...
@overload
def round(number: SupportsRound[_T]) -> _T: ...
def round(number: float, ndigits: int) -> float: ...
@overload
def round(number: SupportsRound[_T]) -> int: ...
@overload
def round(number: SupportsRound[_T], ndigits: None) -> int: ... # type: ignore
@overload
def round(number: SupportsRound[_T], ndigits: int) -> _T: ...
def setattr(object: Any, name: str, value: Any) -> None: ...