Skip to content

[Bug]: Pyparsing warnings emitted in mathtext #25204

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

Closed
jmd-dk opened this issue Feb 12, 2023 · 4 comments · Fixed by #27685
Closed

[Bug]: Pyparsing warnings emitted in mathtext #25204

jmd-dk opened this issue Feb 12, 2023 · 4 comments · Fixed by #27685

Comments

@jmd-dk
Copy link

jmd-dk commented Feb 12, 2023

Bug summary

When plotting anything with at least one logarithmic axes (plt.semilogx(), plt.semilogy(), plt.loglog()) I get two warnings emitted due to code in _mathtext.py. These warnings are not emitted by default, but appears when running Python with e.g. -Wdefault.

Code for reproduction

import matplotlib.pyplot as plt
plt.semilogx([1, 2], [3, 4])
plt.savefig('plot.png')

Actual outcome

/path/to/python/lib/python3.9/site-packages/matplotlib/_mathtext.py:1871: UserWarning: warn_name_set_on_empty_Forward: setting results name 'sym' on Forward expression that has no contained expression
  - p.placeable("sym"))
/path/to/python/lib/python3.9/site-packages/matplotlib/_mathtext.py:1876: UserWarning: warn_ungrouped_named_tokens_in_collection: setting results name 'name' on ZeroOrMore expression collides with 'name' on contained expression
  "{" + ZeroOrMore(p.simple | p.unknown_symbol)("name") + "}")

Expected outcome

No warnings/text should be printed.

Additional information

To see the warnings from the above code, save it to e.g. test.py and run it as either

python -Wdefault test.py

or

PYTHONWARNINGS=default python test.py

The two warnings are triggered by the following lines within matplotlib/_mathtext.py:

        p.accent <<= (
            csnames("accent", [*self._accent_map, *self._wide_accents])
            - p.placeable("sym")
        p.operatorname <<= cmd(
            r"\operatorname",
            "{" + ZeroOrMore(p.simple | p.unknown_symbol)("name") + "}")

where the various attributes on p are instances of pyparsing.Forward.

The warnings appear with Pyparsing versions 3.0.0–3.0.9 but not prior to 3.0.0 (e.g. 2.4.7). Also, the warnings only appear for Matplotlib versions 3.6.0–3.6.3, not e.g. 3.5.3

Operating system

Linux Mint 20

Matplotlib Version

3.6.3

Matplotlib Backend

QtAgg

Python version

3.8.5

Installation

pip

@rcomer
Copy link
Member

rcomer commented Feb 12, 2023

Thanks for the clear report @jmd-dk. After a couple of false starts (!) I have reproduced this with main:

/home/ruth/git_stuff/matplotlib/lib/matplotlib/_mathtext.py:1880: UserWarning: warn_name_set_on_empty_Forward: setting results name 'sym' on Forward expression that has no contained expression
  - p.placeable("sym"))
/home/ruth/git_stuff/matplotlib/lib/matplotlib/_mathtext.py:1885: UserWarning: warn_ungrouped_named_tokens_in_collection: setting results name 'name' on ZeroOrMore expression collides with 'name' on contained expression
  "{" + ZeroOrMore(p.simple | p.unknown_symbol)("name") + "}")

@anntzer
Copy link
Contributor

anntzer commented Feb 12, 2023

The second warning is fixed by #25200 (which was coincidentally opened just before this bug report :)).

@anntzer
Copy link
Contributor

anntzer commented Feb 12, 2023

The other warning can be suppressed with

diff --git i/lib/matplotlib/_mathtext.py w/lib/matplotlib/_mathtext.py
index b41dca9d69..71af0dfdea 100644
--- i/lib/matplotlib/_mathtext.py
+++ w/lib/matplotlib/_mathtext.py
@@ -13,6 +13,7 @@ import types
 import unicodedata

 import numpy as np
+import pyparsing
 from pyparsing import (
     Empty, Forward, Literal, NotAny, oneOf, OneOrMore, Optional,
     ParseBaseException, ParseException, ParseExpression, ParseFatalException,
@@ -1875,9 +1876,11 @@ class Parser:

         p.customspace <<= cmd(r"\hspace", "{" + p.float_literal("space") + "}")

-        p.accent <<= (
-            csnames("accent", [*self._accent_map, *self._wide_accents])
-            - p.placeable("sym"))
+        with cbook._setattr_cm(
+                pyparsing.__diag__, warn_name_set_on_empty_Forward=False):
+            p.accent <<= (
+                csnames("accent", [*self._accent_map, *self._wide_accents])
+                - p.placeable("sym"))

         p.function <<= csnames("name", self._function_names)
         p.operatorname <<= cmd(

but it's not really clear to me whether it's actually warning about a problematic practice or not (one could move the definition of placeable above the one of accent to suppress the warning, but that actually changes the parsing behavior.

@QuLogic QuLogic added this to the v3.7.1 milestone Feb 23, 2023
rjgildea added a commit to dials/dials that referenced this issue Mar 2, 2023
@QuLogic QuLogic modified the milestones: v3.7.1, v3.7.2 Mar 4, 2023
@QuLogic QuLogic modified the milestones: v3.7.2, v3.7.3 Jul 5, 2023
@QuLogic QuLogic modified the milestones: v3.7.3, v3.8.0 Sep 9, 2023
@ksunden ksunden removed this from the v3.8.0 milestone Sep 15, 2023
@slayoo
Copy link

slayoo commented Jan 22, 2024

just came across the very same problem which made our CI pipeline "red" (as run with python -We) after a matplotlib update - would be great address this issue!
Thanks

@tacaswell tacaswell added this to the v3.9.0 milestone Jan 22, 2024
ksunden added a commit to ksunden/matplotlib that referenced this issue Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants