Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative to built-in round()?

Pylint is complaining round built-in referenced but what is the alternative?

The answers I've seen thus far are simply to quiet Pylint with regards to built-in functions. There must be some other way to call round(), perhaps in a standard import library? Is there something potentially wrong about using the built-in function?

My searches for these answers have only come up with dozens of tutorials on using the built-in function.

Usage is anything to with round(). This triggers the warning:

n = 0.05
n = round(n)

The exact warning only shows up in VS Code, it's:

{
    "resource": "/C:/Users/neil.obremski/project/file.py",
    "owner": "python",
    "code": "round-builtin",
    "severity": 4,
    "message": "round built-in referenced",
    "source": "pylint",
    "startLineNumber": 434,
    "startColumn": 9,
    "endLineNumber": 434,
    "endColumn": 9
}

UPDATE: This shows up when --enable=W is set in the Pylint arguments. It shows up for absolutely any use of the round() function, including specifying the second argument.

Here's what the Pylint output looks like for pylint file.py --enable=W:

file.py:435:18: W1633: round built-in referenced (round-builtin)
like image 443
Neil C. Obremski Avatar asked Dec 13 '22 09:12

Neil C. Obremski


2 Answers

If you're only using python3.x you can ignore this warning (it is disabled by default, some option you are passing to pylint is enabling this)

The warning is intended to be part of the --py3k suite of checks, which look for python2 / python3 compatibility issues (if you're python3-only, this suite of checks can be actively harmful to code you write)

The reason for flagging all uses of round are that both the rounding algorithm and types returned changed in python 3. In python3, rounding is now done using "bankers rounding" (what's new in python 3.0#builtins)

like image 188
Anthony Sottile Avatar answered Dec 25 '22 20:12

Anthony Sottile


If you need the code to be both Python 2 and 3 compatible and cannot change your lint settings, use this import from builtins import round

like image 34
Caitlin Hamilton Avatar answered Dec 25 '22 21:12

Caitlin Hamilton