From ab3b7190d728b18512141b9f5f3a1c3dfc7cedf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylvain=20Mari=C3=A9?= Date: Thu, 23 Feb 2023 14:11:25 +0100 Subject: [PATCH] Fixed `AttributeError` issue in `is_case_function` when an inspected symbol is a parametrized type hint without `__name__` (#294) * Fixed #292 by dropping the associated nox sessions * Fixed `AttributeError: __name__` issue in `is_case_function` when an inspected symbol comes from the typing package. Fixes #287 --------- Co-authored-by: Sylvain MARIE --- docs/changelog.md | 4 +++- src/pytest_cases/case_funcs.py | 6 +++++- tests/cases/issues/test_py35_issue_287.py | 24 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/cases/issues/test_py35_issue_287.py diff --git a/docs/changelog.md b/docs/changelog.md index b6efac93..01856108 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,7 +1,9 @@ # Changelog -### 3.6.14 - bugfix +### 3.6.14 - bugfixes + - Fixed `AttributeError` issue in `is_case_function` when an inspected symbol is a parametrized type hint + without `__name__`. Fixes [#287](https://github.com/smarie/python-pytest-cases/issues/287) - Fixed issue with `get_all_cases`: default value for `cases` was wrong. Fixes [#290](https://github.com/smarie/python-pytest-cases/issues/290) ### 3.6.13 - bugfix diff --git a/src/pytest_cases/case_funcs.py b/src/pytest_cases/case_funcs.py index 8a095a25..02dbcafb 100644 --- a/src/pytest_cases/case_funcs.py +++ b/src/pytest_cases/case_funcs.py @@ -361,4 +361,8 @@ def is_case_function(f, # type: Any # a function generated by us. ignore this return False else: - return f.__name__.startswith(prefix) if check_prefix else True + try: + return f.__name__.startswith(prefix) if check_prefix else True + except: + # GH#287: safe fallback + return False diff --git a/tests/cases/issues/test_py35_issue_287.py b/tests/cases/issues/test_py35_issue_287.py new file mode 100644 index 00000000..5e9f2155 --- /dev/null +++ b/tests/cases/issues/test_py35_issue_287.py @@ -0,0 +1,24 @@ +from typing import Tuple + +import pytest_cases + + +class Cases: + ClassVar1 = int # OK + ClassVar2 = int # OK + ClassVar3 = 1 # OK + ClassVar4 = float # OK + + ClassVar5 = Tuple[int] # FAILS with AttributeError: __name__ + # ClassVar6 = Tuple[float] # FAILS with AttributeError: __name__ + # ClassVar7 = List[int] # FAILS with AttributeError: __name__ + # ClassVar8 = Any # FAILS with AttributeError: __name__ + # ClassVar9 = Dict[int, str] # FAILS with AttributeError: __name__ + + def case_b(self): + return 1 + + +@pytest_cases.parametrize_with_cases("case", Cases) +def test_something(case) -> None: + pass