diff --git a/.gitignore b/.gitignore index a008b436323..27bd93c7b3f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ env/ .tox .cache .pytest_cache +.mypy_cache .coverage .coverage.* coverage.xml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 12fa0d343f1..c6813fd48b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,6 +28,7 @@ repos: hooks: - id: flake8 language_version: python3 + additional_dependencies: [flake8-typing-imports] - repo: https://github.com/asottile/reorder_python_imports rev: v1.4.0 hooks: @@ -42,6 +43,15 @@ repos: rev: v1.4.0 hooks: - id: rst-backticks +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.711 + hooks: + - id: mypy + name: mypy (src) + files: ^src/ + - id: mypy + name: mypy (testing) + files: ^testing/ - repo: local hooks: - id: rst diff --git a/bench/bench.py b/bench/bench.py index 31cc7ac137c..c40fc8636c0 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -6,7 +6,7 @@ import pstats script = sys.argv[1:] if len(sys.argv) > 1 else ["empty.py"] - stats = cProfile.run("pytest.cmdline.main(%r)" % script, "prof") + cProfile.run("pytest.cmdline.main(%r)" % script, "prof") p = pstats.Stats("prof") p.strip_dirs() p.sort_stats("cumulative") diff --git a/setup.cfg b/setup.cfg index 2d6e5bee1e7..60e866562cd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -61,3 +61,11 @@ ignore = [devpi:upload] formats = sdist.tgz,bdist_wheel + +[mypy] +ignore_missing_imports = True +no_implicit_optional = True +strict_equality = True +warn_redundant_casts = True +warn_return_any = True +warn_unused_configs = True diff --git a/src/_pytest/_argcomplete.py b/src/_pytest/_argcomplete.py index 1ebf7432ca1..688c9077df2 100644 --- a/src/_pytest/_argcomplete.py +++ b/src/_pytest/_argcomplete.py @@ -56,6 +56,7 @@ import os import sys from glob import glob +from typing import Optional class FastFilesCompleter: @@ -91,7 +92,7 @@ def __call__(self, prefix, **kwargs): import argcomplete.completers except ImportError: sys.exit(-1) - filescompleter = FastFilesCompleter() + filescompleter = FastFilesCompleter() # type: Optional[FastFilesCompleter] def try_argcomplete(parser): argcomplete.autocomplete(parser, always_complete_options=False) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index b0b4d653119..6f520ebcf88 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -33,7 +33,8 @@ def __init__(self, rawcode): def __eq__(self, other): return self.raw == other.raw - __hash__ = None + # Ignore type because of https://github.com/python/mypy/issues/4266. + __hash__ = None # type: ignore def __ne__(self, other): return not self == other @@ -188,11 +189,11 @@ def path(self): """ path to the source code """ return self.frame.code.path - def getlocals(self): + @property + def locals(self): + """ locals of underlaying frame """ return self.frame.f_locals - locals = property(getlocals, None, None, "locals of underlaying frame") - def getfirstlinesource(self): return self.frame.code.firstlineno @@ -255,11 +256,11 @@ def __str__(self): line = "???" return " File %r:%d in %s\n %s\n" % (fn, self.lineno + 1, name, line) + @property def name(self): + """ co_name of underlaying code """ return self.frame.code.raw.co_name - name = property(name, None, None, "co_name of underlaying code") - class Traceback(list): """ Traceback objects encapsulate and offer higher level diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index 70d5f8fcdca..ea2fc5e3f53 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -44,7 +44,8 @@ def __eq__(self, other): return str(self) == other return False - __hash__ = None + # Ignore type because of https://github.com/python/mypy/issues/4266. + __hash__ = None # type: ignore def __getitem__(self, key): if isinstance(key, int): diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 8b2c1e14610..7bd46eeb6a8 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -12,6 +12,10 @@ import sys import tokenize import types +from typing import Dict +from typing import List +from typing import Optional +from typing import Set import atomicwrites @@ -459,17 +463,18 @@ def _fix(node, lineno, col_offset): return node -def _get_assertion_exprs(src: bytes): # -> Dict[int, str] +def _get_assertion_exprs(src: bytes) -> Dict[int, str]: """Returns a mapping from {lineno: "assertion test expression"}""" - ret = {} + ret = {} # type: Dict[int, str] depth = 0 - lines = [] - assert_lineno = None - seen_lines = set() + lines = [] # type: List[str] + assert_lineno = None # type: Optional[int] + seen_lines = set() # type: Set[int] def _write_and_reset() -> None: nonlocal depth, lines, assert_lineno, seen_lines + assert assert_lineno is not None ret[assert_lineno] = "".join(lines).rstrip().rstrip("\\") depth = 0 lines = [] @@ -477,21 +482,21 @@ def _write_and_reset() -> None: seen_lines = set() tokens = tokenize.tokenize(io.BytesIO(src).readline) - for tp, src, (lineno, offset), _, line in tokens: - if tp == tokenize.NAME and src == "assert": + for tp, source, (lineno, offset), _, line in tokens: + if tp == tokenize.NAME and source == "assert": assert_lineno = lineno elif assert_lineno is not None: # keep track of depth for the assert-message `,` lookup - if tp == tokenize.OP and src in "([{": + if tp == tokenize.OP and source in "([{": depth += 1 - elif tp == tokenize.OP and src in ")]}": + elif tp == tokenize.OP and source in ")]}": depth -= 1 if not lines: lines.append(line[offset:]) seen_lines.add(lineno) # a non-nested comma separates the expression from the message - elif depth == 0 and tp == tokenize.OP and src == ",": + elif depth == 0 and tp == tokenize.OP and source == ",": # one line assert with message if lineno in seen_lines and len(lines) == 1: offset_in_trimmed = offset + len(lines[-1]) - len(line) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 302979ef4be..f89aaefba40 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -547,6 +547,8 @@ def __init__(self, targetfd, tmpfile=None): self.start = lambda: None self.done = lambda: None else: + self.start = self._start + self.done = self._done if targetfd == 0: assert not tmpfile, "cannot set tmpfile with stdin" tmpfile = open(os.devnull, "r") @@ -568,7 +570,7 @@ def __repr__(self): self.targetfd, getattr(self, "targetfd_save", None), self._state ) - def start(self): + def _start(self): """ Start capturing on targetfd using memorized tmpfile. """ try: os.fstat(self.targetfd_save) @@ -585,7 +587,7 @@ def snap(self): self.tmpfile.truncate() return res - def done(self): + def _done(self): """ stop capturing, restore streams, return original capture file, seeked to position zero. """ targetfd_save = self.__dict__.pop("targetfd_save") @@ -618,7 +620,8 @@ class FDCapture(FDCaptureBinary): snap() produces text """ - EMPTY_BUFFER = str() + # Ignore type because it doesn't match the type in the superclass (bytes). + EMPTY_BUFFER = str() # type: ignore def snap(self): res = super().snap() @@ -679,7 +682,8 @@ def writeorg(self, data): class SysCaptureBinary(SysCapture): - EMPTY_BUFFER = b"" + # Ignore type because it doesn't match the type in the superclass (str). + EMPTY_BUFFER = b"" # type: ignore def snap(self): res = self.tmpfile.buffer.getvalue() diff --git a/src/_pytest/debugging.py b/src/_pytest/debugging.py index 891630b432d..2e3d49c377a 100644 --- a/src/_pytest/debugging.py +++ b/src/_pytest/debugging.py @@ -74,7 +74,7 @@ class pytestPDB: _pluginmanager = None _config = None - _saved = [] + _saved = [] # type: list _recursive_debug = 0 _wrapped_pdb_cls = None diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 3262b65bb55..965a2e6e9e2 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -6,6 +6,8 @@ from collections import defaultdict from collections import deque from collections import OrderedDict +from typing import Dict +from typing import Tuple import attr import py @@ -31,6 +33,9 @@ from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME +if False: # TYPE_CHECKING + from typing import Type + @attr.s(frozen=True) class PseudoFixtureDef: @@ -54,10 +59,10 @@ def pytest_sessionstart(session): session._fixturemanager = FixtureManager(session) -scopename2class = {} +scopename2class = {} # type: Dict[str, Type[nodes.Node]] -scope2props = dict(session=()) +scope2props = dict(session=()) # type: Dict[str, Tuple[str, ...]] scope2props["package"] = ("fspath",) scope2props["module"] = ("fspath", "module") scope2props["class"] = scope2props["module"] + ("cls",) @@ -960,7 +965,8 @@ class FixtureFunctionMarker: scope = attr.ib() params = attr.ib(converter=attr.converters.optional(tuple)) autouse = attr.ib(default=False) - ids = attr.ib(default=None, converter=_ensure_immutable_ids) + # Ignore type because of https://github.com/python/mypy/issues/6172. + ids = attr.ib(default=None, converter=_ensure_immutable_ids) # type: ignore name = attr.ib(default=None) def __call__(self, function): diff --git a/src/_pytest/mark/__init__.py b/src/_pytest/mark/__init__.py index 30c6e0048f5..e76bb78579d 100644 --- a/src/_pytest/mark/__init__.py +++ b/src/_pytest/mark/__init__.py @@ -91,7 +91,8 @@ def pytest_cmdline_main(config): return 0 -pytest_cmdline_main.tryfirst = True +# Ignore type because of https://github.com/python/mypy/issues/2087. +pytest_cmdline_main.tryfirst = True # type: ignore def deselect_by_keyword(items, config): diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index 1af7a9b42f2..0887d6b9c5f 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -3,6 +3,7 @@ from collections import namedtuple from collections.abc import MutableMapping from operator import attrgetter +from typing import Set import attr @@ -298,7 +299,7 @@ def test_function(): on the ``test_function`` object. """ _config = None - _markers = set() + _markers = set() # type: Set[str] def __getattr__(self, name): if name[0] == "_": diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 491cf9d2c09..7e1c40bcb76 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -280,7 +280,8 @@ def _repr_failure_py(self, excinfo, style=None): truncate_locals=truncate_locals, ) - repr_failure = _repr_failure_py + def repr_failure(self, excinfo, style=None): + return self._repr_failure_py(excinfo, style) def get_fslocation_from_item(item): diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index fb4d471b53c..c7e26f5cc8f 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -70,7 +70,8 @@ def exit(msg, returncode=None): raise Exit(msg, returncode) -exit.Exception = Exit +# Ignore type because of https://github.com/python/mypy/issues/2087. +exit.Exception = Exit # type: ignore def skip(msg="", *, allow_module_level=False): @@ -96,7 +97,8 @@ def skip(msg="", *, allow_module_level=False): raise Skipped(msg=msg, allow_module_level=allow_module_level) -skip.Exception = Skipped +# Ignore type because of https://github.com/python/mypy/issues/2087. +skip.Exception = Skipped # type: ignore def fail(msg="", pytrace=True): @@ -111,7 +113,8 @@ def fail(msg="", pytrace=True): raise Failed(msg=msg, pytrace=pytrace) -fail.Exception = Failed +# Ignore type because of https://github.com/python/mypy/issues/2087. +fail.Exception = Failed # type: ignore class XFailed(Failed): @@ -132,7 +135,8 @@ def xfail(reason=""): raise XFailed(reason) -xfail.Exception = XFailed +# Ignore type because of https://github.com/python/mypy/issues/2087. +xfail.Exception = XFailed # type: ignore def importorskip(modname, minversion=None, reason=None): diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 374fa598fb6..cbd83394606 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -9,6 +9,7 @@ from decimal import Decimal from itertools import filterfalse from numbers import Number +from typing import Union from more_itertools.more import always_iterable @@ -58,7 +59,8 @@ def __eq__(self, actual): a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual) ) - __hash__ = None + # Ignore type because of https://github.com/python/mypy/issues/4266. + __hash__ = None # type: ignore def __ne__(self, actual): return not (actual == self) @@ -202,8 +204,10 @@ class ApproxScalar(ApproxBase): Perform approximate comparisons where the expected value is a single number. """ - DEFAULT_ABSOLUTE_TOLERANCE = 1e-12 - DEFAULT_RELATIVE_TOLERANCE = 1e-6 + # Using Real should be better than this Union, but not possible yet: + # https://github.com/python/typeshed/pull/3108 + DEFAULT_ABSOLUTE_TOLERANCE = 1e-12 # type: Union[float, Decimal] + DEFAULT_RELATIVE_TOLERANCE = 1e-6 # type: Union[float, Decimal] def __repr__(self): """ @@ -261,7 +265,8 @@ def __eq__(self, actual): # Return true if the two numbers are within the tolerance. return abs(self.expected - actual) <= self.tolerance - __hash__ = None + # Ignore type because of https://github.com/python/mypy/issues/4266. + __hash__ = None # type: ignore @property def tolerance(self): @@ -691,7 +696,7 @@ def raises(expected_exception, *args, **kwargs): fail(message) -raises.Exception = fail.Exception +raises.Exception = fail.Exception # type: ignore class RaisesContext: diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index d2f1f33e2fe..4682d5b6ec2 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -1,4 +1,5 @@ from pprint import pprint +from typing import Optional import py @@ -28,7 +29,7 @@ def getslaveinfoline(node): class BaseReport: - when = None + when = None # type: Optional[str] location = None def __init__(self, **kw): diff --git a/src/_pytest/tmpdir.py b/src/_pytest/tmpdir.py index f2c4d905cf5..48680c07ea5 100644 --- a/src/_pytest/tmpdir.py +++ b/src/_pytest/tmpdir.py @@ -26,7 +26,10 @@ class TempPathFactory: # using os.path.abspath() to get absolute path instead of resolve() as it # does not work the same in all platforms (see #4427) # Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012) - converter=attr.converters.optional(lambda p: Path(os.path.abspath(str(p)))) + # Ignore type because of https://github.com/python/mypy/issues/6172. + converter=attr.converters.optional( + lambda p: Path(os.path.abspath(str(p))) # type: ignore + ) ) _trace = attr.ib() _basetemp = attr.ib(default=None) diff --git a/testing/__init__.py b/testing/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index f7787c282f7..d7771833a7b 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -589,7 +589,8 @@ def __repr__(self): def test_repr_local_with_exception_in_class_property(self): class ExceptionWithBrokenClass(Exception): - @property + # Type ignored because it's bypassed intentionally. + @property # type: ignore def __class__(self): raise TypeError("boom!") diff --git a/testing/example_scripts/collect/package_infinite_recursion/__init__.py b/testing/example_scripts/collect/package_infinite_recursion/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/config/collect_pytest_prefix/__init__.py b/testing/example_scripts/config/collect_pytest_prefix/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/conftest_usageerror/__init__.py b/testing/example_scripts/conftest_usageerror/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/fixtures/custom_item/__init__.py b/testing/example_scripts/fixtures/custom_item/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/__init__.py b/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/__init__.py b/testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_module/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/issue88_initial_file_multinodes/__init__.py b/testing/example_scripts/issue88_initial_file_multinodes/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/example_scripts/marks/marks_considered_keywords/__init__.py b/testing/example_scripts/marks/marks_considered_keywords/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testing/python/raises.py b/testing/python/raises.py index c9ede412ae9..6d9604b1d8e 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -258,7 +258,8 @@ def test_raises_with_raising_dunder_class(self): """Test current behavior with regard to exceptions via __class__ (#4284).""" class CrappyClass(Exception): - @property + # Type ignored because it's bypassed intentionally. + @property # type: ignore def __class__(self): assert False, "via __class__" diff --git a/testing/test_compat.py b/testing/test_compat.py index 028d48bed30..9e7d05c5df5 100644 --- a/testing/test_compat.py +++ b/testing/test_compat.py @@ -141,7 +141,8 @@ def test_safe_isclass(): assert safe_isclass(type) is True class CrappyClass(Exception): - @property + # Type ignored because it's bypassed intentionally. + @property # type: ignore def __class__(self): assert False, "Should be ignored" diff --git a/testing/test_pdb.py b/testing/test_pdb.py index f3f7ca70233..8d327cbb322 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -6,7 +6,8 @@ from _pytest.debugging import _validate_usepdb_cls try: - breakpoint + # Type ignored for Python <= 3.6. + breakpoint # type: ignore except NameError: SUPPORTS_BREAKPOINT_BUILTIN = False else: