compat.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Provides compatibility with different pytest versions."""
  2. from inspect import signature
  3. __GETFIXTUREDEFS_2ND_PAR_IS_STR = None
  4. def getfixturedefs(fixturemanager, name, item):
  5. """Calls FixtureManager.getfixturedefs in a way compatible with Python
  6. versions before and after the change described in pytest-dev/pytest#11785.
  7. """
  8. getfixturedefs = fixturemanager.getfixturedefs
  9. itemarg = __resolve_getfixturedefs_2nd_arg(getfixturedefs, item)
  10. return getfixturedefs(name, itemarg)
  11. def __resolve_getfixturedefs_2nd_arg(getfixturedefs, item):
  12. # Starting from pytest 8.1, getfixturedefs requires the item itself.
  13. # In earlier versions it requires the nodeid string.
  14. return item.nodeid if __2nd_parameter_is_str(getfixturedefs) else item
  15. def __2nd_parameter_is_str(getfixturedefs):
  16. global __GETFIXTUREDEFS_2ND_PAR_IS_STR
  17. if __GETFIXTUREDEFS_2ND_PAR_IS_STR is None:
  18. __GETFIXTUREDEFS_2ND_PAR_IS_STR =\
  19. __get_2nd_parameter_type(getfixturedefs) is str
  20. return __GETFIXTUREDEFS_2ND_PAR_IS_STR
  21. def __get_2nd_parameter_type(fn):
  22. return list(
  23. signature(fn).parameters.values()
  24. )[1].annotation