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

ut.update_template_file(): fix IndexError due to special character + #1326

Merged
merged 3 commits into from
Feb 28, 2025

Conversation

MarritL
Copy link
Contributor

@MarritL MarritL commented Feb 26, 2025

Description of proposed changes

Fix IndexError in update_template_file when trying to set the value 'briging+phase_closure' on 'mintpy.unwrapError.method'.

Traceback (most recent call last):
  File "/opt/conda/bin/smallbaselineApp.py", line 10, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.10/site-packages/mintpy/cli/smallbaselineApp.py", line 209, in main
    run_smallbaselineApp(inps)
  File "/opt/conda/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 1154, in run_smallbaselineApp
    app.open()
  File "/opt/conda/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 79, in open
    self._read_template()
  File "/opt/conda/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 118, in _read_template
    self.templateFile = ut.update_template_file(self.templateFile, self.customTemplate)
  File "/opt/conda/lib/python3.10/site-packages/mintpy/utils/utils1.py", line 570, in update_template_file
    old_value_str = re.findall(value2search, line)[0]
    IndexError: list index out of range

With my debug messages:
DEBUG value2search: [\s]*bridging+phase_closure
DEBUG line: mintpy.unwrapError.method = bridging+phase_closure \n'

Reminders

  • Pass Pre-commit check (green)
  • Pass Codacy code review (green)
  • Pass Circle CI test (green)
  • Make sure that your code follows our style. Use the other functions/files as a basis.
  • If modifying functionality, describe changes to function behavior and arguments in a comment below the function declaration.

Summary by Sourcery

Bug Fixes:

  • Fixes an IndexError in the update_template_file function that occurred when processing special characters like '+' in template values. The fix ensures that these characters are properly escaped when constructing regular expressions for searching and replacing values in template files.

Copy link

welcome bot commented Feb 26, 2025

💖 Thanks for opening this pull request! Please check out our contributing guidelines. 💖
Keep in mind that all new features should be documented. It helps to write the comments next to the code or below your functions describing all arguments, and return types before writing the code. This will help you think about your code design and usually results in better code.

Copy link

sourcery-ai bot commented Feb 26, 2025

Reviewer's Guide by Sourcery

This pull request fixes an IndexError in the update_template_file function. The error occurred when the code attempted to find and replace a value containing a '+' character in a template file. The '+' character was not being properly escaped when constructing the regular expression, leading to an incorrect search pattern and the IndexError when no match was found.

Sequence diagram for update_template_file function

sequenceDiagram
  participant UT as utils1.py
  participant RE as re module

  UT->>UT: update_template_file(template_file, extra_dict)
  loop For each key-value pair in extra_dict
    UT->>UT: value2search = value
    UT->>UT: Escape special characters in value2search
    UT->>RE: re.findall(value2search, line)
    RE-->>UT: Returns a list of matches
    alt Match found
      UT->>UT: Replace old value with new value in line
    else Match not found
      UT->>UT: Log warning
    end
  end
  UT-->>UT: Return updated template content
Loading

Updated class diagram for utils1.py

classDiagram
  class utils1 {
    +update_template_file(template_file, extra_dict, delimiter='=')
  }
  note for utils1 "Fixes an IndexError in update_template_file when trying to set a value containing the '+' character."
Loading

File-Level Changes

Change Details Files
Properly escape the '+' character in the update_template_file function to avoid IndexError.
  • Added '+' to the list of special characters to be escaped when constructing the regular expression for searching and replacing values in the template file.
src/mintpy/utils/utils1.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codeautopilot bot commented Feb 26, 2025

PR Summary

This Pull Request addresses a bug in the update_template_file function within the mintpy library, specifically targeting an IndexError that arises when special characters, such as the + symbol, are present in template values. The modification involves updating the list of special characters to include +, ensuring these characters are properly escaped in regular expressions used for searching and replacing values in template files. This fix is crucial for maintaining the robustness of the template update process, particularly when dealing with complex template values like bridging+phase_closure.

Review Checklist

  • Pass Pre-commit check (green)
  • Pass Codacy code review (green)
  • Pass Circle CI test (green)
  • Ensure code follows the project's style guidelines, using other functions/files as a reference.
  • Describe changes to function behavior and arguments in a comment below the function declaration.

Suggestion

To further enhance the robustness of the update_template_file function, consider implementing unit tests that cover various scenarios involving special characters in template values. This will help prevent similar issues in the future and ensure the function behaves as expected across different use cases.

This comment was generated by AI. Information provided may be incorrect.

Current plan usage: 0%

Have feedback or need help?
Documentation
[email protected]

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @MarritL - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It's good you're escaping special characters, but consider doing it with a single call to re.escape.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@yunjunz yunjunz changed the title fix IndexError due to special character '+' ut.update_template_file(): fix IndexError due to special character + Feb 27, 2025
smallbaselineApp.TimeSeriesAnalysis.run_unwrap_error_correction(): ignore whitespace to support input method value of `bridging + phase_closure`
@yunjunz
Copy link
Member

yunjunz commented Feb 27, 2025

LGTM. Thank you @MarritL.

I was able to reproduce the error while switching between bridging+phase_closure and bridging + phase_closure, so I added one more commit to ensure it works as well.

UPDATE: My commit above failed the test, I will revert this and merge your changes only for now, will try this later.

@yunjunz yunjunz merged commit aafeb9d into insarlab:main Feb 28, 2025
8 checks passed
Copy link

welcome bot commented Feb 28, 2025

🎉 🎉 🎉 Congrats on merging your first pull request! We here at behaviorbot are proud of you! 🎉 🎉 🎉

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

Successfully merging this pull request may close these issues.

2 participants