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

TypeError when passing a single function to a marker #11519

Closed
4 tasks done
okken opened this issue Oct 18, 2023 · 2 comments
Closed
4 tasks done

TypeError when passing a single function to a marker #11519

okken opened this issue Oct 18, 2023 · 2 comments

Comments

@okken
Copy link
Contributor

okken commented Oct 18, 2023

  • a detailed description of the bug or problem you are having

I'd like to be able to pass a single function to a marker, for use in a fixture later.
I can pass any number of non-functions, like integers.
I can pass two functions, and presumably more.
But one function results in "TypeError: foo() takes 0 positional arguments but 1 was given"

  • output of pip list from the virtual environment you are using
$ pip list
Package    Version
---------- -------
iniconfig  2.0.0
packaging  23.2
pip        23.3
pluggy     1.3.0
pytest     7.4.2
setuptools 65.5.0
  • pytest and operating system versions
    Python 3.11, pytest 7.4.2, MacOS Ventura 13.0.1

  • minimal example if possible

pytest.ini

[pytest]
markers =
    no_param: no parameters
    one_param: one parameter
    two_params: two parameters

test_marker.py

import pytest

@pytest.mark.no_param
def test_no_arg():
    ...

@pytest.mark.one_param(1)
def test_one_int():
    ...

@pytest.mark.two_params(1, 2)
def test_two_ints():
    ...

def foo():
    ...

def bar():
    ...

# doesn't work, throws an exception
#
# @pytest.mark.one_param(foo)
# def test_one_func():
#     ...
#

@pytest.mark.two_params(foo, bar)
def test_two_funcs():
    ...

test_marker_one_func_param.py

import pytest

def foo():
    ...

def bar():
    ...

@pytest.mark.one_param(foo)
def test_one_func():
    ...

0-2 integers, and 2 functions work fine

$ pytest test_marker.py
============================================ test session starts ============================================
platform darwin -- Python 3.11.6, pytest-7.4.2, pluggy-1.3.0
rootdir: /Users/okken/projects/pytest-param-scope/marker_param
configfile: pytest.ini
collected 4 items                                                                                           

test_marker.py ....                                                                                   [100%]

============================================= 4 passed in 0.01s =============================================

but one func blows up

$ pytest test_marker_one_func_param.py           
============================================ test session starts ============================================
platform darwin -- Python 3.11.6, pytest-7.4.2, pluggy-1.3.0
rootdir: /Users/okken/projects/pytest-param-scope/marker_param
configfile: pytest.ini
collected 0 items / 1 error                                                                                 

================================================== ERRORS ===================================================
______________________________ ERROR collecting test_marker_one_func_param.py _______________________________
../venv/lib/python3.11/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
../venv/lib/python3.11/site-packages/_pytest/runner.py:372: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
../venv/lib/python3.11/site-packages/_pytest/python.py:531: in collect
    self._inject_setup_module_fixture()
../venv/lib/python3.11/site-packages/_pytest/python.py:545: in _inject_setup_module_fixture
    self.obj, ("setUpModule", "setup_module")
../venv/lib/python3.11/site-packages/_pytest/python.py:310: in obj
    self._obj = obj = self._getobj()
../venv/lib/python3.11/site-packages/_pytest/python.py:528: in _getobj
    return self._importtestmodule()
../venv/lib/python3.11/site-packages/_pytest/python.py:617: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
../venv/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
../venv/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:178: in exec_module
    exec(co, module.__dict__)
test_marker_one_func_param.py:11: in <module>
    @pytest.mark.one_param(foo)
E   TypeError: foo() takes 0 positional arguments but 1 was given
========================================== short test summary info ==========================================
ERROR test_marker_one_func_param.py - TypeError: foo() takes 0 positional arguments but 1 was given
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================= 1 error in 0.20s ==============================================
@The-Compiler
Copy link
Member

Duplicate of #8499

@The-Compiler The-Compiler marked this as a duplicate of #8499 Oct 18, 2023
@The-Compiler The-Compiler closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2023
@okken
Copy link
Contributor Author

okken commented Oct 18, 2023

Thanks for the quick reply @The-Compiler .

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

2 participants