perf: hoist import datetime as dt to module scope in time.py - #394
Open
g4lb wants to merge 1 commit into
Open
Conversation
python-humanize#335 declared `__lazy_modules__` so module-scope imports become lazy on Python 3.15+, but only added the declarations — the function-level `import datetime as dt` statements predating it were left in place, costing a `sys.modules` lookup on every call. Move the import to module scope and add "datetime" to `__lazy_modules__`, the same pattern filesize.py already uses for `math`. The now-redundant `TYPE_CHECKING` import goes with it.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #394 +/- ##
==========================================
- Coverage 99.56% 99.56% -0.01%
==========================================
Files 12 12
Lines 927 921 -6
==========================================
- Hits 923 917 -6
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The companion to #393, for
time.py. #335 added__lazy_modules__so module-scopeimports become lazy on Python 3.15+, but it added the declarations only — the
function-level
import datetime as dtstatements that predated it were left behind.This hoists the seven of them to module scope, adds
"datetime"to__lazy_modules__, and drops the now-redundantTYPE_CHECKINGimport. Same patternfilesize.pyalready uses formath.The two doctest examples that contain
>>> import datetime as dtare untouched.Why
A function-level
importcosts asys.moduleslookup and a name binding on everycall. Measured on this repository's own
tests/test_benchmarks.pywithpytest-benchmark, 10 interleaved A/B rounds per side againstmain(Apple Silicon, CPython 3.14.6, medians):
test_naturaltimetest_naturaldatetest_naturaldaytest_naturaldeltaGeometric mean across all 15 benchmarks: 0.9764 (−2.36 %). No benchmark regressed
significantly; the remaining eleven are noise (all p > 0.35).
The aggregate is smaller than #393's because only the time functions are affected —
the other benchmarks dilute it. If you would rather see this measured on the time
benchmarks alone, say so and I will post that.
Import time
import humanizeis unchanged:datetimeis already resident by the time the packagefinishes importing, so moving the statement costs nothing at startup.
Checks
pytest tests/→ 724 passed, 74 skipped.ruff check,black --checkandmypyallclean on the changed file. (
pytest --doctest-modules src/humanize/time.pyhas onepre-existing failure in
naturaldelta, identical onmain— it needsdateutil,which I had not installed. Not related to this change.)
Relationship to #393
Independent — different files, no conflict, either can land first. #393 covers
number.pyandmath.How this was found
By pointing autor3search-python at
humanize: a harness that proposes a change, measures it against a pinned baselinewith interleaved A/B rounds, and keeps or discards it on the numbers. Every figure
above is a measurement, not an estimate.