Aashutosh Kumar
Aashutosh Kumar

Reputation: 831

Python List comprehension error in Github-Actions

I need to create a list from input passed as command line argument to python script. Input contains items separated by either comma or space. I am using list comprehension & filter to get desired output in list containing each items/elements without comma or space.

When using Python List comprehension on Github-Actions, code sample below

import sys
payload=sys.argv
print(f'Payloads= {payload}')
gbsprf_key  = sys.argv[1]
issueLinks = sys.argv[2:]
print(f'IssueLinks before conversion = {issueLinks}')

#Make list of keys, remove commas
issueLinks = list(filter(None, [subitem for subitem in item.split(',') for item in issueLinks]))

getting below error, but it works fine my local system.

Run python link-regular-adhoc-payment.py GBSPRF-1899 GBSAP-20628,GBSAP-20029
Traceback (most recent call last):
Payloads= ['link-regular-adhoc-payment.py', 'GBSPRF-1899', 'GBSAP-20628,GBSAP-20029']
IssueLinks before conversion = ['GBSAP-20628,GBSAP-20029']
  File "/home/runner/work/jiraCloud-regular-adhoc-link/jiraCloud-regular-adhoc-link/link-regular-adhoc-payment.py", line 83, in <module>
    issueLinks = list(filter(None, [ subitem for subitem in item.split(',') for item in issueLinks ]))
NameError: name 'item' is not defined. Did you mean: 'iter'?
Error: Process completed with exit code 1.

But when using normal nested for loop example below it works just fine. I tested in python 3.9 & 3.10, both same error.

newList = []
for item in issueLinks:
    for subitem in item.split(','):
        newList.append(subitem)

Github-Actions workflow file.

name: regular-adhoc-link
on: repository_dispatch  
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-python@v4
      with:
        python-version: '3.10' 
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        #pip install -r requirements.txt
        pip install requests
    - name: Link Regular& Ad Hoc Payment Requests
      run: |
        python link-regular-adhoc-payment.py ${{ github.event.client_payload.key }} ${{ github.event.client_payload.issueLinks }}

Upvotes: 1

Views: 73

Answers (1)

Jay
Jay

Reputation: 2471

Looks like the order of your list comprehension is wrong, to reproduce -

>>> issueLinks = ['GBSAP-20628,GBSAP-20029']
>>> list(filter(None, [subitem for subitem in item.split(',') for item in issueLinks]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'item' is not defined. Did you mean: 'iter'?

It should be like this instead -

>>> issueLinks = ['GBSAP-20628,GBSAP-20029']
>>> list(filter(None, [subitem for item in issueLinks for subitem in item.split(',')]))
['GBSAP-20628', 'GBSAP-20029']

Upvotes: 1

Related Questions