Skip to content
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

SyntaxError when a case id is a reserved keyword and the case is transformed into a fixture behind the scenes #230

Closed
wonboyn opened this issue Sep 14, 2021 · 7 comments

Comments

@wonboyn
Copy link

wonboyn commented Sep 14, 2021

Hi there,

I've just started getting an error during pytest discovery/collection.
The error is for one specific test function but I'm struggling to understand what the issue is.

When I run discovery, it reports the following error:
/usr/local/lib/python3.6/dist-packages/_pytest/python.py:578: in _importtestmodule mod = import_path(self.fspath, mode=importmode) /usr/local/lib/python3.6/dist-packages/_pytest/pathlib.py:524: in import_path importlib.import_module(module_name) /usr/lib/python3.6/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) <frozen importlib._bootstrap>:994: in _gcd_import ??? <frozen importlib._bootstrap>:971: in _find_and_load ??? <frozen importlib._bootstrap>:955: in _find_and_load_unlocked ??? <frozen importlib._bootstrap>:665: in _load_unlocked ??? /usr/local/lib/python3.6/dist-packages/_pytest/assertion/rewrite.py:170: in exec_module exec(co, module.__dict__) testing/unit/web/models/test_aws_credential_pool_dao.py:72: in <module> @parametrize_with_cases('val,exp', cases=GetFirstNotUsedTestCases) /usr/local/lib/python3.6/dist-packages/pytest_cases/common_pytest.py:894: in apply return apply_decorator(test_or_fixture_func, container) /usr/local/lib/python3.6/dist-packages/pytest_cases/case_parametrizer_new.py:161: in _apply_parametrization return _parametrize_with_cases(f, host_class_or_module) /usr/local/lib/python3.6/dist-packages/pytest_cases/fixture_parametrize_plus.py:1020: in parametrize_plus_decorate idstyle=_idstyle, scope=scope) /usr/local/lib/python3.6/dist-packages/pytest_cases/fixture_core1_unions.py:379: in _fixture_union @with_signature("%s(%s, request)" % (name, ', '.join(unique_fix_alt_names))) /usr/local/lib/python3.6/dist-packages/makefun/main.py:940: in replace_f **attrs /usr/local/lib/python3.6/dist-packages/makefun/main.py:190: in create_function func_name_from_str, func_signature, func_signature_str = get_signature_from_string(func_signature, evaldict) /usr/local/lib/python3.6/dist-packages/makefun/main.py:441: in get_signature_from_string dummy_f = _make(func_name_, [], body, evaldict) /usr/local/lib/python3.6/dist-packages/makefun/main.py:612: in _make code = compile(body, filename, 'single') E File "<makefun-gen-212>", line 1 E def test_get_first_not_used_val_exp(None, request): E ^ E SyntaxError: invalid syntax ------------------------------------------------------------------- Captured stderr ------------------------------------------------------------------- Error in generated code: def test_get_first_not_used_val_exp(None, request): pass

The test cases class (GetFirstNotUsedTestCases) consists of the following:
`class GetFirstNotUsedTestCases:

@case(id="None")
def case_none(self, manage_test_data):
    # Grab the required values
    valid_user = manage_test_data["user_pool"][7].user_name
    return None, valid_user

`

Any help to figure out what's going wrong would be greatly appreciated.

Cheers

Tim

@wonboyn
Copy link
Author

wonboyn commented Sep 14, 2021

Hi there,

I found the issue. I had a couple of test cases where I had used a reserved word as the case id - specifically I had used the following as case id's: None, False, True

It would be nice to be able to use any string as a case id & not have to worry about this sort of issue.

Thanks

Tim

@smarie
Copy link
Owner

smarie commented Sep 17, 2021

Thanks @wonboyn !! Indeed I was able to reproduce the bug. It arises when a case function is transformed into a fixture, and that fixture gets a name that can not actually be used as an argument name (False, True, None, ... as you pointed out).

Here is a reproducible example

from pytest_cases import parametrize_with_cases


class FooCases:

    def case_None(self, tmpdir):
        return 1

    def case_True(self, tmpdir):
        return 1

    def case_False(self, tmpdir):
        return 1


@parametrize_with_cases("foo", cases=FooCases)
def test_issue_230(foo):
    pass

@smarie smarie changed the title Test discovery issue SyntaxError when a case id is a reserved keyword and the case is transformed into a fixture behind the scenes Sep 17, 2021
@smarie smarie closed this as completed in 18fa41d Sep 17, 2021
@smarie
Copy link
Owner

smarie commented Sep 17, 2021

Fixed in 3.6.4 to be released soon

@smarie
Copy link
Owner

smarie commented Sep 17, 2021

3.6.4 is available. Can you try and let me know ? Thanks !

@wonboyn
Copy link
Author

wonboyn commented Oct 6, 2021

Thanks for that. Works a treat

@smarie
Copy link
Owner

smarie commented Oct 6, 2021

Thanks for the feedback @wonboyn !

@lawschlosser
Copy link

lawschlosser commented Aug 22, 2022

[EDIT] Nevermind, didn't realize 3.x tag/releases also work with python 2.7
@smarie Any chance this can be backported to a python 2.x release? (sorry, had to ask)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants