Skip to content

Commit

Permalink
[WIP] add type annotations to from_parents and its usages, BROKEN
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Jan 24, 2020
1 parent 8ba0b7b commit 11e48b3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
16 changes: 13 additions & 3 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import traceback
import warnings
from contextlib import contextmanager
from typing import cast
from typing import Dict
from typing import List
from typing import Optional
Expand All @@ -14,6 +15,7 @@
from typing import Union

import pytest
from _pytest import nodes
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
Expand Down Expand Up @@ -217,13 +219,21 @@ def __init__(self, name, parent, runner=None, dtest=None):

@classmethod
def from_parent( # type: ignore
cls, parent: "Union[DoctestTextfile, DoctestModule]", *, name, runner, dtest
):
cls: Type[nodes.N],
parent: "Union[DoctestTextfile, DoctestModule]",
*,
name,
runner,
dtest
) -> nodes.N:
# incompatible signature due to to imposed limits on sublcass
"""
the public named constructor
"""
return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)
return cast(
nodes.N,
super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest),
)

def setup(self):
if self.dtest is not None:
Expand Down
9 changes: 7 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import warnings
from functools import lru_cache
from typing import Any
from typing import cast
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Tuple
from typing import TypeVar
from typing import Union

import py
Expand All @@ -30,6 +32,9 @@
if False: # TYPE_CHECKING
# Imported here due to circular import.
from _pytest.main import Session # noqa: F401
from typing import Type

N = TypeVar("N", bound="Node")

SEP = "/"

Expand Down Expand Up @@ -144,7 +149,7 @@ def __init__(
self._nodeid += "::" + self.name

@classmethod
def from_parent(cls, parent: "Node", **kw):
def from_parent(cls: Type[N], parent: "Node", **kw) -> N:
"""
Public Constructor for Nodes
Expand All @@ -159,7 +164,7 @@ def from_parent(cls, parent: "Node", **kw):
raise TypeError("config is not a valid argument for from_parent")
if "session" in kw:
raise TypeError("session is not a valid argument for from_parent")
return cls._create(parent=parent, **kw)
return cast(N, cls._create(parent=parent, **kw))

@property
def ihook(self):
Expand Down
11 changes: 8 additions & 3 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from collections.abc import Sequence
from functools import partial
from textwrap import dedent
from typing import cast
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union

import py

Expand Down Expand Up @@ -45,6 +47,9 @@
from _pytest.warning_types import PytestCollectionWarning
from _pytest.warning_types import PytestUnhandledCoroutineWarning

if False: # TYPE_CHECKING
from typing import Type


def pyobj_property(name):
def get(self):
Expand Down Expand Up @@ -188,7 +193,7 @@ def path_matches_patterns(path, patterns):
return any(path.fnmatch(pattern) for pattern in patterns)


def pytest_pycollect_makemodule(path, parent):
def pytest_pycollect_makemodule(path, parent) -> Union["Package", "Module"]:
if path.basename == "__init__.py":
return Package.from_parent(parent, fspath=path)
return Module.from_parent(parent, fspath=path)
Expand Down Expand Up @@ -678,11 +683,11 @@ class Class(PyCollector):
""" Collector for test methods. """

@classmethod
def from_parent(cls, parent, *, name, obj=None):
def from_parent(cls: Type[nodes.N], parent, *, name, obj=None) -> nodes.N:
"""
The public constructor
"""
return super().from_parent(name=name, parent=parent)
return cast(nodes.N, super().from_parent(name=name, parent=parent))

def collect(self):
if not safe_getattr(self.obj, "__test__", True):
Expand Down
2 changes: 1 addition & 1 deletion testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def __call__(self, tmpdir):
)

@staticmethod
def make_function(testdir, **kwargs):
def make_function(testdir, **kwargs) -> pytest.Function:
from _pytest.fixtures import FixtureManager

config = testdir.parseconfigure()
Expand Down

0 comments on commit 11e48b3

Please sign in to comment.