Repository navigation
fix: correct slice naming, argname star handling and frame errors; 6x speedup - #122
Merged
Merged
Conversation
Several edge cases returned wrong results, or leaked an AttributeError out
of the `executing` dependency instead of raising the documented error:
- `node_name()` dropped the step of a step-only slice, so `c[::2] = f()`
was named `c[:]`; nested subscripts (`c[[[1]]]`) raised TypeError
- `argname()` never cleared its `*` flag, so an argument that followed
`*args` was expanded too: `argname("*args", "multi")` returned the
characters of the value instead of its name
- a frame index past the end of the stack raised AttributeError rather
than VarnameRetrievingError, and did not return None for
`raise_exc=False`; a negative index did the same
- `get_node_by_frame(None)` raised AttributeError
- `exec_code()` left its temporary file on disk when the call failed
- `make_readme.py` leaked two `codeblock_*` files per run
- `will()`'s Raises section named the wrong trigger for ImproperUseError:
the condition is whether the call site has an attribute, not whether the
caller is a method of a class instance
Performance work in the same code paths. A workload that calls `varname`,
`nameof`, `argname` and `will` 3000 times each, including a 16-frame stack
and a decorator wrapper, went from 2.198s to 0.359s (6.0x, 11 interleaved
rounds, both arms asserted to print identical output):
- walk outer frames lazily over `f_back` instead of materialising a full
`inspect.getouterframes()` list per call
- memoise `path.realpath` for frame filenames and preset ignore paths
- memoise `inspect.signature` for plain functions with no `__signature__`
- precompile the two `argname` regexes
The README documented `__setattr` where the attribute is `__setattr__`.
The notebook had not been executed since 2025-06-20, so its stored outputs still pointed at `core.py:106` and `module_all_calls.py:6` while a later `style:` commit had moved those lines to `:109` and `:7`. - print the source of each `module_*.py` before the demo that ignores it, instead of importing a file the reader cannot see - resolve the playground directory in the first cell, so the notebook runs from the repository root as well as from `playground/` - replace the stale `# argname is superseded by argname` comment, a broken edit of "superseded by argname2" (`argname2` was removed in v0.12), with what `argname` actually takes - re-execute and store the outputs: 68 cells, no errors
pwwang
added a commit
that referenced
this pull request
Dec 1, 2026
Behaviour changes from #122, plus one breaking change: a frame index past the end of the call stack now raises VarnameRetrievingError instead of leaking an AttributeError from `executing`, and returns None for `raise_exc=False`. The version now lives only in `varname/__init__.py`. `pyproject.toml` declares `dynamic = ["version"]` and `tool.hatch.version` points at that file, so a release bumps one line instead of three files, and `uv.lock` no longer records a version for the package at all.
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.
Summary
Audit and performance pass over the library. Fixes ten failure cases across six defects that are proven broken on
master, plus a 6x speedup on a representative workload. Every claim below was produced by running the code on bothmasterand this branch.Behaviour fixes
Each case fails on
masterand produces the correct result here.masternode_name()on a step-only slicec[::2] = f()namedc[:];c[1::2]namedc[1:]node_name()on nested subscriptsc[[[1]]]andc[[1], [2]]raiseTypeErrorargname("*args", "multi")after a*argsargument('first','second','t','h','i','r','d')('first','second','third')AttributeErrorfromexecutingVarnameRetrievingErrorraise_exc=FalseAttributeErrorNone, as documentedAttributeErrorVarnameRetrievingErrorget_node_by_frame(None)AttributeErrorNoneexec_code()with a badignoreTwo hygiene fixes:
make_readme.pyleaked twocodeblock_*temp files per run (now zero, measured at CLI level), and seven branches that could no longer be reached were removed (sys.version_info < (3, 8)/< (3, 9)shims and thePY311guard aroundreconstruct_func_node), whichpragma: no coverhad been hiding from the coverage report.The
will()docstring named the wrong trigger forImproperUseError. The Raises section said it fires whenwill()is not called inside a method or property of a class instance. That is false in both directions: it works in a plain free function, and it raises inside a method. The condition is whether the call site is followed by an attribute access. Reworded to say so.Performance
A workload calling
varname,nameof,argnameandwill3000 times each, including a 16-frame stack and a decorator wrapper:master6.1x, over 11 interleaved rounds. The benchmark asserts both arms resolve to different implementations and print identical output, so a reading of 1.0x cannot come from two identical arms.
Four changes carry it, each measured alone before the next was stacked:
f_backinstead of materialising a fullinspect.getouterframes()list (with source lines) on every callpath.realpathfor frame filenames and for preset ignore pathsinspect.signaturefor plain functions that have no__signature__argnameregexesOne optimisation was measured and reverted: sharing the default
IgnoreListelements bought 1.05x but changedIgnoreList.create().ignore_list ==fromFalsetoTrue, so it is not in this branch.fast-anything
fast-anything scanranksvarname.utils.get_nodefirst at 61.9% cumtime, butportrefuses it and every other hot function, because their arguments are frames and AST nodes rather than plain data:The ceiling was measured rather than assumed. A port replaces only a function's own time, so the best single candidate is worth 1.019x, and porting every candidate at 100% efficiency would remove 25.8% of the run. The 6x came from Python.
Testing
pyteston Python 3.9 / 3.10 / 3.11 / 3.12 / 3.13 / 3.14flake8 varnamemypy -p varnamemake_readme.py README.raw.mdvsREADME.mdplayground/New tests cover each fix above, which is why coverage stays at 100%.
Notes for the reviewer
masterdid not have. They are here because the docstrings already promisedVarnameRetrievingError, andraise_exc=Falsealready promisedNone.