Merge pull request #8367 from The-Compiler/class-fromparent-kw

This commit is contained in:
Bruno Oliveira 2021-02-23 16:25:14 -03:00 committed by GitHub
commit bbea18d7f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix ``Class.from_parent`` so it forwards extra keyword arguments to the constructor.

View File

@ -763,9 +763,9 @@ class Class(PyCollector):
"""Collector for test methods.""" """Collector for test methods."""
@classmethod @classmethod
def from_parent(cls, parent, *, name, obj=None): def from_parent(cls, parent, *, name, obj=None, **kw):
"""The public constructor.""" """The public constructor."""
return super().from_parent(name=name, parent=parent) return super().from_parent(name=name, parent=parent, **kw)
def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
if not safe_getattr(self.obj, "__test__", True): if not safe_getattr(self.obj, "__test__", True):

View File

@ -1360,6 +1360,22 @@ def test_fscollector_from_parent(pytester: Pytester, request: FixtureRequest) ->
assert collector.x == 10 assert collector.x == 10
def test_class_from_parent(pytester: Pytester, request: FixtureRequest) -> None:
"""Ensure Class.from_parent can forward custom arguments to the constructor."""
class MyCollector(pytest.Class):
def __init__(self, name, parent, x):
super().__init__(name, parent)
self.x = x
@classmethod
def from_parent(cls, parent, *, name, x):
return super().from_parent(parent=parent, name=name, x=x)
collector = MyCollector.from_parent(parent=request.session, name="foo", x=10)
assert collector.x == 10
class TestImportModeImportlib: class TestImportModeImportlib:
def test_collect_duplicate_names(self, pytester: Pytester) -> None: def test_collect_duplicate_names(self, pytester: Pytester) -> None:
"""--import-mode=importlib can import modules with same names that are not in packages.""" """--import-mode=importlib can import modules with same names that are not in packages."""