Repository navigation
base: base class to collect any user-defined attribute in user class - #55
Conversation
Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>
593464d to
4ef0f4c
Compare
- Depends on airspeed-velocity/asv_runner#55 (asv-runner >= 0.3.2) - Closes airspeed-velocity#1617 Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>
- Depends on airspeed-velocity/asv_runner#55 (asv-runner >= 0.3.2) - Closes airspeed-velocity#1617 Signed-off-by: Minh Quan Ho <minh-quan.ho@sipearl.com>
HaoZeke
left a comment
There was a problem hiding this comment.
The collect helper is defined at module scope then called as self._collect_dynamic_attrs(func). That is an AttributeError on every Benchmark(...). Hang it on the class, or call _collect_dynamic_attrs(self, func).
already_set = set(vars(self)) runs after self.unit = "unit", so func.unit is never copied. Track still gets unit from _get_first_attr. higher_is_better would copy once the call actually works.
vars(func) misses a class-level higher_is_better = True that attr_sources already walks. The 1617 example is on the function so it is fine; walk attr_sources if you want the class default.
Drop the French comment. Add a test that TrackBenchmark constructs and that higher_is_better lands on the instance (discovery dumps __dict__ bools into the JSON asv 1621 reads).
Can you develop if this comment describes the behavior before (or after) this PR ? And what it should be like ?
I'm not sure to understand which call you are mentioning. The goal of this PR is not to add an explicit new
I don't think a class-level |
d07cf81 to
f7a5161
Compare
|
@HaoZeke can you give a look at the updated branch pls ? and tell me if it addressed your raised concerns. |
HaoZeke
left a comment
There was a problem hiding this comment.
Thanks, the constructor works now and the new tests pass (4 new, 30 in the suite). Two things left.
ruff format --check fails on asv_runner/benchmarks/_base.py: the blank line above class Benchmark is gone, and the new if is over the line length with single quotes.
already_set only has instance attributes, so a plain value on the function can shadow a method on the class:
class Bench:
def track_x(self):
return 1
track_x.run = "oops"With that, TrackBenchmark("track_x", b.track_x, [b.track_x, b]).run is the string, and .run() raises TypeError: 'str' object is not callable. Skipping anything type(self) already has fixes it:
already_set = set(vars(self))
for attr_name, value in getattr(func, "__dict__", {}).items():
if (
attr_name.startswith("_")
or attr_name in already_set
or hasattr(type(self), attr_name)
or callable(value)
):
continue
setattr(self, attr_name, value)getattr(func, "__dict__", {}) also covers callables without a __dict__, which vars() raises on. With that change, a track_mem_bw.run = "not a method" line in the dummy, and a test that self.benchmark.run is still callable, the suite passes (31) and ruff format --check is clean.
Per-function attributes over a class default is fine by me, that matches #1617.
|
Thanks @HaoZeke , I pushed a new commit including your suggestion. |
HaoZeke
left a comment
There was a problem hiding this comment.
Thanks, the Oct 12 commit covers the shadowing case from the last review. hasattr(type(self), attr_name) and getattr(func, "__dict__", {}) are in place, and the existing tests still pass the points I care about (construct, copy user attrs, leave unit alone, skip callables and privates).
One small follow-up if you have a moment: add track_mem_bw.run = "not a method" on the dummy and assert self.benchmark.run is still callable. That is the exact case that used to break, and it is not in test_dynamic_attrs.py yet. Happy to merge without it if you prefer a separate PR.
171aafe to
0af061d
Compare
|
@HaoZeke I pushed a a new test on I also expect a new tag asv-runner-0.3.2 released, so airspeed-velocity/asv#1621 can be unblocked, too. Thanks. |
|
Documentation preview for this PR: documentation.zip. Unzip and open |
To enable users to add any further custom attribute in their benchmark .py scripts, e.g.