4

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
  • There's no problem here, afaikt. Just ignore the linter. – juanpa.arrivillaga Dec 30 '19 at 16:31
  • Could you share the piece of code related ? – azro Dec 30 '19 at 16:42
  • 3
    You may already be aware of this, but :according to http://pylint.pycqa.org/en/latest/technical_reference/features.html#pylint-global-options-and-switches, `round()` has "backwards-incompatible semantics in Python 3". one example of different behavior between versions is: `round(1)` returns an integer in 3.X, and a float in 2.7. I don't know if that's the _only_ difference, but you should definitely account for that if you plan to write a version-intercompatible alternative. – Kevin Dec 30 '19 at 16:47
  • have you tried adding the second Parameter? if your rounding to 2 digits try: round(n, 2) – h_r_b_y Dec 30 '19 at 16:55
  • @h_r_b_y -- Yes, any use of the function results in the warning. – Neil C. Obremski Dec 30 '19 at 16:57
  • If you don't care about calling built-in function, just disable the warning in pylint — not sure why it would it's enabled in the first place... – martineau Dec 30 '19 at 17:15
  • All these comments are incorrect, there is a real difference and potential problem depending on the use case. See answer below. – SwimBikeRun Sep 05 '20 at 02:10

3 Answers3

6

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)

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
4

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

2

Given Kevin's comment and the fact that Python2 is almost obsolete, it's best to ignore this warning.

Linters and even PEP8 give stylistic advice. Often very useful advice. But sometimes there are solid reasons for ignoring that advice.

What I would suggest is to use your preferred linter on all your projects. Then make a list of all errors and warnings that (in your opinion) are false positives, too pedantic or otherwise not helpful. Disable those globally in your linter's configuration file.

If you don't want to disable an error or warning globally, several linters accept the # noqa comment as a notice not to check a certain line.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94