1- import tempfile
2- import sys
3- import platform
41import 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
77import pytest
88import yaml
1616 from pytest_mypy_plugins .item import YamlTestItem
1717
1818
19-
2019class File :
2120 def __init__ (self , path : str , content : str ):
2221 self .path = path
@@ -26,19 +25,19 @@ def __init__(self, path: str, content: str):
2625def 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
3837def 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
5857class 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
113106def 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
119112def 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