From 18fa41d18914d6f02c22a3f61d38199f7c8daee4 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Fri, 17 Sep 2021 10:09:17 +0200 Subject: [PATCH] A case id can now be a reserved keyword without triggering any `SyntaxError`, even if the case is transformed into a fixture. Fixes #230 --- docs/changelog.md | 4 ++++ pytest_cases/common_others.py | 8 +++++++- .../tests/cases/issues/test_issue_230.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 pytest_cases/tests/cases/issues/test_issue_230.py diff --git a/docs/changelog.md b/docs/changelog.md index 2a4afc63..6d7f6024 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,9 @@ # Changelog +### 3.6.4 - Bugfix + +- A case id can now be a reserved keyword without triggering any `SyntaxError`, even if the case is transformed into a fixture. Fixes [#230](https://github.com/smarie/python-pytest-cases/issues/230) + ### 3.6.3 - Bugfix - Fixed an issue where a lazy value would not be resolved. This happens when the "auto-simplify fixture" happens in `@parametrize`. Fixes [#225](https://github.com/smarie/python-pytest-cases/issues/225) diff --git a/pytest_cases/common_others.py b/pytest_cases/common_others.py index 8bb87854..706aa33d 100644 --- a/pytest_cases/common_others.py +++ b/pytest_cases/common_others.py @@ -4,6 +4,7 @@ # License: 3-clause BSD, import functools import inspect +from keyword import iskeyword import makefun from importlib import import_module from inspect import findsource @@ -553,7 +554,12 @@ def make_identifier(name # type: str """Transform the given name into a valid python identifier""" if not isinstance(name, string_types): raise TypeError("name should be a string, found : %r" % name) - elif isidentifier(name): + + if iskeyword(name): + # reserved keywords: add an underscore + name = name + "_" + + if isidentifier(name): return name elif len(name) == 0: # empty string diff --git a/pytest_cases/tests/cases/issues/test_issue_230.py b/pytest_cases/tests/cases/issues/test_issue_230.py new file mode 100644 index 00000000..96dd9c75 --- /dev/null +++ b/pytest_cases/tests/cases/issues/test_issue_230.py @@ -0,0 +1,18 @@ +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