From 3e77959eacf3d445a0cb4db5a4bc6dcf606fc040 Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Tue, 4 Aug 2020 18:14:55 -0500 Subject: [PATCH] Use pytest Node.from_parent if available (#9263) * Use pytest Node.from_parent if available * Use pytest Node.from_parent unconditionally (requires pytest 5.4+) * Bump pytest test requirements * Require pytest 6.0 and remove unused type ignores * Make flake8 happy --- mypy/test/data.py | 37 ++++++++++++++++++++++-------------- mypy/test/helpers.py | 2 +- mypy/test/testfinegrained.py | 2 +- mypy/test/testipc.py | 2 +- mypy/test/testparse.py | 2 +- mypy/test/testpep561.py | 2 +- mypy/test/testpythoneval.py | 2 +- mypyc/test/testutil.py | 2 +- pytest.ini | 3 +-- test-requirements.txt | 9 ++++----- 10 files changed, 35 insertions(+), 28 deletions(-) diff --git a/mypy/test/data.py b/mypy/test/data.py index 5484fd99e9..a4f2d798b1 100644 --- a/mypy/test/data.py +++ b/mypy/test/data.py @@ -9,7 +9,7 @@ from abc import abstractmethod import sys -import pytest # type: ignore # no pytest in typeshed +import pytest from typing import List, Tuple, Set, Optional, Iterator, Any, Dict, NamedTuple, Union from mypy.test.config import test_data_prefix, test_temp_dir, PREFIX @@ -160,9 +160,12 @@ def parse_test_case(case: 'DataDrivenTestCase') -> None: case.expected_fine_grained_targets = targets -class DataDrivenTestCase(pytest.Item): # type: ignore # inheriting from Any +class DataDrivenTestCase(pytest.Item): """Holds parsed data-driven test cases, and handles directory setup and teardown.""" + # Override parent member type + parent = None # type: DataSuiteCollector + input = None # type: List[str] output = None # type: List[str] # Output for the first pass output2 = None # type: Dict[int, List[str]] # Output for runs 2+, indexed by run number @@ -266,7 +269,7 @@ def repr_failure(self, excinfo: Any, style: Optional[Any] = None) -> str: # call exit() and they already print out a stack trace. excrepr = excinfo.exconly() else: - self.parent._prunetraceback(excinfo) + self.parent._prunetraceback(excinfo) # type: ignore[no-untyped-call] excrepr = excinfo.getrepr(style='short') return "data: {}:{}:\n{}".format(self.file, self.line, excrepr) @@ -510,7 +513,9 @@ def pytest_pycollect_makeitem(collector: Any, name: str, # Non-None result means this obj is a test case. # The collect method of the returned DataSuiteCollector instance will be called later, # with self.obj being obj. - return DataSuiteCollector(name, parent=collector) + return DataSuiteCollector.from_parent( # type: ignore[no-untyped-call] + parent=collector, name=name + ) return None @@ -535,19 +540,23 @@ def split_test_cases(parent: 'DataSuiteCollector', suite: 'DataSuite', for i in range(1, len(cases), 6): name, writescache, only_when, platform_flag, skip, data = cases[i:i + 6] platform = platform_flag[1:] if platform_flag else None - yield DataDrivenTestCase(parent, suite, file, - name=add_test_name_suffix(name, suite.test_name_suffix), - writescache=bool(writescache), - only_when=only_when, - platform=platform, - skip=bool(skip), - data=data, - line=line_no) + yield DataDrivenTestCase.from_parent( + parent=parent, + suite=suite, + file=file, + name=add_test_name_suffix(name, suite.test_name_suffix), + writescache=bool(writescache), + only_when=only_when, + platform=platform, + skip=bool(skip), + data=data, + line=line_no, + ) line_no += data.count('\n') + 1 -class DataSuiteCollector(pytest.Class): # type: ignore # inheriting from Any - def collect(self) -> Iterator[pytest.Item]: # type: ignore +class DataSuiteCollector(pytest.Class): + def collect(self) -> Iterator[pytest.Item]: """Called by pytest on each of the object returned from pytest_pycollect_makeitem""" # obj is the object for which pytest_pycollect_makeitem returned self. diff --git a/mypy/test/helpers.py b/mypy/test/helpers.py index 46c01114c4..91c5ff6ab2 100644 --- a/mypy/test/helpers.py +++ b/mypy/test/helpers.py @@ -10,7 +10,7 @@ from mypy import defaults import mypy.api as api -import pytest # type: ignore # no pytest in typeshed +import pytest # Exporting Suite as alias to TestCase for backwards compatibility # TODO: avoid aliasing - import and subclass TestCase directly diff --git a/mypy/test/testfinegrained.py b/mypy/test/testfinegrained.py index 596391da44..d4ed18cab0 100644 --- a/mypy/test/testfinegrained.py +++ b/mypy/test/testfinegrained.py @@ -35,7 +35,7 @@ from mypy.config_parser import parse_config_file from mypy.find_sources import create_source_list -import pytest # type: ignore # no pytest in typeshed +import pytest # Set to True to perform (somewhat expensive) checks for duplicate AST nodes after merge CHECK_CONSISTENCY = False diff --git a/mypy/test/testipc.py b/mypy/test/testipc.py index 1d4829d561..7dd829a590 100644 --- a/mypy/test/testipc.py +++ b/mypy/test/testipc.py @@ -3,7 +3,7 @@ from mypy.ipc import IPCClient, IPCServer -import pytest # type: ignore +import pytest import sys import time diff --git a/mypy/test/testparse.py b/mypy/test/testparse.py index e990a403a5..e9ff6839bc 100644 --- a/mypy/test/testparse.py +++ b/mypy/test/testparse.py @@ -2,7 +2,7 @@ import sys -from pytest import skip # type: ignore[import] +from pytest import skip from mypy import defaults from mypy.test.helpers import assert_string_arrays_equal, parse_options diff --git a/mypy/test/testpep561.py b/mypy/test/testpep561.py index a8eabd7702..aadf01ae5f 100644 --- a/mypy/test/testpep561.py +++ b/mypy/test/testpep561.py @@ -1,6 +1,6 @@ from contextlib import contextmanager import os -import pytest # type: ignore +import pytest import re import subprocess from subprocess import PIPE diff --git a/mypy/test/testpythoneval.py b/mypy/test/testpythoneval.py index 7586a3854e..e7e9f16183 100644 --- a/mypy/test/testpythoneval.py +++ b/mypy/test/testpythoneval.py @@ -18,7 +18,7 @@ import sys from tempfile import TemporaryDirectory -import pytest # type: ignore # no pytest in typeshed +import pytest from typing import List diff --git a/mypyc/test/testutil.py b/mypyc/test/testutil.py index 18ab39a103..c1ce8626ba 100644 --- a/mypyc/test/testutil.py +++ b/mypyc/test/testutil.py @@ -7,7 +7,7 @@ import shutil from typing import List, Callable, Iterator, Optional, Tuple -import pytest # type: ignore[import] +import pytest from mypy import build from mypy.errors import CompileError diff --git a/pytest.ini b/pytest.ini index 81586a2370..ed76809091 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,6 +1,5 @@ [pytest] -# testpaths is new in 2.8 -minversion = 2.8 +minversion = 6.0.0 testpaths = mypy/test mypyc/test