pylint icon indicating copy to clipboard operation
pylint copied to clipboard

PyLint reports "no assignment is done" but variable is changed in the scope of the procedure

Open motib79 opened this issue 1 year ago • 4 comments

Bug description

I got the message:
Using global for 'ticker' but no assignment is done

for the code:
---
def remove_ticker_from_list(stock_index):
    global ticker
    ticker[stock_index]="NULL_STOCK"

Configuration

No response

Command used

VSCode

Pylint output

mentioned in descr

Expected behavior

do not report this error

Pylint version

Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32

OS / Environment

Win10

Additional dependencies

No response

motib79 avatar Mar 26 '24 15:03 motib79

Thanks @motib79.

Using global for 'ticker' but no assignment is done - in other words, the global ticker line is not necessary and can be removed.

Here is where global would be necessary:

ticker = {1: 2}

def remove_ticker_from_list(stock_index):
    global ticker
    ticker = "NULL_STOCK"

remove_ticker_from_list('x')
print(ticker)

NULL_STOCK

Perhaps the documentation could be updated to make it more clear?

mbyrnepr2 avatar Mar 28 '24 13:03 mbyrnepr2

Thanks for the comments. Ticker is a list. And modified in the scope of the procedure. Therefore I believe global assignment is required as like in case of any other variable changes. Looks like PyLint does not recognize list change as a change that require global assignment .

On Thu, 28 Mar 2024, 15:54 Mark Byrne, @.***> wrote:

Thanks @motib79 https://github.com/motib79.

Using global for 'ticker' but no assignment is done - in other words, the global ticker line is not necessary and can be removed.

Here is where global would be necessary:

ticker = {1: 2} def remove_ticker_from_list(stock_index): global ticker ticker = "NULL_STOCK" remove_ticker_from_list('x')print(ticker) NULL_STOCK

Perhaps the documentation https://pylint.readthedocs.io/en/stable/user_guide/messages/warning/global-variable-not-assigned.html could be updated to make it more clear?

— Reply to this email directly, view it on GitHub https://github.com/pylint-dev/pylint/issues/9522#issuecomment-2025242710, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFVJFIJONZTXW2QURDFDPUDY2QHHDAVCNFSM6AAAAABFJGADIKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRVGI2DENZRGA . You are receiving this because you were mentioned.Message ID: @.***>

motib79 avatar Mar 29 '24 04:03 motib79

@motib79 I think that is the same situation for list or dict. I think this is your example (expanded a bit to show the list definition outside the function). Python is happy with this and global isn't needed here because you are not re-assigning ticker but rather some element contained within ticker:

(venv312) markbyrne@Marks-Air-2 programming % cat example.py 
ticker = [1, 2, 3]


def remove_ticker_from_list(stock_index):
    ticker[stock_index]="NULL_STOCK"

 
remove_ticker_from_list(0)
print(ticker)
(venv312) markbyrne@Marks-Air-2 programming % python example.py
['NULL_STOCK', 2, 3]

mbyrnepr2 avatar Mar 29 '24 12:03 mbyrnepr2

Ok, thanks for the clarification.

On Fri, 29 Mar 2024, 15:36 Mark Byrne, @.***> wrote:

@motib79 https://github.com/motib79 I think that is the same situation for list or dict. I think this is your example (expanded a bit to show the list definition outside the function). Python is happy with this and global isn't needed here because you are not re-assigning ticker but rather some element contained within ticker. Global would be needed if ticker were a string, for example, because the assignment inside the function wouldn't affect ticker which is defined outside the function:

(venv312) @.*** programming % cat example.py ticker = [1, 2, 3]

def remove_ticker_from_list(stock_index): ticker[stock_index]="NULL_STOCK"

remove_ticker_from_list(0)print(ticker) (venv312) @.*** programming % python example.py ['NULL_STOCK', 2, 3]

— Reply to this email directly, view it on GitHub https://github.com/pylint-dev/pylint/issues/9522#issuecomment-2027186081, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFVJFILKBCMV473TY7EWLNLY2VG4TAVCNFSM6AAAAABFJGADIKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRXGE4DMMBYGE . You are receiving this because you were mentioned.Message ID: @.***>

motib79 avatar Mar 29 '24 13:03 motib79