Skip to content

fix: request signature with annotation format STRING#668

Open
mschoettle wants to merge 2 commits intopytest-dev:mainfrom
mschoettle:fix-inspect-signature
Open

fix: request signature with annotation format STRING#668
mschoettle wants to merge 2 commits intopytest-dev:mainfrom
mschoettle:fix-inspect-signature

Conversation

@mschoettle
Copy link
Copy Markdown
Contributor

On Python 3.14+ the default annotation format is Format.VALUE which causes an error when requesting a signature with an undefined name.

On Python 3.14, ruff suggests to move certain imports into type checking blocks, leading for example to the following:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from _pytest.config import Config
    from _pytest.main import Session
    from _pytest.python import Function

def pytest_collection_modifyitems(session: Session, config: Config, items: list[Function]) -> None:
    ...

Causing:

.venv/lib/python3.14/site-packages/pluggy/_manager.py:161: in register
    hookimpl = HookImpl(plugin, plugin_name, method, hookimpl_opts)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.14/site-packages/pluggy/_hooks.py:664: in __init__
    argnames, kwargnames = varnames(self.function)
                           ^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.14/site-packages/pluggy/_hooks.py:313: in varnames
    sig = inspect.signature(
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/inspect.py:3323: in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/inspect.py:3038: in from_callable
    return _signature_from_callable(obj, sigcls=cls,
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/inspect.py:2512: in _signature_from_callable
    return _signature_from_function(sigcls, obj,
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/inspect.py:2335: in _signature_from_function
    annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str,
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/annotationlib.py:966: in get_annotations
    ann = _get_dunder_annotations(obj)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.4-macos-aarch64-none/lib/python3.14/annotationlib.py:1159: in _get_dunder_annotations
    ann = getattr(obj, "__annotations__", None)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
opal/conftest.py:45: in __annotate__
    def pytest_collection_modifyitems(session: Session, config: Config, items: list[Function]) -> None:
                                               ^^^^^^^
E   NameError: name 'Session' is not defined

Changing the annotation format to STRING fixes this.

@mschoettle mschoettle force-pushed the fix-inspect-signature branch 3 times, most recently from 30f1b58 to 7ae22a5 Compare April 28, 2026 20:29
Comment thread src/pluggy/_hooks.py Outdated

def _signature(func: Callable[..., object]) -> inspect.Signature:
return inspect.signature(func, annotation_format=Format.STRING)
else: # pragma: no cover
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is reported as uncovered despite the comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, with the combined coverage data the comment should not be required. Not sure what's wrong with the codecov check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be fixed once #669 is merged

@mschoettle mschoettle force-pushed the fix-inspect-signature branch from 7ae22a5 to bba1a7a Compare April 28, 2026 20:56
@mschoettle mschoettle marked this pull request as draft April 29, 2026 13:00
@mschoettle mschoettle force-pushed the fix-inspect-signature branch from 33a4b79 to bba1a7a Compare April 29, 2026 13:39
@mschoettle mschoettle marked this pull request as ready for review April 29, 2026 13:41
@RonnyPfannschmidt
Copy link
Copy Markdown
Member

note - this may be super-seeded by #632 which i just picked up again

@mschoettle
Copy link
Copy Markdown
Contributor Author

Yes indeed. inspect.signature is not called anymore with that change.

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.

2 participants