Skip to content

Use f-string type conversion !r in error messages#4409

Open
Copilot wants to merge 9 commits intomainfrom
copilot/use-fstring-type-conversion
Open

Use f-string type conversion !r in error messages#4409
Copilot wants to merge 9 commits intomainfrom
copilot/use-fstring-type-conversion

Conversation

Copy link

Copilot AI commented Feb 12, 2026

Updated remaining instances of manual quote formatting to use !r type conversion for consistency with the rest of the codebase.

Changes

pygmt/helpers/decorators.py (2 docstring examples)

  • f"'{k}': {repr(kwargs[k])}"f"{k!r}: {kwargs[k]!r}"

pygmt/tests/test_clib_loading.py (10 test patterns)

  • Test expectations and mock error messages updated to match !r format
  • Regex patterns: rf"Error loading '{var}'"rf"Error loading {var!r}"

Example

# Before
", ".join(f"'{k}': {repr(kwargs[k])}" for k in sorted(kwargs))
msg = rf"Error loading '{libgmt}'. Couldn't access function GMT_Create_Session."

# After
", ".join(f"{k!r}: {kwargs[k]!r}" for k in sorted(kwargs))
msg = rf"Error loading {libgmt!r}. Couldn't access function GMT_Create_Session."

This completes the conversion started in earlier commits, ensuring all error messages and test patterns use the more concise !r syntax.

Original prompt

This section details on the original issue you should resolve

<issue_title>Use f-string type conversion !r in error messages to avoid single quotes</issue_title>
<issue_description>Currently, the error messages are in the following style:

>>> arg = "invalid"
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: 'invalid'.

>>> arg = 10
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: '10'.

>>> arg = None
>>> msg = f"Invalid value: '{arg}'."
>>> print(msg)
Invalid value: 'None'.

The cons are:

  • Extra single quotes in f-strings
  • Values are always enclosed in single quotes, regardless of the types

I propose to use type conversion !r in the new style:

>>> arg = "invalid"
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: 'invalid'.

>>> arg = 10
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: 10.

>>> arg = None
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: None.

>>> arg = [10, 20, 30]
>>> msg = f"Invalid value: {arg!r}."
>>> print(msg)
Invalid value: [10, 20, 30].

👍 if you like new style; 👎 if you like the old style; or leave your comments.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 12, 2026 13:05
Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Copilot AI changed the title [WIP] Use f-string type conversion in error messages Use f-string !r conversion in error messages Feb 12, 2026
Copilot AI requested a review from seisman February 12, 2026 13:10
Convert objects to string before using !r to ensure proper formatting for both regular strings and test objects like FakedLibGMT.

Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Copilot AI and others added 2 commits February 12, 2026 13:21
The source code correctly uses !r conversion. Tests needed to be fixed by adding a __repr__ method to FakedLibGMT class so it formats correctly with !r.

Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Copilot AI changed the title Use f-string !r conversion in error messages Use f-string !r type conversion in error messages Feb 12, 2026
Updated docstring examples in decorators.py and test expectations in test_clib_loading.py to consistently use !r conversion instead of manual quotes.

Co-authored-by: seisman <3974108+seisman@users.noreply.github.com>
Copilot AI changed the title Use f-string !r type conversion in error messages Complete f-string !r conversion in docstrings and test patterns Feb 12, 2026
@seisman seisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Feb 12, 2026
@seisman seisman added this to the 0.19.0 milestone Feb 12, 2026
@seisman seisman marked this pull request as ready for review February 12, 2026 13:41
@seisman seisman changed the title Complete f-string !r conversion in docstrings and test patterns Use f-string type conversion !r in error messages Feb 12, 2026
@seisman
Copy link
Member

seisman commented Feb 12, 2026

This PR is made by Copilot, and it looks pretty good.

@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

final review call This PR requires final review and approval from a second reviewer maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use f-string type conversion !r in error messages to avoid single quotes

2 participants