Skip to content

[MNT] Typing: Use Literal for set_loglevel #30302

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

Merged
merged 1 commit into from
Jul 20, 2025

Conversation

nrnavaneet
Copy link
Contributor

This PR addresses part of #30257 by improving type safety and IDE support for set_loglevel.

Changes in this PR:

  • Introduced LogLevel as a TypeAlias in matplotlib.typing, using Literal to restrict accepted values.
  • Updated set_loglevel() in init.py to use LogLevel.

This improves discoverability and static type checking for valid logging levels like "info", "debug", "error", etc.

Related work not included here
MarkerType updates were implemented by @ZPyrolink in #30261
Event type literals for connect methods were contributed by @ZPyrolink in #30275

@nrnavaneet
Copy link
Contributor Author

Hi all 👋,

I initially attempted to address multiple points from this issue in a single PR, but I realized that it introduced unnecessary complexity, especially for someone like me who is still learning and new to contributing at this scale.

To manage things better and ensure clarity in both review and implementation, I’ve now shifted to handling one fix at a time. I’ve started with set_loglevel as it seemed like a simpler, self-contained change to begin with.

This approach will also help me improve gradually and avoid making mistakes across unrelated parts. Thank you so much for your patience and guidance, it’s really encouraging for a beginner like me 🙏🙂

@timhoffm
Copy link
Member

Thanks. This is correct in the sense that it matches documentation now.

image

However, implementation-wise we've accepted any capitalization, in particular also thd capitalized form "INFO", which is passed through to/accepted by stdlibs Logger.setLevel - maybe we should take the opportunity to align more with the stdlib?

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's take this as is. And handle the question of aligning with stdlib as a follow-up topic.

@nrnavaneet
Copy link
Contributor Author

nrnavaneet commented Jul 14, 2025

Yes @timhoffm, you’re right. I’ve implemented this by normalising the input to lowercase within set_loglevel(), so it aligns with the stdlib behaviour. You think that will work?

Let me know if any further changes are required.
Thanks again for pointing that out

@nrnavaneet nrnavaneet requested a review from timhoffm July 17, 2025 06:36
@nrnavaneet nrnavaneet force-pushed the fix/literal-typing-setloglevel branch from 1f29d69 to 0555206 Compare July 17, 2025 07:35
@dstansby dstansby added this to the v3.11.0 milestone Jul 20, 2025
@dstansby dstansby merged commit 53af231 into matplotlib:main Jul 20, 2025
69 checks passed
@nrnavaneet nrnavaneet deleted the fix/literal-typing-setloglevel branch July 21, 2025 13:24
timhoffm added a commit to timhoffm/matplotlib that referenced this pull request Jul 21, 2025
matplotlib#30302 just introduced literals for the log levels. Previously, the
situation was a bit vague: While the docstring described the levels
as all lowercase, in fact any casing is accepted. This PR changes the
preferred casing to be all-capitalized in analogy to the logging
standard library, which only supports this casing. Lowercase remains
supported because it's not worth an API breakage.

We might later consider to also accept logging levels directly, i.e.
`logging.DEBUG` to be more congruent with the logging library. But this
can be done any time. It's important to get this PR in soon to not
release the typing Literal in lowercase form.
timhoffm added a commit that referenced this pull request Jul 22, 2025
* MNT: Prefer capitalized logging levels

#30302 just introduced literals for the log levels. Previously, the
situation was a bit vague: While the docstring described the levels
as all lowercase, in fact any casing is accepted. This PR changes the
preferred casing to be all-capitalized in analogy to the logging
standard library, which only supports this casing. Lowercase remains
supported because it's not worth an API breakage.

We might later consider to also accept logging levels directly, i.e.
`logging.DEBUG` to be more congruent with the logging library. But this
can be done any time. It's important to get this PR in soon to not
release the typing Literal in lowercase form.

* Update lib/matplotlib/__init__.py

Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>

---------

Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy