diff --git a/changelog/13888.improvement.rst b/changelog/13888.improvement.rst new file mode 100644 index 00000000000..b15231a8b79 --- /dev/null +++ b/changelog/13888.improvement.rst @@ -0,0 +1 @@ +:attr:`FixtureRequest.node ` is now explicitly annotated as returning ``Any``, so strict pyright no longer reports it as ``Unknown``. The return type stays deliberately broad, see :issue:`13888`. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 20e43f89132..e3d6300b599 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -564,9 +564,18 @@ def fixturenames(self) -> list[str]: result.extend(set(self._fixture_defs).difference(result)) return result + # The return type is intentionally ``Any``. + # The concrete node type depends on the request scope, so the only correct + # annotation would be a broad one (``Item | Collector``). That would force + # users to ``assert isinstance(...)`` or ``cast`` whenever they know the + # actual type (e.g. ``request.node.function`` in a function-scoped + # fixture), so we trade type safety for convenience here. + # It is spelled out explicitly because pyright in strict mode reports an + # unannotated return as ``Unknown`` rather than ``Any``. + # See https://github.com/pytest-dev/pytest/issues/13888#issuecomment-3511168937. @property @abc.abstractmethod - def node(self): + def node(self) -> Any: """Underlying collection node (depends on current request scope).""" raise NotImplementedError()