Skip to content

Commit

Permalink
teststubtest: further fix tests (#12287)
Browse files Browse the repository at this point in the history
Basically a follow up to #12282

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja committed Mar 4, 2022
1 parent a562f0a commit 4d99f7c
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions mypy/test/teststubtest.py
Expand Up @@ -7,7 +7,6 @@
import tempfile
import textwrap
import unittest
from pathlib import Path
from typing import Any, Callable, Iterator, List, Optional

import mypy.stubtest
Expand All @@ -16,15 +15,15 @@


@contextlib.contextmanager
def use_tmp_dir(mod_name: str) -> Iterator[None]:
def use_tmp_dir(mod_name: str) -> Iterator[str]:
current = os.getcwd()
current_syspath = sys.path[:]
with tempfile.TemporaryDirectory() as tmp:
try:
os.chdir(tmp)
if sys.path[0] != tmp:
sys.path.insert(0, tmp)
yield
yield tmp
finally:
sys.path = current_syspath[:]
if mod_name in sys.modules:
Expand Down Expand Up @@ -99,7 +98,7 @@ def staticmethod(f: T) -> T: ...
def run_stubtest(
stub: str, runtime: str, options: List[str], config_file: Optional[str] = None,
) -> str:
with use_tmp_dir(TEST_MODULE_NAME):
with use_tmp_dir(TEST_MODULE_NAME) as tmp_dir:
with open("builtins.pyi", "w") as f:
f.write(stubtest_builtins_stub)
with open("typing.pyi", "w") as f:
Expand All @@ -118,10 +117,8 @@ def run_stubtest(
parse_options([TEST_MODULE_NAME] + options),
use_builtins_fixtures=True
)

module_path = Path(os.getcwd()) / TEST_MODULE_NAME
# remove cwd as it's not available from outside
return output.getvalue().replace(str(module_path), TEST_MODULE_NAME)
return output.getvalue().replace(tmp_dir + os.sep, "")


class Case:
Expand Down Expand Up @@ -780,27 +777,18 @@ def test_dunders(self) -> Iterator[Case]:
error=None,
)

def test_not_subclassable(self) -> None:
output = run_stubtest(
stub=(
"class CanBeSubclassed: ...\n"
"class CanNotBeSubclassed:\n"
" def __init_subclass__(cls) -> None: ...\n"
),
runtime=(
"class CanNotBeSubclassed:\n"
" def __init_subclass__(cls): raise TypeError('nope')\n"
# ctypes.Array can be subclassed, but subclasses must define a few
# special attributes, e.g. _length_
"from ctypes import Array as CanBeSubclassed\n"
),
options=[],
@collect_cases
def test_not_subclassable(self) -> Iterator[Case]:
yield Case(
stub="class CanBeSubclassed: ...",
runtime="class CanBeSubclassed: ...",
error=None,
)
yield Case(
stub="class CannotBeSubclassed:\n def __init_subclass__(cls) -> None: ...",
runtime="class CannotBeSubclassed:\n def __init_subclass__(cls): raise TypeError",
error="CannotBeSubclassed",
)
assert (
"CanNotBeSubclassed cannot be subclassed at runtime,"
" but isn't marked with @final in the stub"
) in output
assert "CanBeSubclassed cannot be subclassed" not in output

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

0 comments on commit 4d99f7c

Please sign in to comment.