Skip to content

Commit 7e3a850

Browse files
committed
Closes #21, closes #20
1 parent 91c0537 commit 7e3a850

File tree

9 files changed

+248
-234
lines changed

9 files changed

+248
-234
lines changed

.travis.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,28 @@ jobs:
1717
script: |
1818
set -e
1919
pytest
20+
- name: "run test suite with python 3.7"
21+
python: 3.8
22+
script: |
23+
set -e
24+
pytest
25+
2026
- name: "typecheck with mypy"
2127
python: 3.7
2228
script: |
2329
set -e
2430
mypy .
2531
32+
- name: Lint with black
33+
python: 3.7
34+
script: 'black --check pytest_mypy_plugins setup.py'
35+
- name: Lint with isort
36+
python: 3.7
37+
script: 'isort --check --diff'
38+
2639
before_install: |
2740
# Upgrade pip, setuptools, and wheel
2841
pip install -U pip setuptools wheel
2942
3043
install: |
31-
pip install -e .
44+
pip install -r dev-requirements.txt

dev-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
black
2+
isort
3+
-e .

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 120

pytest_mypy_plugins/collect.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import tempfile
2-
import sys
3-
import platform
41
import os
5-
from typing import Any, Dict, List, Iterator, TYPE_CHECKING, Optional
2+
import platform
3+
import sys
4+
import tempfile
5+
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional
66

77
import pytest
88
import yaml
@@ -16,7 +16,6 @@
1616
from pytest_mypy_plugins.item import YamlTestItem
1717

1818

19-
2019
class File:
2120
def __init__(self, path: str, content: str):
2221
self.path = path
@@ -26,19 +25,19 @@ def __init__(self, path: str, content: str):
2625
def parse_test_files(test_files: List[Dict[str, Any]]) -> List[File]:
2726
files: List[File] = []
2827
for test_file in test_files:
29-
path = test_file.get('path')
28+
path = test_file.get("path")
3029
if not path:
31-
path = 'main.py'
30+
path = "main.py"
3231

33-
file = File(path=path, content=test_file.get('content', ''))
32+
file = File(path=path, content=test_file.get("content", ""))
3433
files.append(file)
3534
return files
3635

3736

3837
def parse_environment_variables(env_vars: List[str]) -> Dict[str, str]:
3938
parsed_vars: Dict[str, str] = {}
4039
for env_var in env_vars:
41-
name, _, value = env_var.partition('=')
40+
name, _, value = env_var.partition("=")
4241
parsed_vars[name] = value
4342
return parsed_vars
4443

@@ -49,81 +48,81 @@ def construct_mapping(self, node: yaml.Node, deep: bool = False) -> None:
4948
# Add 1 so line numbering starts at 1
5049
starting_line = node.start_mark.line + 1
5150
for (title_node, contents_node) in node.value:
52-
if title_node.value == 'main':
51+
if title_node.value == "main":
5352
starting_line = title_node.start_mark.line + 1
54-
mapping['__line__'] = starting_line
53+
mapping["__line__"] = starting_line
5554
return mapping
5655

5756

5857
class YamlTestFile(pytest.File):
59-
def collect(self) -> Iterator['YamlTestItem']:
58+
def collect(self) -> Iterator["YamlTestItem"]:
6059
from pytest_mypy_plugins.item import YamlTestItem
6160

62-
parsed_file = yaml.load(stream=self.fspath.read_text('utf8'), Loader=SafeLineLoader)
61+
parsed_file = yaml.load(stream=self.fspath.read_text("utf8"), Loader=SafeLineLoader)
6362
if parsed_file is None:
6463
return
6564

6665
if not isinstance(parsed_file, list):
67-
raise ValueError(f'Test file has to be YAML list, got {type(parsed_file)!r}.')
66+
raise ValueError(f"Test file has to be YAML list, got {type(parsed_file)!r}.")
6867

6968
for raw_test in parsed_file:
70-
test_name = raw_test['case']
71-
if ' ' in test_name:
69+
test_name = raw_test["case"]
70+
if " " in test_name:
7271
raise ValueError(f"Invalid test name {test_name!r}, only '[a-zA-Z0-9_]' is allowed.")
7372

