Skip to content

[Bug]: Pyparsing warnings emitted in mathtext #25204

Closed

Description

@jmd-dk

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

Activity

  1. rcomer commented on Feb 12, 2023

    @rcomer
    Member

    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") + "}")
    
    
  2. anntzer commented on Feb 12, 2023

    @anntzer
    Contributor

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

  3. anntzer commented on Feb 12, 2023

    @anntzer
    Contributor

    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.

  4. added a commit that references this issue on Feb 20, 2023
  5. added this to the v3.7.1 milestone on Feb 23, 2023
  6. modified the milestones: v3.7.1, v3.7.2 on Mar 4, 2023
  7. modified the milestones: v3.7.2, v3.7.3 on Jul 5, 2023
  8. modified the milestones: v3.7.3, v3.8.0 on Sep 9, 2023
  9. removed this from the v3.8.0 milestone on Sep 15, 2023
  10. slayoo commented on Jan 22, 2024

    @slayoo

    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

  11. added this to the v3.9.0 milestone on Jan 22, 2024
  12. added a commit that references this issue on Jan 22, 2024
    f2124dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions