| 1384 | |
| 1385 | # TODO: paramspec/return type annotation tracking and storing |
| 1386 | class FixtureFunctionDefinition: |
| 1387 | def __init__( |
| 1388 | self, |
| 1389 | *, |
| 1390 | function: Callable[..., Any], |
| 1391 | fixture_function_marker: FixtureFunctionMarker, |
| 1392 | instance: object | None = None, |
| 1393 | _ispytest: bool = False, |
| 1394 | ) -> None: |
| 1395 | check_ispytest(_ispytest) |
| 1396 | self.name = fixture_function_marker.name or function.__name__ |
| 1397 | # In order to show the function that this fixture contains in messages. |
| 1398 | # Set the __name__ to be same as the function __name__ or the given fixture name. |
| 1399 | self.__name__ = self.name |
| 1400 | self._fixture_function_marker = fixture_function_marker |
| 1401 | if instance is not None: |
| 1402 | self._fixture_function = cast( |
| 1403 | Callable[..., Any], function.__get__(instance) |
| 1404 | ) |
| 1405 | else: |
| 1406 | self._fixture_function = function |
| 1407 | functools.update_wrapper(self, function) |
| 1408 | |
| 1409 | def __repr__(self) -> str: |
| 1410 | return f"<pytest_fixture({self._fixture_function})>" |
| 1411 | |
| 1412 | def __get__(self, instance, owner=None): |
| 1413 | """Behave like a method if the function it was applied to was a method.""" |
| 1414 | return FixtureFunctionDefinition( |
| 1415 | function=self._fixture_function, |
| 1416 | fixture_function_marker=self._fixture_function_marker, |
| 1417 | instance=instance, |
| 1418 | _ispytest=True, |
| 1419 | ) |
| 1420 | |
| 1421 | def __call__(self, *args: Any, **kwds: Any) -> Any: |
| 1422 | message = ( |
| 1423 | f'Fixture "{self.name}" called directly. Fixtures are not meant to be called directly,\n' |
| 1424 | "but are created automatically when test functions request them as parameters.\n" |
| 1425 | "See https://docs.pytest.org/en/stable/explanation/fixtures.html for more information about fixtures, and\n" |
| 1426 | "https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly" |
| 1427 | ) |
| 1428 | fail(message, pytrace=False) |
| 1429 | |
| 1430 | def _get_wrapped_function(self) -> Callable[..., Any]: |
| 1431 | return self._fixture_function |
| 1432 | |
| 1433 | |
| 1434 | @overload |