Skip to content

Fix test_decorator issue #269 #270

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

Closed
wants to merge 5 commits into from
Closed
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
58 changes: 33 additions & 25 deletions test/test_decorator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Tests simple parsing decorators.
"""
"""Tests simple parsing decorators."""
import collections
import dataclasses
import functools
import inspect
import sys
import typing
from typing import Callable

import typing
import inspect
import pytest

import simple_parsing as sp
Expand Down Expand Up @@ -43,6 +41,14 @@ def pop(self, index, default_value=None):
return default_value


def delay_evaluation_wrapper(fn: Callable) -> Callable:
def delayed_call(*args, **kwargs):
return lambda: fn(*args, **kwargs)

return delayed_call


@delay_evaluation_wrapper
def partial(fn: Callable, *args, **kwargs) -> Callable:
"""Partial via changing the signature defaults."""

Expand All @@ -69,29 +75,41 @@ def _wrapper(*other_args, **other_kwargs):
return _wrapper


def _xfail_in_py311(*param):
return pytest.param(
*param,
marks=pytest.mark.xfail(
sys.version_info >= (3, 11),
reason="TODO: test doesn't work in Python 3.11",
strict=True,
),
)


@pytest.mark.parametrize(
"args, expected, fn",
"args, expected, delay_wrapper",
[
("", 1, partial(_fn_with_positional_only, 1)),
("2", 2, partial(_fn_with_positional_only, 1)),
("2", 2, _fn_with_positional_only),
("2", 2, partial(_fn_with_positional_only)),
Copy link
Owner

@lebrice lebrice Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there @zhiruiluo , Would you mind explaining a few things:

  • why is the delay_wrapper necessary here?
  • Did the inspect module change from 3.11 to 3.11.3? Is that what broke this test?

Copy link
Contributor Author

@zhiruiluo zhiruiluo Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recently PR python/cpython#103557 disallows pos-or-kw params without default after pos-only with default, which breaks the test for Python 3.11.4 released on June 6.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will likely backport this change to all Python 3.11 verions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply @lebrice. The delay_wrapper can postpone the generation of partial with changing default function until test collected. If the error happens in pytest collection time, we can't use xfail to bypass the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked the backport status. This PR python/cpython#103675 has backport inspect.Signature change to Python 3.11

("", 1, partial(_fn_with_keyword_only, x=1)),
("--x=2", 2, partial(_fn_with_keyword_only, x=1)),
("--x=2", 2, _fn_with_keyword_only),
("--x=2", 2, partial(_fn_with_keyword_only)),
("", 3, partial(_fn_with_all_argument_types, 1, b=1, c=1)),
("2", 4, partial(_fn_with_all_argument_types, b=1, c=1)),
("2 --b=2", 5, partial(_fn_with_all_argument_types, c=1)),
("2 --b=2 --c=2", 6, _fn_with_all_argument_types),
("2 --b=2 --c=2", 6, partial(_fn_with_all_argument_types)),
("--c=2", 4, partial(_fn_with_all_argument_types, 1, b=1)),
("--b=2", 4, partial(_fn_with_all_argument_types, 1, c=1)),
("--b=2 --c=2", 5, partial(_fn_with_all_argument_types, 1)),
_xfail_in_py311("--b=2", 4, partial(_fn_with_all_argument_types, 1, c=1)),
_xfail_in_py311("--b=2 --c=2", 5, partial(_fn_with_all_argument_types, 1)),
],
)
def test_simple_arguments(
args: str,
expected: int,
fn: Callable,
delay_wrapper: Callable,
):
fn = delay_wrapper()
decorated = sp.decorators.main(fn, args=args)
assert decorated() == expected

Expand All @@ -100,19 +118,8 @@ def _fn_with_nested_dataclass(x: int, /, *, data: AddThreeNumbers) -> int:
return x + data()


def _xfail_in_py311(*param):
return pytest.param(
*param,
marks=pytest.mark.xfail(
sys.version_info >= (3, 11),
reason="TODO: test doesn't work in Python 3.11",
strict=True,
),
)


@pytest.mark.parametrize(
"args, expected, fn",
"args, expected, delay_wrapper",
[
_xfail_in_py311("", 1, partial(_fn_with_nested_dataclass, 1, data=AddThreeNumbers())),
("--a=1", 2, partial(_fn_with_nested_dataclass, 1)),
Expand All @@ -124,7 +131,8 @@ def _xfail_in_py311(*param):
def test_nested_dataclass(
args: str,
expected: int,
fn: Callable,
delay_wrapper: Callable,
):
fn = delay_wrapper()
decorated = sp.decorators.main(fn, args=args)
assert decorated() == expected