Skip to content

[NO-SQUASH-MERGE] fix varnames on py3.14 deferred annotations - #632

Merged
RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:varnames-no-resolve-types
May 27, 2026
Merged

RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:varnames-no-resolve-types

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

closes #629

using direct attributes instead of signature to avoid the extra objects

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes pluggy._hooks.varnames() to avoid inspect.signature() on Python 3.14+ (where deferred/string annotations may be resolved and can fail), and adds coverage + benchmarks around the new behavior.

Changes:

  • Reimplements varnames() using __code__, __defaults__, and __qualname__ instead of inspect.signature().
  • Adds a regression test for annotations that cannot be resolved at runtime.
  • Extends benchmark suite to compare the new implementation against a legacy inspect.signature() version.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/pluggy/_hooks.py Updates varnames() implementation to avoid annotation resolution and reduce overhead.
testing/test_helpers.py Adds a regression test ensuring varnames() works with unresolvable string annotations.
testing/benchmark.py Adds microbenchmarks comparing new vs legacy varnames() approaches.
Comments suppressed due to low confidence (1)

src/pluggy/_hooks.py:298

  • Typo in the varnames docstring: "keywrord" should be "keyword".
def varnames(func: object) -> tuple[tuple[str, ...], tuple[str, ...]]:
    """Return tuple of positional and keywrord argument names for a function,
    method, class or callable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pluggy/_hooks.py Resolved
Comment thread testing/test_helpers.py Resolved
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the varnames-no-resolve-types branch 2 times, most recently from 9d852b3 to d2775b4 Compare April 29, 2026 18:33
@RonnyPfannschmidt RonnyPfannschmidt changed the title fix varnames on py3.14 deferred annotations [NO-SQUAHS-MERGE] fix varnames on py3.14 deferred annotations Apr 29, 2026
@RonnyPfannschmidt RonnyPfannschmidt changed the title [NO-SQUAHS-MERGE] fix varnames on py3.14 deferred annotations [NO-SQUASH-MERGE] fix varnames on py3.14 deferred annotations Apr 29, 2026

@bluetech bluetech left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm a bit wary of stopping to use inspect.signature. It means we drop support for stuff like __signature__ and __text_signature__ and whatever weird stuff inspect.signature supports.

The performance is definitely nice but if I'm not mistaken varnames is only used #hookspecs + #hookimpls times so not very performance critical.

Nonetheless, if you think it should be fine, I would at least suggest running some downstream tests (see downstream/ directory) to make sure there are no regressions (I usually run it before release).

Comment thread src/pluggy/_hooks.py Resolved
Comment thread testing/benchmark.py Resolved
Comment thread changelog/629.bugfix.rst Resolved
@RonnyPfannschmidt

RonnyPfannschmidt commented May 1, 2026 •

Copy link
Copy Markdown
Member Author

fixing the downstream tests now (scripts are bitrot affected)

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

im creating a new pr to fix upstream testing first - i'll make it so its triggerable on github as well

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

@bluetech expected futurewarning from pytest-timeout in conda - im going for a upstream fix in pytest-timeout

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

upstream pytest-timeut already has self - now i need to validate conda 🙀

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

ah, we need to release

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM in general, but have a couple of comments.

Comment thread testing/test_hookcaller.py Outdated
]


@pytest.mark.filterwarnings("ignore::FutureWarning")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this doesn't specifically test the no-self case, maybe better to add self to the methods than to ignore the warning?

Comment thread src/pluggy/_hooks.py Outdated
f" {args[0]!r} is not 'self'."
f" Add 'self' as the first parameter or use @staticmethod."
f" This will become an error in a future version of pluggy.",
FutureWarning,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I haven't seen FutureWarning a lot so I looked at its intended usage (ref):

Base class for warnings about deprecated features when those warnings are intended for end users of applications that are written in Python.

I don't think a user of an application using pluggy would be interested in this message. So I think it's better as a DeprecationWarning?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i intended to use FutureDeprecationWarning

Comment thread src/pluggy/_hooks.py Outdated
elif _is_class_method and args[0] in _IMPLICIT_NAMES:
args = args[1:]
elif _is_class_method and legacy_noself:
warnings.warn(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add a changelog entry about the deprecation.

RonnyPfannschmidt and others added 5 commits May 27, 2026 09:05
In Python 3.14+, annotations are evaluated lazily per PEP 649/749.
When inspect.signature() is called, it tries to resolve annotations
by default, which fails if the annotation references an undefined type.

Add a version-gated _signature helper that uses
annotation_format=annotationlib.Format.STRING on Python 3.14+ to
prevent annotation resolution errors.

Fixes pytest-dev#629

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Use code object attributes (co_varnames, co_argcount) and __defaults__
directly instead of inspect.signature(). This avoids annotation
resolution entirely, which is simpler and more efficient.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Include both the current implementation and the legacy inspect.signature-based
version to clearly demonstrate the ~7-66x speedup from using code objects directly.

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
A module-level function assigned to a class attribute becomes a bound
method on instances, but its __qualname__ has no dot. Track whether
the original callable was a bound method before unwrapping to __func__,
so self is always stripped for bound methods.

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
RonnyPfannschmidt and others added 6 commits May 27, 2026 09:05
Replace exec()-based test with a direct function definition. The string
annotation "NonExistentType" triggers the same behavior without the
indirection.

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
…lusion

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
Made-with: Cursor
Move the missing-self warning into varnames where the ambiguity
actually lives, instead of bolting it onto HookSpec.__init__.

Add a legacy_noself parameter to varnames: when True and the
function looks like a class method but lacks self/cls as its
first parameter, emit a FutureWarning. HookSpec.__init__ passes
legacy_noself=True for class-based non-static hookspecs to
support the legacy pattern while warning about it.

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
…-timeout

- Change FutureWarning to DeprecationWarning for hookspec methods missing self
- Add self to HookSpec methods in test_hookspec instead of suppressing the warning
- Suppress the deprecation warning for pytest-timeout's TimeoutHooks (upstream
  fix exists but is unreleased)
- Add changelog entry for the deprecation
- Update all test references from FutureWarning to DeprecationWarning

Co-authored-by: Cursor AI <[email protected]>
Co-authored-by: Anthropic Claude Opus 4 <[email protected]>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the varnames-no-resolve-types branch from 006b245 to 0258484 Compare May 27, 2026 07:38
@RonnyPfannschmidt
RonnyPfannschmidt merged commit 5d7bd2c into pytest-dev:main May 27, 2026
19 checks passed
github-actions Bot pushed a commit to ai-avimiot/pipewarden that referenced this pull request Oct 19, 2026