Skip to content

Commit 4d99f7c

Browse files
authored
teststubtest: further fix tests (#12287)
Basically a follow up to #12282 Co-authored-by: hauntsaninja <>
1 parent a562f0a commit 4d99f7c

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

mypy/test/teststubtest.py

+16-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import tempfile
88
import textwrap
99
import unittest
10-
from pathlib import Path
1110
from typing import Any, Callable, Iterator, List, Optional
1211

1312
import mypy.stubtest
@@ -16,15 +15,15 @@
1615

1716

1817
@contextlib.contextmanager
19-
def use_tmp_dir(mod_name: str) -> Iterator[None]:
18+
def use_tmp_dir(mod_name: str) -> Iterator[str]:
2019
current = os.getcwd()
2120
current_syspath = sys.path[:]
2221
with tempfile.TemporaryDirectory() as tmp:
2322
try:
2423
os.chdir(tmp)
2524
if sys.path[0] != tmp:
2625
sys.path.insert(0, tmp)
27-
yield
26+
yield tmp
2827
finally:
2928
sys.path = current_syspath[:]
3029
if mod_name in sys.modules:
@@ -99,7 +98,7 @@ def staticmethod(f: T) -> T: ...
9998
def run_stubtest(
10099
stub: str, runtime: str, options: List[str], config_file: Optional[str] = None,
101100
) -> str:
102-
with use_tmp_dir(TEST_MODULE_NAME):
101+
with use_tmp_dir(TEST_MODULE_NAME) as tmp_dir:
103102
with open("builtins.pyi", "w") as f:
104103
f.write(stubtest_builtins_stub)
105104
with open("typing.pyi", "w") as f:
@@ -118,10 +117,8 @@ def run_stubtest(
118117
parse_options([TEST_MODULE_NAME] + options),
119118
use_builtins_fixtures=True
120119
)
121-
122-
module_path = Path(os.getcwd()) / TEST_MODULE_NAME
123120
# 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, "")
125122

126123

127124
class Case:
@@ -780,27 +777,18 @@ def test_dunders(self) -> Iterator[Case]:
780777
error=None,
781778
)
782779

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",
798791
)
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
804792

805793
@collect_cases
806794
def test_name_mangling(self) -> Iterator[Case]:
@@ -1114,7 +1102,7 @@ def test_config_file(self) -> None:
11141102
"plugins={}/test-data/unit/plugins/decimal_to_int.py\n".format(root_dir)
11151103
)
11161104
output = run_stubtest(stub=stub, runtime=runtime, options=[])
1117-
assert output == (
1105+
assert remove_color_code(output) == (
11181106
"error: test_module.temp variable differs from runtime type Literal[5]\n"
11191107
"Stub: at line 2\ndecimal.Decimal\nRuntime:\n5\n\n"
11201108
)

0 commit comments

Comments
 (0)