Coverage for tests/test_public_exports.py: 94%

22 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 11:35 +0000

1from importlib import import_module 

2from importlib.metadata import PackageNotFoundError, version 

3from pathlib import Path 

4 

5import pytest 

6 

7import faststream 

8 

9OPTIONAL_DEPENDENCIES = { 

10 "cli": ("typer", "watchfiles"), 

11 "confluent": ("confluent-kafka",), 

12 "fastapi": ("fastapi",), 

13 "kafka": ("aiokafka",), 

14 "mqtt": ("zmqtt",), 

15 "nats": ("nats-py",), 

16 "opentelemetry": ("opentelemetry-sdk",), 

17 "prometheus": ("prometheus-client",), 

18 "rabbit": ("aio-pika",), 

19 "redis": ("redis",), 

20} 

21 

22 

23def _public_modules() -> list[str]: 

24 root = Path(faststream.__file__).parent 

25 names = [] 

26 for source in sorted(root.rglob("*.py")): 

27 parts = source.relative_to(root).with_suffix("").parts 

28 if parts[-1] == "__init__": 

29 parts = parts[:-1] 

30 if all(not part.startswith("_") for part in parts): 

31 names.append(".".join((faststream.__name__, *parts))) 

32 return names 

33 

34 

35@pytest.mark.parametrize("module_name", _public_modules()) 

36def test_all_resolves_at_runtime(module_name: str) -> None: 

37 """Every name in `__all__` must exist at runtime, not only for type checkers. 

38 

39 A name imported under `if TYPE_CHECKING:` satisfies mypy and raises 

40 AttributeError in production - see #2841 -> #2898, where the API reference 

41 builder broke on `ConfluentParserType`. 

42 """ 

43 try: 

44 for part in module_name.split("."): 

45 for dependency in OPTIONAL_DEPENDENCIES.get(part, ()): 

46 version(dependency) 

47 except PackageNotFoundError as exc: 

48 pytest.skip(f"{module_name} requires {exc.name}") 

49 

50 module = import_module(module_name) 

51 

52 for name in getattr(module, "__all__", ()): 

53 assert hasattr(module, name), ( 

54 f"{module_name}.__all__ exports {name!r}, which does not exist at runtime" 

55 )