74-
test_files = [
75-
File(path='main.py', content=raw_test['main'])
76-
]
77-
test_files += parse_test_files(raw_test.get('files', []))
73+
test_files = [File(path="main.py", content=raw_test["main"])]
74+
test_files += parse_test_files(raw_test.get("files", []))
7875

7976
output_from_comments = []
8077
for test_file in test_files:
81-
output_lines = utils.extract_errors_from_comments(test_file.path, test_file.content.split('\n'))
78+
output_lines = utils.extract_errors_from_comments(test_file.path, test_file.content.split("\n"))
8279
output_from_comments.extend(output_lines)
8380

84-
starting_lineno = raw_test['__line__']
85-
extra_environment_variables = parse_environment_variables(raw_test.get('env', []))
86-
disable_cache = raw_test.get('disable_cache', False)
87-
expected_output_lines = raw_test.get('out', '').split('\n')
88-
additional_mypy_config = raw_test.get('mypy_config', '')
81+
starting_lineno = raw_test["__line__"]
82+
extra_environment_variables = parse_environment_variables(raw_test.get("env", []))
83+
disable_cache = raw_test.get("disable_cache", False)
84+
expected_output_lines = raw_test.get("out", "").split("\n")
85+
additional_mypy_config = raw_test.get("mypy_config", "")
8986

90-
skip = self._eval_skip(str(raw_test.get('skip', 'False')))
87+
skip = self._eval_skip(str(raw_test.get("skip", "False")))
9188
if not skip:
9289
yield YamlTestItem(
93-
name=test_name,
94-
collector=self,
95-
config=self.config,
96-
files=test_files,
97-
starting_lineno=starting_lineno,
98-
environment_variables=extra_environment_variables,
99-
disable_cache=disable_cache,
100-
expected_output_lines=output_from_comments + expected_output_lines,
101-
parsed_test_data=raw_test,
102-
mypy_config=additional_mypy_config)
90+
name=test_name,
91+
collector=self,
92+
config=self.config,
93+
files=test_files,
94+
starting_lineno=starting_lineno,
95+
environment_variables=extra_environment_variables,
96+
disable_cache=disable_cache,
97+
expected_output_lines=output_from_comments + expected_output_lines,
98+
parsed_test_data=raw_test,
99+
mypy_config=additional_mypy_config,
100+
)
103101

104102
def _eval_skip(self, skip_if: str) -> bool:
105-
return eval(skip_if, {
106-
'sys': sys,
107-
'os': os,
108-
'pytest': pytest,
109-
'platform': platform,
110-
})
103+
return eval(skip_if, {"sys": sys, "os": os, "pytest": pytest, "platform": platform})
111104

112105

113106
def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]:
114-
if path.ext in {'.yaml', '.yml'} and path.basename.startswith(('test-', 'test_')):
107+
if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")):
115108
return YamlTestFile(path, parent=parent, config=parent.config)
116109
return None
117110

118111

119112
def pytest_addoption(parser: Parser) -> None:
120-
group = parser.getgroup('mypy-tests')
121-
group.addoption('--mypy-testing-base', type=str, default=tempfile.gettempdir(),
122-
help='Base directory for tests to use')
123-
group.addoption('--mypy-ini-file', type=str,
124-
help='Which .ini file to use as a default config for tests')
125-
group.addoption('--mypy-same-process', action='store_true',
126-
help='Run in the same process. Useful for debugging, will create problems with import cache')
127-
group.addoption('--mypy-extension-hook', type=str,
128-
help='Fully qualifield path to the extension hook function, in case you need custom yaml keys. '
129-
'Has to be top-level.')
113+
group = parser.getgroup("mypy-tests")
114+
group.addoption(
115+
"--mypy-testing-base", type=str, default=tempfile.gettempdir(), help="Base directory for tests to use"
116+
)
117+
group.addoption("--mypy-ini-file", type=str, help="Which .ini file to use as a default config for tests")
118+
group.addoption(
119+
"--mypy-same-process",
120+
action="store_true",
121+
help="Run in the same process. Useful for debugging, will create problems with import cache",
122+
)
123+
group.addoption(
124+
"--mypy-extension-hook",
125+
type=str,
126+
help="Fully qualifield path to the extension hook function, in case you need custom yaml keys. "
127+
"Has to be top-level.",
128+
)

0 commit comments

Comments
 (0)