Skip to content

Collection error when HTTPError passed to parametrize #10184

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
jaraco opened this issue Aug 4, 2022 · 3 comments
Closed

Collection error when HTTPError passed to parametrize #10184

jaraco opened this issue Aug 4, 2022 · 3 comments

Comments

@jaraco
Copy link
Contributor

jaraco commented Aug 4, 2022

Using the latest version of pytest against this minimal reproducer produces an error in collection:

 draft $ cat test.py
from urllib.request import HTTPError

import pytest


@pytest.mark.parametrize(
    'error', [HTTPError('url', 400, 'httperror', {}, None)])
def test_something(error):
    pass
 draft $ pip-run pytest -- -m pytest test.py
Collecting pytest
  Using cached pytest-7.1.2-py3-none-any.whl (297 kB)
Collecting pluggy<2.0,>=0.12
  Using cached pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting py>=1.8.2
  Using cached py-1.11.0-py2.py3-none-any.whl (98 kB)
Collecting tomli>=1.0.0
  Using cached tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting iniconfig
  Using cached iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)
Collecting attrs>=19.2.0
  Using cached attrs-22.1.0-py2.py3-none-any.whl (58 kB)
Collecting packaging
  Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
  Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB)
Installing collected packages: iniconfig, tomli, pyparsing, py, pluggy, attrs, packaging, pytest
Successfully installed attrs-22.1.0 iniconfig-1.1.1 packaging-21.3 pluggy-1.0.0 py-1.11.0 pyparsing-3.0.9 pytest-7.1.2 tomli-2.0.1
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.10.5, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/jaraco/draft
collected 0 items / 1 error                                                                                                                                                       

