Skip to content

[MNT]: Cleanup internal import dependencies and initialization logic #29813

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

Open
timhoffm opened this issue Mar 26, 2025 · 1 comment
Open

[MNT]: Cleanup internal import dependencies and initialization logic #29813

timhoffm opened this issue Mar 26, 2025 · 1 comment

Comments

@timhoffm
Copy link
Member

timhoffm commented Mar 26, 2025

Summary

There's a delicate dependency on who goes first in the import order because we couple module dependencies (imports) and intialization (code execution at import).

See e.g. #29808 (comment)

Proposed fix

I think we can make this simpler by applying the following rules

  • no submodule is allowed to run intialization code (e.g. loading the colormaps) as part of their import
  • initialization should be lazy if possible
  • if lazy initialization is not possible, the initalization code must be placed in a _mpl_init() function in that module. ( (e.g. it's tricky for colormaps because the colormaps are injected as globals into the cm module)
  • only the top-level matplotlib module is allowed to call ._mpl_init() of the submodules.

This way, we can decouple initialization from import and have one central place that orchestrates the initialization.

What do you think?

@timhoffm timhoffm changed the title [MNT]: Cleanup internal import dependencies and setup of structures at import-time [MNT]: Cleanup internal import dependencies and initialization logic Mar 26, 2025
@oscargus
Copy link
Member

oscargus commented Apr 9, 2025

(With missing emoji-fonts, I hope that the first option is thumbs up...)

Seems to make sense! I guess the first thing is to add the mechanism to run _mpl_init() and then one can do this step-by-step and figure out any complications as one realizes them? Also, even if this turns out to be not fully workable, it will still be an improvement when it can be applied (it can probably be applied everywhere though).

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

2 participants