collect.py 902 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import sys
  2. import warnings
  3. from types import ModuleType
  4. from typing import Any
  5. from typing import List
  6. import pytest
  7. from _pytest.deprecated import PYTEST_COLLECT_MODULE
  8. COLLECT_FAKEMODULE_ATTRIBUTES = [
  9. "Collector",
  10. "Module",
  11. "Function",
  12. "Session",
  13. "Item",
  14. "Class",
  15. "File",
  16. "_fillfuncargs",
  17. ]
  18. class FakeCollectModule(ModuleType):
  19. def __init__(self) -> None:
  20. super().__init__("pytest.collect")
  21. self.__all__ = list(COLLECT_FAKEMODULE_ATTRIBUTES)
  22. self.__pytest = pytest
  23. def __dir__(self) -> List[str]:
  24. return dir(super()) + self.__all__
  25. def __getattr__(self, name: str) -> Any:
  26. if name not in self.__all__:
  27. raise AttributeError(name)
  28. warnings.warn(PYTEST_COLLECT_MODULE.format(name=name), stacklevel=2)
  29. return getattr(pytest, name)
  30. sys.modules["pytest.collect"] = FakeCollectModule()