Skip to content
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

Generate __version__ at build to avoid slow importlib.metadata #30

Merged
merged 1 commit into from
Oct 12, 2024

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Oct 11, 2024

importlib.metadata is quite a heavy library to import, and can make CLI use less snappy.

It's only used for fetching the version, and we can pre-generate it as part of our regular build instead.

Before

python -X importtime -c "import blurb" 2> import.log && tuna import.log
image image

After

image image

Copy link

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Nice! The actual importlib.metadata.version call can be slow too, esp in larger envs (just checked and it was 15ms in a venv I'm in)

@larryhastings
Copy link
Contributor

I don't want to step in front of the steamroller of progress here, but what is this heavy ___version___ object? I thought the convention for __version__ was just a string.

@hugovk
Copy link
Member Author

hugovk commented Oct 11, 2024

Yep, __vesion__ is a string.

@@ -42,6 +42,9 @@ scripts.blurb = "blurb.blurb:main"
[tool.hatch]
version.source = "vcs"

[tool.hatch.build.hooks.vcs]
version-file = "src/blurb/_version.py"
Copy link
Member

Choose a reason for hiding this comment

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

Do these two lines tell hatch to automatically create the _version.py file that contains __version__ = '...' on build?

Copy link
Member Author

Choose a reason for hiding this comment

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

@hugovk hugovk merged commit bdcbac2 into python:main Oct 12, 2024
12 checks passed
@hugovk hugovk deleted the rm-importlib.metadata branch October 12, 2024 15:21
@hugovk hugovk changed the title Generate __version__ at build to avoid slow importlib.metadata import Generate __version__ at build to avoid slow importlib.metadata Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants