You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fmt_docstring: Allow raw curly braces {} to avoid KeyError #3815
The @fmt_docstring decorator is responsible for inserting common docstrings into placeholders. While it works well in general, it currently raises a KeyError when encountering an undefined placeholder. For example, in #3811, we're writing equations in ReST syntax:
:math:`mantissa * 10 ^ {exponent}`
and it reports KeyError: exponent because we don't have exponent defined in COMMON_DOCSTRINGS. This requires us to escape such placeholders by writing the equation like
:math:`mantissa * 10 ^ {{exponent}}`
which is inconvenient, and sometimes we may forget to do it and then see the KeyError exception surprisingly.
Current Behavior:
If {exponent} (or any other undefined placeholder) appears in a docstring, fmt_docstring raises a KeyError.
To work around this, users must write {{exponent}} to ensure the curly braces are preserved in the final docstring.
Expected Behavior:
{exponent} (or any other undefined placeholder) should be treated as a literal string unless it matches a defined common docstring key.
When writing docstrings, we don't have to manually escape curly braces unless explicitly required.
Pros
No need to escape curly braces, especially for equations
The raw docstrings, especially math equations, are more readable
Cons
If there are any typos in common placeholder (e.g., projection typed as projecti), then the literal string {projecti} is shown in the HTML documentation. It no longer raises a KeyError exception, and we may miss the typo.
Are you willing to help implement and maintain this feature?
Using substitutions | | sounds an interesting idea. As far as I know, substituions don't support multi-paragraph long texts, so it may not work as expected.
Initially, I considered using Python's str.format_map, since it supports missing keys via a custom __missing__ method. However, it does not support nested curly braces, which appear frequently in LaTeX equations. For example:
Activity
seisman commentedon Feb 25, 2025
@GenericMappingTools/pygmt-maintainers Any comments on this?
weiji14 commentedon Mar 2, 2025
Yeah, it would be nice to write LaTeX style math equations directly without the double curly braces
{{ }}. It's not just ReST syntax, but also MyST that uses it - https://mystmd.org/guide/math.Sphinx does support substitutions via a
| |syntax for ReST - https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#substitutions, that might be coming to MyST in some form - jupyter-book/mystmd#852. Maybe we can consider using this built-in method than complicating the@fmt_docstringdecorator function further? But it will involve a lot of rewriting of the docstrings to do{ }->| |for the aliases.seisman commentedon Mar 3, 2025
Using substitutions
| |sounds an interesting idea. As far as I know, substituions don't support multi-paragraph long texts, so it may not work as expected.seisman commentedon Nov 15, 2025
I'm working on this issue in PR #4210.
Initially, I considered using Python's str.format_map, since it supports missing keys via a custom
__missing__method. However, it does not support nested curly braces, which appear frequently in LaTeX equations. For example:The second example fails because
.format_map()doesn't allow '{' in field name.After exploring alternatives, I found that Python's
string.Templateworks well for this use case:In
string.Template,$marks the placeholder, so curly braces (nested or not) remain intact while placeholders can still be safely substituted.I think
string.Templateis the best approach. To adopt it, we need to make two changes:{projection}to either${projection}or$projection(I prefer$projectionfor its simplicity).{{and}}in LaTeX equations with plain{and}.This ensures that math equations are clean, substitution is safe, and no
KeyErrororValueErroroccurs.Comments?