Skip to content

test_positional_args and test_keyword_args throw warnings with pytest 8.4 #1755

@s-t-e-v-e-n-k

Description

@s-t-e-v-e-n-k

Both of these test cases fail running with pytest 8.4, giving the following warning:

pytest.PytestWarning: matching against an empty string will *always* pass. If you want to check for an empty message you need to pass '^$'. If you don't want to match you should pass `None` or leave out the parameter.

Picking on one testcase (the tracebacks are all very similar):

[   19s] _ test_keyword_args[json-ArgsKwargs(())-Err('', errors=[{'type': 'missing_keyword_only_argument', 'loc': ('a',), 'msg': 'Missing required keyword only argument', 'input': ArgsKwargs(())}, {'type': 'missing_keyword_only_argument', 'loc': ('b',), 'msg': 'Missing required keyword only argument', 'input': ArgsKwargs(())}, {'type': 'missing_keyword_only_argument', 'loc': ('c',), 'msg': 'Missing required keyword only argument', 'input': ArgsKwargs(())}])] _
[   19s]
[   19s] py_and_json = <class 'tests.conftest.py_and_json.<locals>.ChosenPyAndJsonValidator'>
[   19s] input_value = ArgsKwargs(())
[   19s] expected = Err('', errors=[{'type': 'missing_keyword_only_argument', 'loc': ('a',), 'msg': 'Missing required keyword only argumen...sing_keyword_only_argument', 'loc': ('c',), 'msg': 'Missing required keyword only argument', 'input': ArgsKwargs(())}])
[   19s]
[   19s]     @pytest.mark.parametrize(
[   19s]         'input_value,expected',
[   19s]         [
[   19s]             [ArgsKwargs((), {'a': 1, 'b': 'a', 'c': True}), ((), {'a': 1, 'b': 'a', 'c': True})],
[   19s]             [{'a': 1, 'b': 'a', 'c': True}, ((), {'a': 1, 'b': 'a', 'c': True})],
[   19s]             [ArgsKwargs((), {'a': '1', 'b': 'a', 'c': 'True'}), ((), {'a': 1, 'b': 'a', 'c': True})],
[   19s]             [ArgsKwargs((), {'a': 1, 'b': 'a', 'c': True}), ((), {'a': 1, 'b': 'a', 'c': True})],
[   19s]             [ArgsKwargs((1,), {'a': 1, 'b': 'a', 'c': True}), Err('type=unexpected_positional_argument,')],
[   19s]             [
[   19s]                 ArgsKwargs((), {'a': 1, 'b': 'a', 'c': True, 'd': 'wrong'}),
[   19s]                 Err(
[   19s]                     'type=unexpected_keyword_argument,',
[   19s]                     [
[   19s]                         {
[   19s]                             'type': 'unexpected_keyword_argument',
[   19s]                             'loc': ('d',),
[   19s]                             'msg': 'Unexpected keyword argument',
[   19s]                             'input': 'wrong',
[   19s]                         }
[   19s]                     ],
[   19s]                 ),
[   19s]             ],
[   19s]             [
[   19s]                 ArgsKwargs((), {'a': 1, 'b': 'a'}),
[   19s]                 Err(
[   19s]                     'type=missing_keyword_only_argument,',
[   19s]                     [
[   19s]                         {
[   19s]                             'type': 'missing_keyword_only_argument',
[   19s]                             'loc': ('c',),
[   19s]                             'msg': 'Missing required keyword only argument',
[   19s]                             'input': ArgsKwargs((), {'a': 1, 'b': 'a'}),
[   19s]                         }
[   19s]                     ],
[   19s]                 ),
[   19s]             ],
[   19s]             [
[   19s]                 ArgsKwargs((), {'a': 'x', 'b': 'a', 'c': 'wrong'}),
[   19s]                 Err(
[   19s]                     '',
[   19s]                     [
[   19s]                         {
[   19s]                             'type': 'int_parsing',
[   19s]                             'loc': ('a',),
[   19s]                             'msg': 'Input should be a valid integer, unable to parse string as an integer',
[   19s]                             'input': 'x',
[   19s]                         },
[   19s]                         {
[   19s]                             'type': 'bool_parsing',
[   19s]                             'loc': ('c',),
[   19s]                             'msg': 'Input should be a valid boolean, unable to interpret input',
[   19s]                             'input': 'wrong',
[   19s]                         },
[   19s]                     ],
[   19s]                 ),
[   19s]             ],
[   19s]             [
[   19s]                 ArgsKwargs(()),
[   19s]                 Err(
[   19s]                     '',
[   19s]                     [
[   19s]                         {
[   19s]                             'type': 'missing_keyword_only_argument',
[   19s]                             'loc': ('a',),
[   19s]                             'msg': 'Missing required keyword only argument',
[   19s]                             'input': ArgsKwargs(()),
[   19s]                         },
[   19s]                         {
[   19s]                             'type': 'missing_keyword_only_argument',
[   19s]                             'loc': ('b',),
[   19s]                             'msg': 'Missing required keyword only argument',
[   19s]                             'input': ArgsKwargs(()),
[   19s]                         },
[   19s]                         {
[   19s]                             'type': 'missing_keyword_only_argument',
[   19s]                             'loc': ('c',),
[   19s]                             'msg': 'Missing required keyword only argument',
[   19s]                             'input': ArgsKwargs(()),
[   19s]                         },
[   19s]                     ],
[   19s]                 ),
[   19s]             ],
[   19s]         ],
[   19s]         ids=repr,
[   19s]     )
[   19s]     def test_keyword_args(py_and_json: PyAndJson, input_value, expected):
[   19s]         v = py_and_json(
[   19s]             {
[   19s]                 'type': 'arguments',
[   19s]                 'arguments_schema': [
[   19s]                     {'name': 'a', 'mode': 'keyword_only', 'schema': {'type': 'int'}},
[   19s]                     {'name': 'b', 'mode': 'keyword_only', 'schema': {'type': 'str'}},
[   19s]                     {'name': 'c', 'mode': 'keyword_only', 'schema': {'type': 'bool'}},
[   19s]                 ],
[   19s]             }
[   19s]         )
[   19s]         if isinstance(expected, Err):
[   19s] >           with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
[   19s]                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[   19s]
[   19s] tests/validators/test_arguments.py:282:
[   19s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
[   19s] /usr/lib/python3.11/site-packages/_pytest/raises.py:624: in __init__
[   19s]     super().__init__(match=match, check=check)
[   19s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
[   19s]
[   19s] self = <[AttributeError("'RaisesExc' object has no attribute 'expected_exceptions'") raised in repr()] RaisesExc object at 0x7f90f82a0990>
[   19s]
[   19s]     def __init__(
[   19s]         self,
[   19s]         *,
[   19s]         match: str | Pattern[str] | None,
[   19s]         check: Callable[[BaseExcT_co], bool] | None,
[   19s]     ) -> None:
[   19s]         if isinstance(match, str):
[   19s]             # juggle error in order to avoid context to fail (necessary?)
[   19s]             re_error = None
[   19s]             try:
[   19s]                 self.match: Pattern[str] | None = re.compile(match)
[   19s]             except re.error as e:
[   19s]                 re_error = e
[   19s]             if re_error is not None:
[   19s]                 fail(f"Invalid regex pattern provided to 'match': {re_error}")
[   19s]             if match == "":
[   19s] >               warnings.warn(
[   19s]                     PytestWarning(
[   19s]                         "matching against an empty string will *always* pass. If you want "
[   19s]                         "to check for an empty message you need to pass '^$'. If you don't "
[   19s]                         "want to match you should pass `None` or leave out the parameter."
[   19s]                     ),
[   19s]                     stacklevel=2,
[   19s]                 )
[   19s] E               pytest.PytestWarning: matching against an empty string will *always* pass. If you want to check for an empty message you need to pass '^$'. If you don't want to match you should pass `None` or leave out the parameter.
[   19s]
[   19s] /usr/lib/python3.11/site-packages/_pytest/raises.py:401: PytestWarning

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions