Skip to content

Release 1.3.0: fix measurement bugs, add stats/async support, trusted publishing - #3

Merged
johanbeytell1 merged 3 commits into
mainfrom
arena/7dbefc7a-funcprofiler
Nov 18, 2026
Merged

johanbeytell1 merged 3 commits into
mainfrom
arena/7dbefc7a-funcprofiler

Conversation

@johanbeytell1

Copy link
Copy Markdown
Member

Summary

This PR is the v1.3.0 update: it fixes the defects that made measurements unreliable, adds the features that were missing (block profiling, aggregated statistics, async/generator support), and moves releases to PyPI trusted publishing.

Bugs fixed (each reproduced before fixing)

Defect Before After
@function_profile without parentheses TypeError: decorator() missing 1 required positional argument works
Profiled function raises tracemalloc left tracing forever; every later measurement wrong cleanup in finally, state always restored
Nested/recursive profiled calls nested call stopped the outer session (0.0 MB); caller's own tracemalloc session killed joins the session; outer peak kept, nested peak reported n/a instead of a fake zero
Line-by-line overhead traced every line of every frame (~5.5×) only the profiled code object (~1.2× with track_memory=False)
Recursion in line profiler one re-instrumented report per recursive call, clobbered timers single report, per-frame timers, correct attribution
Existing trace hook (coverage/debugger) destroyed with sys.settrace(None) restored
filename="report.json" wrote report.json.json no doubling; missing dirs created
HTML/Markdown exports unescaped args/return values, broken tables/injection escaped
YAML/TOML/XML exports invalid documents (unquoted keys with spaces, ''' blocks, illegal XML names) valid documents
CSV export manual quote wrapping, rows mixed across runs csv module, header once, Timestamp column
Bad export_format/log_level error after the function ran / silently ignored validated at decoration time; log_level="silent" added
shared_log=True one file per decoration second one file per process
Per-line memory cumulative tracemalloc peak (only ever increases) heap sampled while the line runs
No-source functions (exec, REPL) OSError: could not get source code source shown as ???
Async / generator functions silently reported ~0 s measured across awaited/consumed lifetime (async generators warn)
Empty line report export IndexError handled

Features added

  • profile_block(...) context manager.
  • Aggregated statistics: get_stats, reset_stats, summary_rows, print_summary, export_summary.
  • New options: track_memory, sample_every, include_args, include_return_value, max_value_length, append, export_dir, logger, sort_by, top_n.
  • log_level="silent" and logging.Logger sinks.
  • Async functions and generators measured correctly.
  • Percent column and human-readable units in line reports.
  • format_duration, format_memory, EXPORT_FORMATS, __version__, py.typed.

Behaviour changes

  • Line-by-line csv/html exports now overwrite by default; use append=True to accumulate runs.
  • Line-by-line exports gained Percent and Timestamp columns.
  • Exporters no longer print; the decorators report Report exported to: ….
  • The console line-by-line report is now an aligned table.
  • Minimum Python is 3.8; CI covers 3.8 – 3.13.

Migration notes are in the README changelog.

Release engineering

  • PyPI trusted publishing (OIDC): the publish job uses id-token: write and pypa/gh-action-pypi-publish@v1.14.3; no API token secret.
  • Split build and publish jobs with artifacts, so only the minimal publish job holds an OIDC token.
  • Tag validation before building (v1.3.0 → 1.3.0), version stamping in setup.py and funcprofiler/__init__.py, twine check --strict.
  • PEP 740 attestations on; concurrency guard added.
  • Actions bumped: checkout v7.0.1, setup-python v7.1.0, upload-artifact v7.0.2, download-artifact v8.0.2, gh-action-pypi-publish v1.14.3.
  • New tests.yml workflow (Python 3.8 – 3.13 + build check).
  • The PyPI publisher is configured without an environment, so the publish job deliberately has no environment: block.

Before merging: add the pending/trusted publisher on PyPI with owner Infinitode, repository FuncProfiler, workflow build_and_publish.yml, environment blank.

Testing

  • 68 unit tests (python -m unittest test -v) — all pass.
  • ruff check --select E,W,F clean; Python 3.8 syntax verified.
  • Wheel/sdist build cleanly; wheel contains all modules + py.typed; twine check --strict passes.
  • Simulated the release stamp step: v1.3.0 → funcprofiler-1.3.0-*.whl, 2.0.0-rc1 → 2.0.0rc1; branch-like tags fail fast.
  • Workflow YAML validated.

Checklist

  • Tests added/updated
  • Documentation (README changelog, usage, options, limitations, releasing)
  • No new runtime dependencies
  • Backwards-compatible public API signatures

Arena Agent and others added 3 commits November 18, 2026 10:59
Fix the bugs that made measurements unreliable and add the features
users asked for.

Correctness
- Support the bare @function_profile decorator (previously TypeError).
- Always stop tracemalloc and restore the previous trace hook, so an
  exception no longer leaves the profiler (and debuggers/coverage)
  broken for the rest of the process.
- Join an existing tracemalloc session instead of stopping it: nested
  and recursive profiled calls keep the outer peak, and a caller's own
  tracemalloc session survives.
- Return None from the global trace hook for unrelated frames. Line-by-
  line tracing now costs ~1.2x instead of tracing every line of every
  frame in the process.
- Aggregate recursive line-by-line calls into a single report and keep
  per-frame timers so recursion is timed correctly.
- Build export paths without doubling the extension, create missing
  directories, and never crash on functions without source.
- Escape HTML/Markdown values; emit valid YAML/TOML/XML (quoted keys,
  proper scalars, legal XML names, XML declaration).
- Use the csv module for CSV output; append with a single header and a
  run timestamp instead of mixing indistinguishable rows.
- Validate export_format/log_level at decoration time.
- Share one log file per process for shared_log=True.
- Sample per-line heap usage instead of reporting the monotonic
  tracemalloc peak.
- Handle empty line-by-line reports without IndexError.

Features
- profile_block() context manager.
- Aggregated stats: get_stats, reset_stats, summary_rows,
  print_summary, export_summary.
- track_memory, sample_every, include_args, include_return_value,
  max_value_length, append, export_dir, logger, sort_by, top_n.
- log_level="silent" and logging.Logger sinks.
- Async functions and generators are measured across their real
  lifetime; async generators warn instead of reporting ~0 s.
- Percent column and human-readable units in line reports.
- format_duration/format_memory helpers, EXPORT_FORMATS, __version__,
  py.typed.

Packaging and CI
- Split the module into core/exporters/stats/_utils.
- Require Python 3.8+, add pyproject build metadata and a valid
  fallback version so pip install . works from a checkout.
- Add a test workflow for 3.8-3.13, modernise the publish workflow.
- Replace the ad-hoc script with 68 unit tests covering the fixes and
  the new API.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
- Replace the PYPI_API_TOKEN secret with PyPI trusted publishing (OIDC):
  the publish job gets id-token: write and declares the `pypi`
  environment, matching the environment name configured on PyPI.
- Split the release into a build job (tests, tag validation, version
  stamping, sdist/wheel, twine check) and a minimal publish job that only
  downloads the artifacts and uploads them, so build-time code cannot
  reach the OIDC token.
- Fail fast on non-version tags (e.g. a manual dispatch on a branch)
  before anything is built or uploaded.
- Enable PEP 740 attestations explicitly (on by default with trusted
  publishing).
- Add a concurrency guard and artifact retention.
- Bump actions: checkout v7.0.1, setup-python v7.1.0,
  upload-artifact v7.0.2, download-artifact v8.0.2,
  gh-action-pypi-publish v1.14.3; build on Python 3.13.
- Document the PyPI publisher fields and release steps in the README.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
The PyPI trusted publisher is configured with a blank environment name,
so the OIDC token must not carry an environment claim. Remove the
`environment:` block from the publish job and document that PyPI and the
workflow must agree if an environment is added later.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@johanbeytell1
johanbeytell1 merged commit fcc8673 into main Nov 18, 2026
14 checks passed
@johanbeytell1
johanbeytell1 deleted the arena/7dbefc7a-funcprofiler branch November 18, 2026 11:13
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.

1 participant