Skip to content

Commit 930a158

Browse files
committed
Fix type errors after adding types to the py dependency
1 parent d347a30 commit 930a158

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

src/_pytest/config/argparsing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def parse(self, args, namespace=None):
8282

8383
self.optparser = self._getparser()
8484
try_argcomplete(self.optparser)
85-
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
86-
return self.optparser.parse_args(args, namespace=namespace)
85+
strargs = [str(x) if isinstance(x, py.path.local) else x for x in args]
86+
return self.optparser.parse_args(strargs, namespace=namespace)
8787

8888
def _getparser(self) -> "MyOptionParser":
8989
from _pytest._argcomplete import filescompleter
@@ -124,8 +124,8 @@ def parse_known_and_unknown_args(
124124
the remaining arguments unknown at this point.
125125
"""
126126
optparser = self._getparser()
127-
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
128-
return optparser.parse_known_args(args, namespace=namespace)
127+
strargs = [str(x) if isinstance(x, py.path.local) else x for x in args]
128+
return optparser.parse_known_args(strargs, namespace=namespace)
129129

130130
def addini(self, name, help, type=None, default=None):
131131
""" register an ini-file option.

src/_pytest/config/findpaths.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
2+
from typing import Any
3+
from typing import Iterable
24
from typing import List
35
from typing import Optional
6+
from typing import Tuple
47

58
import py
69

@@ -60,7 +63,7 @@ def getcfg(args, config=None):
6063
return None, None, None
6164

6265

63-
def get_common_ancestor(paths):
66+
def get_common_ancestor(paths: Iterable[py.path.local]) -> py.path.local:
6467
common_ancestor = None
6568
for path in paths:
6669
if not path.exists():
@@ -113,7 +116,7 @@ def determine_setup(
113116
args: List[str],
114117
rootdir_cmd_arg: Optional[str] = None,
115118
config: Optional["Config"] = None,
116-
):
119+
) -> Tuple[py.path.local, Optional[str], Any]:
117120
dirs = get_dirs_from_args(args)
118121
if inifile:
119122
iniconfig = py.iniconfig.IniConfig(inifile)

src/_pytest/doctest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def repr_failure(self, excinfo):
308308
else:
309309
return super().repr_failure(excinfo)
310310

311-
def reportinfo(self) -> Tuple[str, int, str]:
311+
def reportinfo(self) -> Tuple[py.path.local, int, str]:
312312
return self.fspath, self.dtest.lineno, "[doctest] %s" % self.name
313313

314314

src/_pytest/fixtures.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def __init__(self, pyfuncitem):
351351
self.fixturename = None
352352
#: Scope string, one of "function", "class", "module", "session"
353353
self.scope = "function"
354-
self._fixture_defs = {} # argname -> FixtureDef
354+
self._fixture_defs = {} # type: Dict[str, FixtureDef]
355355
fixtureinfo = pyfuncitem._fixtureinfo
356356
self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy()
357357
self._arg2index = {}
@@ -426,7 +426,8 @@ def module(self):
426426
@scopeproperty()
427427
def fspath(self) -> py.path.local:
428428
""" the file system path of the test module which collected this test. """
429-
return self._pyfuncitem.fspath
429+
# TODO: Remove ignore once _pyfuncitem is properly typed.
430+
return self._pyfuncitem.fspath # type: ignore
430431

431432
@property
432433
def keywords(self):
@@ -549,7 +550,9 @@ def _compute_fixture_value(self, fixturedef):
549550
source_lineno = frameinfo.lineno
550551
source_path = py.path.local(source_path)
551552
if source_path.relto(funcitem.config.rootdir):
552-
source_path = source_path.relto(funcitem.config.rootdir)
553+
source_path_str = source_path.relto(funcitem.config.rootdir)
554+
else:
555+
source_path_str = str(source_path)
553556
msg = (
554557
"The requested fixture has no parameter defined for test:\n"
555558
" {}\n\n"
@@ -558,7 +561,7 @@ def _compute_fixture_value(self, fixturedef):
558561
funcitem.nodeid,
559562
fixturedef.argname,
560563
getlocation(fixturedef.func, funcitem.config.rootdir),
561-
source_path,
564+
source_path_str,
562565
source_lineno,
563566
)
564567
)

src/_pytest/main.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ class Failed(Exception):
367367

368368
@attr.s
369369
class _bestrelpath_cache(dict):
370-
path = attr.ib()
370+
path = attr.ib(type=py.path.local)
371371

372-
def __missing__(self, path: str) -> str:
372+
def __missing__(self, path: py.path.local) -> str:
373373
r = self.path.bestrelpath(path) # type: str
374374
self[path] = r
375375
return r
@@ -399,7 +399,7 @@ def __init__(self, config):
399399
self._node_cache = {}
400400
self._bestrelpathcache = _bestrelpath_cache(
401401
config.rootdir
402-
) # type: Dict[str, str]
402+
) # type: Dict[py.path.local, str]
403403
# Dirnames of pkgs with dunder-init files.
404404
self._pkg_roots = {}
405405

@@ -414,7 +414,7 @@ def __repr__(self):
414414
self.testscollected,
415415
)
416416

417-
def _node_location_to_relpath(self, node_path: str) -> str:
417+
def _node_location_to_relpath(self, node_path: py.path.local) -> str:
418418
# bestrelpath is a quite slow function
419419
return self._bestrelpathcache[node_path]
420420

src/_pytest/nodes.py

+1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def reportinfo(self) -> Tuple[Union[py.path.local, str], Optional[int], str]:
462462
@cached_property
463463
def location(self) -> Tuple[str, Optional[int], str]:
464464
location = self.reportinfo()
465+
assert isinstance(location[0], py.path.local), location[0]
465466
fspath = self.session._node_location_to_relpath(location[0])
466467
assert type(location[2]) is str
467468
return (fspath, location[1], location[2])

testing/python/collect.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1018,18 +1018,18 @@ class TestReportInfo:
10181018
def test_itemreport_reportinfo(self, testdir):
10191019
testdir.makeconftest(
10201020
"""
1021-
import pytest
1021+
import pytest, py
10221022
class MyFunction(pytest.Function):
10231023
def reportinfo(self):
1024-
return "ABCDE", 42, "custom"
1024+
return py.path.local("foo"), 42, "custom"
10251025
def pytest_pycollect_makeitem(collector, name, obj):
10261026
if name == "test_func":
10271027
return MyFunction(name, parent=collector)
10281028
"""
10291029
)
10301030
item = testdir.getitem("def test_func(): pass")
10311031
item.config.pluginmanager.getplugin("runner")
1032-
assert item.location == ("ABCDE", 42, "custom")
1032+
assert item.location == ("foo", 42, "custom")
10331033

10341034
def test_func_reportinfo(self, testdir):
10351035
item = testdir.getitem("def test_func(): pass")

0 commit comments

Comments
 (0)