0

I am trying to run a python script with custom args in a Github workflow.

When my script runs on a local machine it uses the package python-git-info to retreive the current git commit and pass it to the logger. But within the workflow, I can get the commit using $(git rev-parse "$GITHUB_SHA"). I want to pass that to my script as an arg.

But my script always ignores the passed arg and refers to the default value. What am I missing?

Github workflow:

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Performance Checker

on:
  pull_request:
    types: [ labeled ]

jobs:
  build:
    if: ${{ github.event.label.name == 'breaker' }}
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Test performance of all algorithms to ensure nothing broke
      run: |
        python train.py --git_commit=$(git rev-parse "$GITHUB_SHA") --load_config=configs/*

Relevant python arg:

parser.add_argument(
    '--git_commit',
    type=str,
    default=gitinfo.get_git_info()['commit'],
    help='current git commit')

Error from workflow:

Traceback (most recent call last):
  File "train.py", line 74, in <module>
    default=gitinfo.get_git_info()['commit'],
TypeError: 'NoneType' object is not subscriptable

This error mesasge indicates to me that the script ignores the passed args and refers to the default value. Any help or insight would be immensely appreciated.

5
  • Can you add git rev-parse "$GITHUB_SHA" as a separate step and check the output? Commented Mar 30, 2021 at 11:31
  • I did, and I get the commit value. So when I do echo "COMMIT $(git rev-parse "$GITHUB_SHA")" I get COMMIT 9940feaa831a52380cf74a5dc69c12bf47bc56f9. Commented Mar 30, 2021 at 11:32
  • I even tried passing any random string, but the script always defers to the default value. I don't have any way to check if the input is actually a commit or not. My code just accepts any string passed so I dont know why this behavior is happening. Commented Mar 30, 2021 at 11:34
  • Maybe add set -v before running the script so you can see what exactly is executed Commented Mar 30, 2021 at 11:36
  • 1
    I just understood what's going on. I'm posting an answer Commented Mar 30, 2021 at 11:36

1 Answer 1

2

As you can see in the error:

  File "train.py", line 74, in <module>
    default=gitinfo.get_git_info()['commit'],
TypeError: 'NoneType' object is not subscriptable

You are calling gitinfo.get_git_info() anyway (since you pass its value to parser.add_argument()), it returns None and you try to access None['commit'], which is why you get TypeError: 'NoneType' object is not subscriptable.

Try to use default=None and call gitinfo.get_git_info() only if the value of the git_commit parameter is None.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try your solution in a moment, but I'm a little confused. Why is gitinfo.get_git_info() being called anyway? Doesn't default only run if no argument is provided?
It's used only if no argument is provided, but when you call parser.add_argument() you pass a value to default. This is when gitinfo.get_git_info()['commit'] is evaluated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.