Linting error on BitBucket: TypeError: 'LinterStats' object is not subscriptable

Question:

I am using BitBucket pipelines to perform linting checks with pylint. It was working fine a few hours ago. I have been facing the following error even though the final score is well past the minimum criteria (8.0):

Your code has been rated at 9.43/10

Traceback (most recent call last):
  File "/usr/local/bin/pylint-fail-under", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/dist-packages/pylint_fail_under/__main__.py", line 42, in main
    score = results.linter.stats["global_note"]
TypeError: 'LinterStats' object is not subscriptable
Asked By: Arjun A J

||

Answers:

Do not use pylint-fail-under, pylint has a fail-under option since pylint 2.5.0, and pylint-fail-under‘s maintener will not update their package for newer pylint.

Change pylint-fail-under --fail_under 8.0 to pylint --fail-under=8.0 and remove the dependency to pylint-fail-under.

See also https://github.com/PyCQA/pylint/issues/5405, and: https://github.com/TNThieding/pylint-fail-under/issues/8#issuecomment-626369567

Answered By: Pierre.Sassoulas

Another option is to downgrade, if that’s acceptable:

pip install pylint==2.11.1
Answered By: Rama Krishna

Your issue

TypeError: 'LinterStats' object is not subscriptable

Just means, that LinterStats is not idexable (see: What does it mean if a Python object is "subscriptable" or not?)

Old code (not working anymore)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: AGPL-3.0-or-later

"""Print ``pylint`` score (old way)."""

from pylint.lint import Run
score = Run([...], exit=False).linter.stats["global_note"]
print(score)

Example: https://github.com/apmechev/pylint-badge/blob/master/pylintbadge/pylintbadge.py

New code (working)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: AGPL-3.0-or-later

"""Print ``pylint`` score (new way)."""

from pylint.lint import Run
score = Run([...], exit=False).linter.stats.global_note
print(score)

Working silent code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: AGPL-3.0-or-later

"""Print ``pylint`` score (new & silent way)."""

import sys
from typing import TextIO
from pylint.lint import Run

default_stdout = sys.stdout
sys.stdout = type("Dummy", (TextIO,), {"write": lambda self, data: ()})()
score = Run(["./src"], exit=False).linter.stats.global_note
sys.stdout = default_stdout
print(score)
Answered By: Sukombu