Visitar URL original
[Bug]: Pyparsing warnings emitted in mathtext · Issue #25204 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20230323083627/https://github.com/matplotlib/matplotlib/issues/25204
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

[Bug]: Pyparsing warnings emitted in mathtext #25204

Open
jmd-dk opened this issue Feb 12, 2023 · 3 comments
Open

[Bug]: Pyparsing warnings emitted in mathtext #25204

jmd-dk opened this issue Feb 12, 2023 · 3 comments

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants