Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
motib79 opened this issue Mar 26, 2024 · 4 comments

Comments

@motib79
Copy link

motib79 commented Mar 26, 2024

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 motib79 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 26, 2024
@mbyrnepr2
Copy link
Member

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 mbyrnepr2 added Documentation 📗 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 28, 2024
@motib79
Copy link
Author

motib79 commented Mar 29, 2024 via email

@mbyrnepr2
Copy link
Member

mbyrnepr2 commented Mar 29, 2024

@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]

@motib79
Copy link
Author

motib79 commented Mar 29, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants