-
-
Notifications
You must be signed in to change notification settings - Fork 744
⚡️ Lazy-load rich_utils
to reduce startup time
#1128
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sys | ||
|
||
import typer | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main(): | ||
for m in sys.modules: | ||
print(m) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||||||||||||||||||
import subprocess | ||||||||||||||||||||||
import sys | ||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||
|
||||||||||||||||||||||
ACCEPTED_MODULES = {"rich._extension", "rich"} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_rich_not_imported_unnecessary(): | ||||||||||||||||||||||
file_path = Path(__file__).parent / "assets/print_modules.py" | ||||||||||||||||||||||
result = subprocess.run( | ||||||||||||||||||||||
[sys.executable, "-m", "coverage", "run", str(file_path)], | ||||||||||||||||||||||
capture_output=True, | ||||||||||||||||||||||
encoding="utf-8", | ||||||||||||||||||||||
) | ||||||||||||||||||||||
modules = result.stdout.splitlines() | ||||||||||||||||||||||
modules = [ | ||||||||||||||||||||||
module | ||||||||||||||||||||||
for module in modules | ||||||||||||||||||||||
if module not in ACCEPTED_MODULES and module.startswith("rich") | ||||||||||||||||||||||
] | ||||||||||||||||||||||
assert not modules | ||||||||||||||||||||||
Comment on lines
+16
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point!
Suggested change
is even better I guess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all good suggestions, however in addition to the readability of the code, we should also consider the readability of the error messages. If an assertion fails, To demonstrate this, I changed ACCEPTED_MODULES to an empty set to cause a test failure. With
This tells me immediately which modules were unexpectedly imported, and thus helps finding the cause. With
This is a bit more lengthy and indirect, but I can still see which modules were unexpectedly imported. With
This is rather cryptic and unhelpful. I therefore prefer the first variant. I find the second variant (with However, in the end, this is @tiangolo's project, so I will use whatever he prefers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a matter of personal preference, but for clarity of code I would prefer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is arguably the most idiomatic style, see also PEP8:
But I can change it if you prefer.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave the final decision with Tiangolo 🙏
UPDATE: Note that I'm not suggesting to check truthness of
len(seq)
as in your code above, I'm suggesting to checklen(seq) == 0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are even pylint / Ruff rules that enforce this behavior: https://docs.astral.sh/ruff/rules/len-test/
assert not modules
is the right thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not reading this with the same interpretation. I'm not suggesting to check truthness of
len(x)
, I'm suggesting to explicitely check the length being zero, as suggested on the page you cited: