-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for source tree and fix annotations
- Loading branch information
1 parent
c4ed8c8
commit 9b15944
Showing
10 changed files
with
178 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ dmypy.json | |
# Packages | ||
*.egg | ||
*.egg-info | ||
.eggs | ||
|
||
# IDEs | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, List, Tuple | ||
|
||
import pytest | ||
from py._path.local import LocalPath | ||
|
||
from mypy.build import BuildSource | ||
from mypy.find_sources import create_source_list | ||
from mypy.options import Options | ||
|
||
MergeFinder = Callable[[Dict[str, Any], bool], Tuple[Path, List[BuildSource]]] | ||
|
||
|
||
def create_files(folder: Path, content: Dict[str, Any]): | ||
folder.mkdir(exist_ok=True) | ||
for key, value in content.items(): | ||
if key.endswith(".py") or key.endswith(".pyi"): | ||
(folder / key).write_text(value) | ||
elif isinstance(value, dict): | ||
create_files(folder / key, value) | ||
|
||
|
||
@pytest.fixture() | ||
def merge_finder(tmpdir: LocalPath) -> MergeFinder: | ||
def _checker( | ||
content: Dict[str, Any], merge: bool | ||
) -> Tuple[Path, List[BuildSource]]: | ||
options = Options() | ||
options.merge_stub_into_src = merge | ||
test_dir = str(tmpdir) | ||
create_files(Path(test_dir), content) | ||
targets = create_source_list(files=[test_dir], options=options) | ||
return Path(str(tmpdir)), targets | ||
|
||
return _checker | ||
|
||
|
||
def test_source_finder_merge(merge_finder: MergeFinder) -> None: | ||
base_dir, found = merge_finder({"a.py": "", "a.pyi": ""}, True) | ||
assert found == [ | ||
BuildSource( | ||
path=str(base_dir / "a.py"), | ||
base_dir=str(base_dir), | ||
module="a", | ||
text=None, | ||
merge_with=BuildSource( | ||
path=str(base_dir / "a.pyi"), | ||
base_dir=str(base_dir), | ||
module="a", | ||
text=None, | ||
), | ||
) | ||
] | ||
|
||
|
||
def test_source_finder_merge_sub_folder(merge_finder: MergeFinder) -> None: | ||
base_dir, found = merge_finder( | ||
{"pkg": {"a.py": "", "a.pyi": "", "__init__.py": ""}}, True | ||
) | ||
assert found == [ | ||
BuildSource( | ||
path=str(base_dir / "pkg" / "__init__.py"), | ||
base_dir=str(base_dir), | ||
module="pkg", | ||
text=None, | ||
), | ||
BuildSource( | ||
path=str(base_dir / "pkg" / "a.py"), | ||
base_dir=str(base_dir), | ||
module="pkg.a", | ||
text=None, | ||
merge_with=BuildSource( | ||
path=str(base_dir / "pkg" / "a.pyi"), | ||
base_dir=str(base_dir), | ||
module="pkg.a", | ||
text=None, | ||
), | ||
), | ||
] | ||
|
||
|
||
def test_source_finder_no_merge(merge_finder: MergeFinder) -> None: | ||
base_dir, found = merge_finder({"a.py": "", "a.pyi": ""}, False) | ||
assert found == [ | ||
BuildSource( | ||
path=str(base_dir / "a.pyi"), base_dir=str(base_dir), module="a", text=None | ||
) | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("merge", [True, False]) | ||
def test_source_finder_merge_just_source(merge_finder, merge): | ||
base_dir, found = merge_finder({"a.py": ""}, merge=merge) | ||
assert found == [ | ||
BuildSource( | ||
path=str(base_dir / "a.py"), base_dir=str(base_dir), module="a", text=None | ||
) | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("merge", [True, False]) | ||
def test_source_finder_merge_just_stub(merge_finder, merge): | ||
base_dir, found = merge_finder({"a.pyi": ""}, merge=merge) | ||
assert found == [ | ||
BuildSource( | ||
path=str(base_dir / "a.pyi"), base_dir=str(base_dir), module="a", text=None | ||
) | ||
] |
Oops, something went wrong.