Skip to content

Use pytest Node.from_parent if available #9263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testfinegrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from mypy.ipc import IPCClient, IPCServer

import pytest # type: ignore
import pytest
import sys
import time

Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testpep561.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from contextlib import contextmanager
import os
import pytest # type: ignore
import pytest
import re
import subprocess
from subprocess import PIPE
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
from tempfile import TemporaryDirectory

import pytest # type: ignore # no pytest in typeshed
import pytest

from typing import List

Expand Down
2 changes: 1 addition & 1 deletion mypyc/test/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[pytest]
# testpaths is new in 2.8
minversion = 2.8
minversion = 6.0.0

testpaths = mypy/test mypyc/test

Expand Down
9 changes: 4 additions & 5 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ flake8-bugbear; python_version >= '3.5'
flake8-pyi>=20.5; python_version >= '3.6'
lxml>=4.4.0
psutil>=4.0
pytest==5.3.2
pytest-xdist>=1.22
# pytest-xdist depends on pytest-forked and 1.1.0 doesn't install clean on macOS 3.5
pytest-forked>=1.0.0,<1.1.0
pytest-cov>=2.4.0
pytest>=6.0.0,<7.0.0
pytest-xdist>=1.34.0,<2.0.0
pytest-forked>=1.3.0,<2.0.0
pytest-cov>=2.10.0,<3.0.0
typing>=3.5.2; python_version < '3.5'
py>=1.5.2
virtualenv<20
Expand Down