Skip to content

disable=wrong-import-position applied to a statement that isn't an import fouls up recognition on later lines #10589

@ruck94301

Description

@ruck94301

Bug description

I am deliberately putting some logging statements interspersed with imports, as imports of some 3rd party libs take significant time and I am instrumenting that progress.

Import statements following non-import statements produce wrong-import-position, and I tried to suppress them precisely instead of disable globally or by block.

I found, accidentally, that if I disable=wrong-import-position for the non-import statement,
a. pylint correctly reports it as a useless-suppression
b. wrongly suppresses the subsequent wrong-import-position findings.

See notes at
https://stackoverflow.com/questions/79773154/pylint-single-line-pragma-is-sticky-effect-is-not-limited-to-single-line/79774191#79774191

Command used

pylint --rcfile pylintrc --verbose --enable=useless-suppression --disable=C0301,W0311 ../demo_pylint.py

Pylint output

************* Module LabGym.demo_pylint
/Users/john/Public/LabGym/LabGym/demo_pylint.py:7:0: I0021: Useless suppression of 'wrong-import-position' (useless-suppression)

Expected behavior

************* Module LabGym.demo_pylint
/Users/john/Public/LabGym/LabGym/demo_pylint.py:7:0: I0021: Useless suppression of 'wrong-import-position' (useless-suppression)
/Users/john/Public/LabGym/LabGym/demo_pylint.py:11:0: C0413: Import "import os" should be placed at the top of the module (wrong-import-position)
/Users/john/Public/LabGym/LabGym/demo_pylint.py:12:0: C0413: Import "import pathlib" should be placed at the top of the module (wrong-import-position)
/Users/john/Public/LabGym/LabGym/demo_pylint.py:13:0: C0413: Import "import random" should be placed at the top of the module (wrong-import-position)

Pylint version

pylint 3.3.7
astroid 3.3.11
Python 3.10.11 (v3.10.11:7d4cc5aa85, Apr  4 2023, 19:05:19) [Clang 13.0.0 (clang-1300.0.29.30)]

OS / Environment

here is simple test case whose output was provided in this issue:

"""Demo"""

import logging
import sys

# Xpylint: disable-next=wrong-import-position
logger = logging.getLogger()  # pylint: disable=wrong-import-position
logging.basicConfig(level='DEBUG')

logger.debug('import random (starting...)')
import os
import pathlib
import random
logger.debug('import random (done)')

sys.argv()
os.environ()
pathlib.Path()
random.random()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲False Negative 🦋No message is emitted but something is wrong with the code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions