Skip to content

python3: tests collecting fails with TypeError: object of type 'map' has no len() #3325

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

Closed
lpramuk opened this issue Mar 21, 2018 · 10 comments
Closed
Labels
type: bug problem that needs to be addressed

Comments

@lpramuk
Copy link

lpramuk commented Mar 21, 2018

$ py.test --collect-only -v tests/foreman/cli/test_setting.py
================================================= test session starts ==================================================
platform linux -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0 -- /home/jenkins/shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/bin/python3.6
cachedir: .pytest_cache
shared_function enabled - OFF - scope:  - storage: file
rootdir: /home/jenkins/workspace/automation-6.3-tier1-rhel7, inifile:
plugins: xdist-1.22.2, services-1.2.1, mock-1.6.3, forked-0.2
collected 0 items / 1 errors                                                                                           
2018-03-21 05:59:18 - conftest - DEBUG - Collected 0 test cases


======================================================== ERRORS ========================================================
__________________________________ ERROR collecting tests/foreman/cli/test_setting.py __________________________________
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/_pytest/python.py:200: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/_pytest/python.py:378: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/_pytest/python.py:125: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
../../shiningpanda/jobs/375dbdea/virtualenvs/d41d8cd9/lib/python3.6/site-packages/_pytest/python.py:824: in parametrize
    if len(ids) != len(parameters):
E   TypeError: object of type 'map' has no len()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================== 0 tests deselected ==================================================
=============================================== 1 error in 0.68 seconds ================================================

There is one test that cause collector to fail on python3:

@pytest.mark.parametrize('value', **xdist_adapter(invalid_boolean_strings()))
def test_negative_update_send_welcome_email(value):

Collector is not python3 ready as it fails when collecting tests with the following mark @pytest.mark.parametrize()

@lpramuk
Copy link
Author

lpramuk commented Mar 21, 2018

In python3 you can't do:

len(map(lambda x: x, [1,3,5,7]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'map' has no len()

the fix is to convert map into a list:

>>> len(list(map(lambda x: x, [1,3,5,7])))
4

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Mar 21, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #638 (python.py:813 (in "parametrize") raises "TypeError: object of type 'int' has no len()" in some cases), #651 (test fails in ntpath.py when LocalPath has no len), #747 (flakes test fail), #1777 (sigalrm fails test), and #239 (not collect deselected tests).

@pytestbot pytestbot added type: bug problem that needs to be addressed and removed type: question general question, might be closed after 2 weeks of inactivity labels Mar 21, 2018
@The-Compiler
Copy link
Member

I don't think passing anything other than a sequence to parametrize is supported by pytest.

@lpramuk
Copy link
Author

lpramuk commented Mar 21, 2018

@lpramuk
Copy link
Author

lpramuk commented Mar 21, 2018

RE passing anything other than a sequence to parametrize

Why then with python2.7 there is no problem to collect such tests?

@The-Compiler
Copy link
Member

Trying to "fix" this in pytest will probably have wider implications - you should really just use the pytest API like documented, which means passing a list (or at least some kind of sequence) to pytest.

Why then with python2.7 there is no problem to collect such tests?

Because map returns a list in Python 2.

@lpramuk
Copy link
Author

lpramuk commented Mar 21, 2018

Ok we can fix it then probably ourselves by changing map to list in our code. Yay

@RonnyPfannschmidt
Copy link
Member

note that both points in pytest would fix it wrong and break on marker reuse and/or double call

@RonnyPfannschmidt
Copy link
Member

but we should probably assert that we get an actual sequence there,

@lpramuk
Copy link
Author

lpramuk commented Mar 21, 2018

Thanks guys I'm happy with closing this issue now

@lpramuk lpramuk closed this as completed Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

4 participants