===================================================================================== ERRORS ======================================================================================
____________________________________________________________________________ ERROR collecting test.py _____________________________________________________________________________
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/pluggy/_hooks.py:265: in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/pluggy/_manager.py:80: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:262: in pytest_pycollect_makeitem
    return list(collector._genfunctions(name, obj))
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:489: in _genfunctions
    self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/pluggy/_hooks.py:292: in call_extra
    return self(**kwargs)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/pluggy/_hooks.py:265: in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/pluggy/_manager.py:80: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:148: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:1304: in parametrize
    ids = self._resolve_parameter_set_ids(
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:1374: in _resolve_parameter_set_ids
    return id_maker.make_unique_parameterset_ids()
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:966: in make_unique_parameterset_ids
    resolved_ids = list(self._resolve_ids())
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:991: in _resolve_ids
    yield "-".join(
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:992: in <genexpr>
    self._idval(val, argname, idx)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:1004: in _idval
    idval = self._idval_from_value(val)
/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-7rkih960/_pytest/python.py:1051: in _idval_from_value
    elif isinstance(getattr(val, "__name__", None), str):
/opt/homebrew/Cellar/[email protected]/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tempfile.py:477: in __getattr__
    file = self.__dict__['file']
E   KeyError: 'file'
============================================================================= short test summary info =============================================================================
ERROR test.py - KeyError: 'file'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================ 1 error in 0.11s =================================================================================

The same error can be elicited by trying to get the __name__ property of an HTTPError:

>>> import urllib.request
>>> urllib.request.HTTPError().__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 5 required positional arguments: 'url', 'code', 'msg', 'hdrs', and 'fp'
>>> urllib.request.HTTPError('url', 400, 'httperror', {}, None).__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tempfile.py", line 472, in __getattr__
    file = self.__dict__['file']
KeyError: 'file'

I'm not sure if there's anything that can be done about this situation. In the code where I encountered it, I've decided to pass the parameter as a string and then eval it inside the test, a workaround to be sure.

@RonnyPfannschmidt
Copy link
Member

I believe pytest. param(...,id=...)can be used to mitigate the bug in the stdlib by setting explicit ids

@jaraco
Copy link
Contributor Author

jaraco commented Aug 4, 2022

Very nice. Yes, that's a suitable workaround. Much better than the string eval workaround I was using.

@jaraco
Copy link
Contributor Author

jaraco commented Aug 4, 2022

I'll close this issue, but I welcome maintainers to re-open if they deem appropriate.

@jaraco jaraco closed this as completed Aug 4, 2022
clrpackages pushed a commit to clearlinux-pkgs/pypi-setuptools that referenced this issue Aug 12, 2022
…version 63.4.3

Anderson Bravalheri (1):
      Update docs/userguide/dependency_management.rst

Jason R. Coombs (86):
      Convert test_cmd to pytest
      Convert BasePyPIRCCommandTestCase to pytest
      Convert RegisterTestCase to pytest
      ⚫ Fade to black.
      Use jaraco.path to generate a tree.
      Port sdist tests to pytest
      Convert TestUpload to pytest
      Convert TestUpload to parametrized test. Remove dependence on unittest for PyPIRC tests.
      Implement HTTP 400 error as a pytest.param to avoid collection error. Ref pytest-dev/pytest#10184.
      Convert core tests to pytest
      ⚫ Fade to black.
      Convert TestVersion to pytest.
      Convert TestConfig to pytest
      Convert TestCygwinCCompiler to pytest
      Convert TestDirUtil to pytest
      Convert TestFileUtil to pytest
      Convert TestSysconfig to pytest
      ⚫ Fade to black.
      👹 Feed the hobgoblins (delint).
      Remove patching of uname.
      Convert TestUtil to pytest
      Convert TestUnixCCompiler to pytest
      Prefer pytest for skip
      Convert more tests to pytest
      Prefer pytest for skip
      Prefer pytest for skip
      Convert TestBuild to pytest
      Prefer pytest for skip
      Prefer pytest for skip
      Remove unreachable code
      Copy xxmodule.c from Python 3.11 and 3.8, restoring tests for build_ext.
      Exclude Python 3.11 on macOS due to lack of wheels. Ref pypa/distutils#165.
      Mark test as xfail for now. Ref pypa/distutils#166.
      Use pathlib to read the text
      ⚫ Fade to black.
      Convert TestInstall to pytest
      Only xfail on Windows
      Allow overriding toxworkdir with an env var.
      Ensure sys.version is restored in test_cygwinccompiler. Fixes #166.
      Run test_xx in process, utilizing import_helper
      Redirect extension module to a directory that's not deleted on Windows.
      Include cygwin
      Prefer pytest in test_build_py
      Prefer pytest in test_check
      Refactor imports around docutils.
      Replace addCleanup with monkeypatch.
      Enable tests requiring docutils.
      Fix broken tests around docutils.
      Convert PendingDeprecationWarnings to DeprecationWarnings.
      Ignore unactionable warnings in docutils.
      Prefer pytest for skip
      Prefer pytest for skip
      Prefer pytest for skip
      Prefer pytest for skip
      Prefer pytest for skip
      Convert TestLog to pytest.
      Prefer pytest for skip
      Consolidate tests
      Prefer pytest for skip
      Prefer pytest for skip
      Convert unconditional skip to conditional skip. Mark test as xfail because it's failing.
      Prefer pytest for skip
      ⚫ Fade to black.
      Convert unix_compat to pytest skips
      Consistently import unittest.mock.
      Prefer pytest for skip
      Remove unnecessary comment.
      ⚫ Fade to black.
      👹 Feed the hobgoblins (delint).
      Prefer tabs
      Add pytest-flake8 and pytest-black and pytest-cov to test lint and style and coverage
      👹 Feed the hobgoblins (delint).
      Add test capturing failed expectation.
      Add compatibility for Python 3.7
      Windows is sensitive even on Python 3.10
      Also test library dirs
      Extract fixture for c_file
      Generate a C file that imports Python.h and something platform specific.
      Ensure Python include directory is configured.
      Extend the test to compile a second time after setting include dirs again.
      ⚫ Fade to black.
      Allow compiler classes to supply include and library dirs at the class level.
      Disallow repeat calls to .initialize in one place.
      Update changelog
      Revert "Disallow repeat calls to .initialize in one place."
      Bump version: 63.4.2 → 63.4.3

Steve Dower (4):
      Ensure Windows SDK directories are not cleared when caller specifies include/library dirs
      Remove stray colon
      Fixup bad super() call
      Use CCompiler._fix_compile_args to fix args to compile()

nxnjz (1):
      typo fix

smheidrich (1):
      Add docs section on direct URL dependencies
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