From c1d134172cff3787dbfe51cd154af8f8d4b5e556 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Thu, 14 Jul 2022 16:36:05 -0700 Subject: [PATCH] Add typing for FixtureRequest.param (#10133) For now, mark it as Any until #8073 is solved Fixes #9514 --- AUTHORS | 1 + changelog/9514.bugfix.rst | 1 + src/_pytest/fixtures.py | 11 ++++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelog/9514.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 09fe12e00..b0669ba29 100644 --- a/AUTHORS +++ b/AUTHORS @@ -252,6 +252,7 @@ Nicholas Murphy Niclas Olofsson Nicolas Delaby Nikolay Kondratyev +Nipunn Koorapati Olga Matoula Oleg Pidsadnyi Oleg Sushchenko diff --git a/changelog/9514.bugfix.rst b/changelog/9514.bugfix.rst new file mode 100644 index 000000000..a940b5c72 --- /dev/null +++ b/changelog/9514.bugfix.rst @@ -0,0 +1 @@ +Type-annotate ``FixtureRequest.param`` as ``Any`` as a stop gap measure until :issue:`8073` is fixed. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 32c3ec4b0..d1d36d7fa 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -345,7 +345,7 @@ def reorder_items_atscope( return items_done -def get_direct_param_fixture_func(request): +def get_direct_param_fixture_func(request: "FixtureRequest") -> Any: return request.param @@ -407,6 +407,15 @@ class FixtureRequest: self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy() self._arg2index: Dict[str, int] = {} self._fixturemanager: FixtureManager = pyfuncitem.session._fixturemanager + # Notes on the type of `param`: + # -`request.param` is only defined in parametrized fixtures, and will raise + # AttributeError otherwise. Python typing has no notion of "undefined", so + # this cannot be reflected in the type. + # - Technically `param` is only (possibly) defined on SubRequest, not + # FixtureRequest, but the typing of that is still in flux so this cheats. + # - In the future we might consider using a generic for the param type, but + # for now just using Any. + self.param: Any @property def scope(self) -> "_ScopeName":