简体   繁体   中英

What is the 'scope' of pylint comments

What is the scope for various pylint comments/annotations? For example:

# myfile.py

import math
import operator as op

# pylint: disable=something <----- (1) scope here?

# pylint: disable=something <-- (2) scope here?
class MyClass: # pylint: disable=something <-- (3) scope here?
    # pylint: disable=something <-- (4) scope here?
    def __init__(self):
        print ("HI")      #  pylint: disable=something <--- (5) scope here?

# pylint:disable=something <-- (6) scope here?
def myfunc(num): # pylint: disable=something <-- (7) scope here?
    print ("HI")

I know there are quite a few here and hopefully there are just a few rules which cover them all but I wanted to make sure I tried to cover most scenarios.

If there is only the comment on the line: The scope is the current context, from the disable comment to the end of the context, unless there is an enable then it's from the disable comment to the enable comment. (Same scope than a variable you define would be).

If there is also code on the line: The scope is only the line.

Ie:

# pylint: disable=something
# something inactive
# pylint: enable=something
# something active
# pylint: disable=something
# something inactive
def function_context():
    # pylint: disable=something-else
    # something inactive
    a = 42 # pylint: enable=something ('something' is active on this line)
    # something inactive
    # something-else inactive
# something-else active

I was wondering the same thing and did some experiments. So IMO the answers are:

1 - global scope, affects everything below that rule

2 - same as above, you'd thing it will only apply MyClass but it will also impact classes and functions below

3, 4 - same thing, will apply only inside the class

6 - applies to everything below that line

7 - applies only to the myfunct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM