Skip to content

Conversation

@nathanjmcdougall
Copy link
Contributor

To avoid regressions like #1294 occurring in the future, I propose a different way of lazy-loading rich using importlib.util.find_spec. I also propose using the TID Ruff rules to enforce the lazy-loading logic in the future.

This provides a framework to systematically apply other lazy-loading migrations safely.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review September 8, 2025 06:06
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@svlandeg
Copy link
Member

svlandeg commented Sep 8, 2025

Thanks for working on this! I'm putting this in draft as long as the CI fails - feel free to mark as ready for review when the CI goed green!

@svlandeg svlandeg marked this pull request as draft September 8, 2025 07:08
@svlandeg svlandeg changed the title Use Ruff rules to enforce rich lazy-loading safely ✅ Use Ruff rules to ensure safe lazy-loading of rich Sep 8, 2025
@github-actions

This comment was marked as outdated.

@nathanjmcdougall nathanjmcdougall force-pushed the config/lazy-load-rich-via-ruff branch from 86aeb12 to 4e75183 Compare September 8, 2025 20:58
@github-actions

This comment was marked as outdated.

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review September 8, 2025 21:08
@github-actions

This comment was marked as outdated.

@nathanjmcdougall
Copy link
Contributor Author

@svlandeg Alright, I think it's good now. I can't add labels so that check's failing, but everything else is green.

@svlandeg svlandeg added the feature New feature, enhancement or request label Sep 9, 2025
@svlandeg svlandeg self-assigned this Sep 9, 2025
@svlandeg svlandeg removed their assignment Sep 9, 2025
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@nathanjmcdougall nathanjmcdougall force-pushed the config/lazy-load-rich-via-ruff branch from 669a873 to 562b36f Compare September 9, 2025 09:37
@nathanjmcdougall nathanjmcdougall force-pushed the config/lazy-load-rich-via-ruff branch from 562b36f to 4bbe297 Compare September 9, 2025 09:38
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg self-assigned this Sep 9, 2025
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

Great, so a recap.

This PR does a number of related things:

  1. Avoid importing rich_utils at the top of any module, ensuring its lazy loading from #1128 (flake8-tidy-imports)
  2. Avoid importing rich anywhere in the codebase, except in rich_utils.py and docs files (flake8-tidy-imports.banned-api)
  3. Refactor how to define and store HAS_RICH: using importlib.util.find_spec("rich") instead of a try-catch import in core.py
  4. Fetching the HAS_RICH var from core.py in both main.py and cli.py to avoid code duplication and have easier maintenance in the future
  5. Refactor the tests to use HAS_RICH and pytest.MonkeyPatch instead of setting the typer.core.rich variable directly.

To me, 1 & 2 are the crucial points of this PR: they ensure we're not importing rich or rich_utils by accident. This would have caught #1290 as well as a similar issue we encountered in #828: it will be nice to set things up so that we can't hit ourselves with this again 🥲

3 follows out of this because you can't import rich anymore, and in fact having a HAS_RICH variable feels like a nicer solution as well, as compared to our old solution of rich either being an imported library, or None.

4 was added to this PR by my suggestion as I think it's more clean to define this functionality only once and then import.

5, the refactoring of the tests, needs to happen anyway when switching from the rich variable check for None to the boolean HAS_RICH. Using MonkeyPatch to do this feels more elegant to me than what we had before.

So, all in all, this looks (really) good to me. Thanks again @nathanjmcdougall for all your work on this!

I'm passing this on internally to Tiangolo's reviewing queue, he will update here once he's had a chance to review 🙏

@svlandeg svlandeg removed their assignment Sep 9, 2025
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg added the repo / tests Involving the CI / test suite label Sep 9, 2025
@nathanjmcdougall
Copy link
Contributor Author

After this PR is done, I plan do the same but with shellingham.

@github-actions
Copy link
Contributor

📝 Docs preview for commit d8ffd89 at: https://07df5e78.typertiangolo.pages.dev

@tiangolo
Copy link
Member

Awesome, looks great! Thanks for all the work on this @nathanjmcdougall! 🙌

And thanks @svlandeg for the thorough explanation of everything and the reasons for it all. 🚀

@tiangolo tiangolo merged commit 661b49e into fastapi:master Sep 20, 2025
25 checks passed
@tiangolo tiangolo added refactor and removed feature New feature, enhancement or request labels Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor repo / tests Involving the CI / test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants