Coverage for tests/test_merger.py: 100.00%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.2, created at 2024-10-12 01:34 +0200

1"""Tests for the `merger` module.""" 

2 

3from __future__ import annotations 

4 

5from textwrap import dedent 

6 

7from griffe import GriffeLoader, temporary_pypackage 

8 

9 

10def test_dont_trigger_alias_resolution_when_merging_stubs() -> None: 

11 """Assert that we don't trigger alias resolution when merging stubs.""" 

12 with temporary_pypackage("package", ["mod.py", "mod.pyi"]) as tmp_package: 

13 tmp_package.path.joinpath("mod.py").write_text( 

14 dedent( 

15 """ 

16 import pathlib 

17 

18 def f() -> pathlib.Path: 

19 return pathlib.Path() 

20 """, 

21 ), 

22 ) 

23 tmp_package.path.joinpath("mod.pyi").write_text( 

24 dedent( 

25 """ 

26 import pathlib 

27 

28 def f() -> pathlib.Path: ... 

29 """, 

30 ), 

31 ) 

32 loader = GriffeLoader(search_paths=[tmp_package.tmpdir]) 

33 loader.load(tmp_package.name) 

34 

35 

36def test_merge_stubs_on_wildcard_imported_objects() -> None: 

37 """Assert that stubs can be merged on wildcard imported objects.""" 

38 with temporary_pypackage("package", ["mod.py", "__init__.pyi"]) as tmp_package: 

39 tmp_package.path.joinpath("mod.py").write_text( 

40 dedent( 

41 """ 

42 class A: 

43 def hello(value: int | str) -> int | str: 

44 return value 

45 """, 

46 ), 

47 ) 

48 tmp_package.path.joinpath("__init__.py").write_text("from .mod import *") 

49 tmp_package.path.joinpath("__init__.pyi").write_text( 

50 dedent( 

51 """ 

52 from typing import overload 

53 class A: 

54 @overload 

55 def hello(value: int) -> int: ... 

56 @overload 

57 def hello(value: str) -> str: ... 

58 """, 

59 ), 

60 ) 

61 loader = GriffeLoader(search_paths=[tmp_package.tmpdir]) 

62 module = loader.load(tmp_package.name) 

63 assert module["A.hello"].overloads