1414import unittest
1515from argparse import ArgumentParser
1616from collections import deque
17+ from dataclasses import dataclass , fields
1718from pathlib import Path
18- from typing import TYPE_CHECKING , Any , DefaultDict , Literal
19+ from typing import TYPE_CHECKING , Any , DefaultDict
1920
2021import libcst as cst
2122import pytest
@@ -157,18 +158,30 @@ def check_autofix(
157158 assert added_autofix_diff == autofix_diff_content
158159
159160
160- MAGIC_MARKERS = ("NOANYIO" , "NOTRIO" , "TRIO_NO_ERROR" , "ANYIO_NO_ERROR" )
161+ # This can be further cleaned up by adding the other return values from
162+ # parse_eval_file (Errors, args and enabled_codes) to this class - and find magic
163+ # markers in the same pass as we parse out errors etc.
164+ @dataclass
165+ class MagicMarkers :
166+ NOANYIO : bool = False
167+ NOTRIO : bool = False
168+ ANYIO_NO_ERROR : bool = False
169+ TRIO_NO_ERROR : bool = False
161170
162171
163172def find_magic_markers (
164173 content : str ,
165- ) -> dict [Literal ["NOANYIO" , "NOTRIO" , "TRIO_NO_ERROR" , "ANYIO_NO_ERROR" ], bool ]:
166- found_markers : dict [str , bool ] = {m : False for m in MAGIC_MARKERS }
167- for f in re .findall (rf'# ({ "|" .join (MAGIC_MARKERS )} )' , content ):
168- found_markers [f ] = True
169- return found_markers # type: ignore
174+ ) -> MagicMarkers :
175+ found_markers = MagicMarkers ()
176+ markers = (f .name for f in fields (found_markers ))
177+ pattern = rf'# ({ "|" .join (markers )} )'
178+ for f in re .findall (pattern , content ):
179+ setattr (found_markers , f , True )
180+ return found_markers
170181
171182
183+ # This could be optimized not to reopen+reread+reparse the same file over and over
184+ # when testing the same file
172185@pytest .mark .parametrize (("test" , "path" ), test_files )
173186@pytest .mark .parametrize ("autofix" , [False , True ])
174187@pytest .mark .parametrize ("anyio" , [False , True ])
@@ -177,11 +190,11 @@ def test_eval(
177190):
178191 content = path .read_text ()
179192 magic_markers = find_magic_markers (content )
180- if anyio and magic_markers [ " NOANYIO" ] :
193+ if anyio and magic_markers . NOANYIO :
181194 pytest .skip ("file marked with NOANYIO" )
182195
183196 ignore_column = False
184- if magic_markers [ " NOTRIO" ] :
197+ if magic_markers . NOTRIO :
185198 if not anyio :
186199 pytest .skip ("file marked with NOTRIO" )
187200
@@ -198,8 +211,8 @@ def test_eval(
198211 if autofix :
199212 parsed_args .append (f"--autofix={ enable } " )
200213
201- if (anyio and magic_markers [ " ANYIO_NO_ERROR" ] ) or (
202- not anyio and magic_markers [ " TRIO_NO_ERROR" ]
214+ if (anyio and magic_markers . ANYIO_NO_ERROR ) or (
215+ not anyio and magic_markers . TRIO_NO_ERROR
203216 ):
204217 expected = []
205218
0 commit comments