7
7
import tempfile
8
8
import textwrap
9
9
import unittest
10
- from pathlib import Path
11
10
from typing import Any , Callable , Iterator , List , Optional
12
11
13
12
import mypy .stubtest
16
15
17
16
18
17
@contextlib .contextmanager
19
- def use_tmp_dir (mod_name : str ) -> Iterator [None ]:
18
+ def use_tmp_dir (mod_name : str ) -> Iterator [str ]:
20
19
current = os .getcwd ()
21
20
current_syspath = sys .path [:]
22
21
with tempfile .TemporaryDirectory () as tmp :
23
22
try :
24
23
os .chdir (tmp )
25
24
if sys .path [0 ] != tmp :
26
25
sys .path .insert (0 , tmp )
27
- yield
26
+ yield tmp
28
27
finally :
29
28
sys .path = current_syspath [:]
30
29
if mod_name in sys .modules :
@@ -99,7 +98,7 @@ def staticmethod(f: T) -> T: ...
99
98
def run_stubtest (
100
99
stub : str , runtime : str , options : List [str ], config_file : Optional [str ] = None ,
101
100
) -> str :
102
- with use_tmp_dir (TEST_MODULE_NAME ):
101
+ with use_tmp_dir (TEST_MODULE_NAME ) as tmp_dir :
103
102
with open ("builtins.pyi" , "w" ) as f :
104
103
f .write (stubtest_builtins_stub )
105
104
with open ("typing.pyi" , "w" ) as f :
@@ -118,10 +117,8 @@ def run_stubtest(
118
117
parse_options ([TEST_MODULE_NAME ] + options ),
119
118
use_builtins_fixtures = True
120
119
)
121
-
122
- module_path = Path (os .getcwd ()) / TEST_MODULE_NAME
123
120
# remove cwd as it's not available from outside
124
- return output .getvalue ().replace (str ( module_path ), TEST_MODULE_NAME )
121
+ return output .getvalue ().replace (tmp_dir + os . sep , "" )
125
122
126
123
127
124
class Case :
@@ -780,27 +777,18 @@ def test_dunders(self) -> Iterator[Case]:
780
777
error = None ,
781
778
)
782
779
783
- def test_not_subclassable (self ) -> None :
784
- output = run_stubtest (
785
- stub = (
786
- "class CanBeSubclassed: ...\n "
787
- "class CanNotBeSubclassed:\n "
788
- " def __init_subclass__(cls) -> None: ...\n "
789
- ),
790
- runtime = (
791
- "class CanNotBeSubclassed:\n "
792
- " def __init_subclass__(cls): raise TypeError('nope')\n "
793
- # ctypes.Array can be subclassed, but subclasses must define a few
794
- # special attributes, e.g. _length_
795
- "from ctypes import Array as CanBeSubclassed\n "
796
- ),
797
- options = [],
780
+ @collect_cases
781
+ def test_not_subclassable (self ) -> Iterator [Case ]:
782
+ yield Case (
783
+ stub = "class CanBeSubclassed: ..." ,
784
+ runtime = "class CanBeSubclassed: ..." ,
785
+ error = None ,
786
+ )
787
+ yield Case (
788
+ stub = "class CannotBeSubclassed:\n def __init_subclass__(cls) -> None: ..." ,
789
+ runtime = "class CannotBeSubclassed:\n def __init_subclass__(cls): raise TypeError" ,
790
+ error = "CannotBeSubclassed" ,
798
791
)
799
- assert (
800
- "CanNotBeSubclassed cannot be subclassed at runtime,"
801
- " but isn't marked with @final in the stub"
802
- ) in output
803
- assert "CanBeSubclassed cannot be subclassed" not in output
804
792
805
793
@collect_cases
806
794
def test_name_mangling (self ) -> Iterator [Case ]:
@@ -1114,7 +1102,7 @@ def test_config_file(self) -> None:
1114
1102
"plugins={}/test-data/unit/plugins/decimal_to_int.py\n " .format (root_dir )
1115
1103
)
1116
1104
output = run_stubtest (stub = stub , runtime = runtime , options = [])
1117
- assert output == (
1105
+ assert remove_color_code ( output ) == (
1118
1106
"error: test_module.temp variable differs from runtime type Literal[5]\n "
1119
1107
"Stub: at line 2\n decimal.Decimal\n Runtime:\n 5\n \n "
1120
1108
)
0 commit comments