Skip to content

[MNT]: Not possible to set AutoDateFormatter rcParam to function without deprecation warning #23012

Open

Description

@oscargus

Summary

Not clear if it should be possible, but it is possible to use functions instead of format strings so one may think it should be possible to set the rcParam to a function as well. The documentation is a bit vague and do not really encourage changing them, so maybe it is not supposed to be possible.

`.AutoDateFormatter` has a ``.scale`` dictionary that maps tick scales (the
interval in days between one major tick) to format strings; this dictionary
defaults to ::

In [1]: mpl.rcParams['date.autoformatter.year'] = lambda a: str(a)
C:\Users\Oscar\AppData\Local\Temp\ipykernel_9940\331672118.py:1: MatplotlibDeprecationWarning: Support for setting an rcParam that expects a str value to a non-str value is deprecated since 3.5 and support will be removed two minor releases later.
  mpl.rcParams['date.autoformatter.year'] = lambda a: str(a)

Proposed fix

Add a "string or callable" verifier and use that.

Activity

  1. jklymak commented on May 19, 2022

    @jklymak
    Member

    Do we allow functions for any rcParams?

  2. timhoffm commented on May 19, 2022

    @timhoffm
    Member

    Not sure. If so, only when setting it at runtime. Obviously, one cannot set a function in config files. But if there is no technical limitation I don’t See a reason why who shouldn’t allow that at run time.

  3. tacaswell commented on May 19, 2022

    @tacaswell
    Member

    As long as the function in importable, we can do something like 'function://module.module:function' as we do with backends.

    I suspect our logic when deprecated this was that if you can not express it in the config file, it should not go in rcparams

  4. added this to the v3.7.0 milestone on May 19, 2022
  5. oscargus commented on May 20, 2022

    @oscargus
    MemberAuthor

    I guess that the example in the documentation actually tells you how it should be done if you want a function. If I recall correctly, I was just trying to test out some things for code coverage and maybe didn't really read the documentation that much in detail, but thought that it should be possible as I probably copied some other test code...

    So if the conclusion is to close this as it is OK and the documentation says so, I will not argue. (Possibly one can add a note that the only way to get a function is to do as in the example, now it can, maybe, be interpreted as one way to use a function, not the way).

  6. timhoffm commented on May 22, 2022

    @timhoffm
    Member

    As long as the function in importable, we can do something like 'function://module.module:function' as we do with backends.

    I wouldn't do that unless we have very good reasons. This is a proprietary pattern users would have to learn (reduces the chance that it gets used) and we would have to maintain it.

    Let's not take any actions here until users are asking for it.

    @oscargus feel free to clarify the documentation.

  7. timhoffm commented on May 22, 2022

    @timhoffm
    Member

    Ah, sorry. I'm against parsing functions from Matplotlibrc. It would be ok to modify the validator to support runtime-assignment.

  8. QuLogic commented on Jun 21, 2025

    @QuLogic
    Member

    This is now raising an error instead of warning:

    Traceback (most recent call last):
      File "<python-input-1>", line 1, in <module>
        mpl.rcParams['date.autoformatter.year'] = lambda a: str(a)
        ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File ".../lib/matplotlib/__init__.py", line 750, in __setitem__
        raise ValueError(f"Key {key}: {ve}") from None
    ValueError: Key date.autoformatter.year: Could not convert <function <lambda> at 0x7fd757c7cc20> to str
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions