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

Plugin function not recognized by MyPy #10691

Closed
4 tasks done
joooeey opened this issue Jan 24, 2023 · 2 comments
Closed
4 tasks done

Plugin function not recognized by MyPy #10691

joooeey opened this issue Jan 24, 2023 · 2 comments

Comments

@joooeey
Copy link

joooeey commented Jan 24, 2023

  • a detailed description of the bug or problem you are having
  • pytest and operating system versions
  • output of pip list from the virtual environment you are using
  • minimal example if possible

I have a simple plugin to shorten the boilerplate for not-implemented parameters from

pytest.param(42, marks=pytest.mark.xfail(reason=NotImplemented))

to

pytest.not_implemented(42)

My conftest.py looks like this:

import pytest

def not_implemented(*args, **kwargs):
    """X-fail not-implemented parameter."""
    return pytest.param(
        *args, marks=pytest.mark.xfail(reason=NotImplemented), **kwargs
    )

def pytest_configure() -> None:
    pytest.not_implemented = not_implemented

However, MyPy produces the error: error: 'object' not callable on every line where I use pytest.not_implemented.

Is there a way to work around this that does not require multiple ugly comments like in the following code snippet everywhere in my parametrized tests?

@pytest.mark.parametrize("value", [
    1,
    pytest.not_implemented(2),  # type: ignore
    pytest.not_implemented(3),  # type: ignore
    pytest.not_implemented(4),  # type: ignore
    5,
    pytest.not_implemented(6),  # type: ignore
    pytest.not_implemented(7),  # type: ignore
def test_something(value):
    ...

Can the underlying issue be fixed in Pytest?

The same issue has been reported in pytest-lazy-fixture on PyPI: TvoroG/pytest-lazy-fixture#51 . The OP there has determined that they don't get the problem in Pytest 5.4.3 or earlier.

Versions

This is on Windows 10, MyPy 0.981 and Pytest 7.1.2

packages in conda environment
Package                       Version
----------------------------- -----------
alabaster                     0.7.12
arrow                         1.2.3
astroid                       2.12.13
atomicwrites                  1.4.0
attrs                         22.2.0
autopep8                      1.6.0
Babel                         2.11.0
backcall                      0.2.0
bcrypt                        3.2.2
beautifulsoup4                4.11.1
binaryornot                   0.4.4
black                         22.10.0
bleach                        5.0.1
Bottleneck                    1.3.5
branca                        0.6.0
brotlipy                      0.7.0
certifi                       2022.12.7
cffi                          1.15.1
chardet                       5.1.0
charset-normalizer            2.1.1
click                         8.1.3
click-plugins                 1.1.1
cligj                         0.7.2
cloudpickle                   2.0.0
colorama                      0.4.6
contourpy                     1.0.6
cookiecutter                  2.1.1
cryptography                  39.0.0
cycler                        0.11.0
debugpy                       1.5.1
decorator                     5.1.1
defusedxml                    0.7.1
diff-match-patch              20200713
dill                          0.3.6
docutils                      0.18.1
entrypoints                   0.4
fastjsonschema                2.16.2
Fiona                         1.8.22
flake8                        4.0.1
folium                        0.14.0
fonttools                     4.38.0
GDAL                          3.6.1
geopandas                     0.12.2
idna                          3.4
imagesize                     1.4.1
importlib-metadata            6.0.0
importlib-resources           5.10.2
inflection                    0.5.1
iniconfig                     1.1.1
intervaltree                  3.0.2
ipykernel                     6.15.2
ipython                       7.31.1
ipython-genutils              0.2.0
isort                         5.11.4
jaraco.classes                3.2.3
jedi                          0.18.1
jellyfish                     0.9.0
Jinja2                        3.1.2
jinja2-time                   0.2.0
joblib                        1.2.0
jsonschema                    4.17.3
jupyter_client                7.4.8
jupyter_core                  5.1.1
jupyterlab-pygments           0.2.2
keyring                       23.13.1
kiwisolver                    1.4.4
lazy-object-proxy             1.8.0
lxml                          4.9.2
mapclassify                   2.4.3
MarkupSafe                    2.1.1
matplotlib                    3.6.2
matplotlib-inline             0.1.6
mccabe                        0.6.1
mistune                       2.0.4
mkl-fft                       1.3.1
mkl-random                    1.2.2
mkl-service                   2.4.0
more-itertools                9.0.0
munch                         2.5.0
munkres                       1.1.4
mypy                          0.981
mypy-extensions               0.4.3
nbclient                      0.7.2
nbconvert                     7.2.7
nbformat                      5.7.1
nest-asyncio                  1.5.6
networkx                      2.8.8
numexpr                       2.8.4
numpy                         1.23.5
numpydoc                      1.5.0
packaging                     22.0
pandas                        1.5.2
pandocfilters                 1.5.0
paramiko                      2.12.0
parso                         0.8.3
pathspec                      0.10.3
pexpect                       4.8.0
pickleshare                   0.7.5
Pillow                        9.4.0
pip                           22.3.1
pkgutil_resolve_name          1.3.10
platformdirs                  2.5.2
pluggy                        1.0.0
ply                           3.11
prompt-toolkit                3.0.36
psutil                        5.9.0
ptyprocess                    0.7.0
py                            1.11.0
pycodestyle                   2.8.0
pycparser                     2.21
pydocstyle                    6.1.1
pyflakes                      2.4.0
Pygments                      2.11.2
pylint                        2.15.9
pyls-spyder                   0.4.0
PyNaCl                        1.5.0
pyOpenSSL                     23.0.0
pyparsing                     3.0.9
pyproj                        3.4.1
PyQt5                         5.15.7
PyQt5-sip                     12.11.0
PyQtWebEngine                 5.15.4
pyrsistent                    0.19.3
PySocks                       1.7.1
pytest                        7.1.2
python-dateutil               2.8.2
python-lsp-black              1.2.1
python-lsp-jsonrpc            1.0.0
python-lsp-server             1.5.0
python-slugify                7.0.0
pytoolconfig                  1.2.4
pytz                          2022.7
pywin32                       305.1
pywin32-ctypes                0.2.0
PyYAML                        6.0
pyzmq                         23.2.0
QDarkStyle                    3.0.3
qstylizer                     0.2.2
QtAwesome                     1.2.2
qtconsole                     5.3.2
QtPy                          2.3.0
requests                      2.28.1
rope                          1.6.0
Rtree                         1.0.1
scikit-learn                  1.1.3
scipy                         1.9.3
setuptools                    65.6.3
shapely                       2.0.0
sip                           6.7.5
six                           1.16.0
snowballstemmer               2.2.0
sortedcontainers              2.4.0
soupsieve                     2.3.2.post1
Sphinx                        5.0.2
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
spyder                        5.3.3
spyder-kernels                2.3.3
text-unidecode                1.3
textdistance                  4.5.0
threadpoolctl                 3.1.0
three-merge                   0.1.1
tinycss2                      1.2.1
toml                          0.10.2
tomli                         2.0.1
tomlkit                       0.11.6
tornado                       6.2
traitlets                     5.7.1
typing_extensions             4.4.0
ujson                         5.5.0
unicodedata2                  15.0.0
Unidecode                     1.3.6
urllib3                       1.26.13
watchdog                      2.2.1
wcwidth                       0.2.5
webencodings                  0.5.1
whatthepatch                  1.0.3
wheel                         0.38.4
win-inet-pton                 1.1.0
wrapt                         1.14.1
xyzservices                   2022.9.0
yapf                          0.32.0
zipp                          3.11.0
@RonnyPfannschmidt
Copy link
Member

It's considered invalid to monkeypatch the pytest module

As such it's absolutely on you and nothing pytest has to fix

@RonnyPfannschmidt RonnyPfannschmidt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2023
@nicoddemus
Copy link
Member

nicoddemus commented Jan 24, 2023

@joooeey I suggest to not add to the namespace, but publish it as a public API:

from myplugin import not_implemented

@pytest.mark.parametrize("value", [
    1,
    not_implemented(2),
    not_implemented(3),
    not_implemented(4),
    5,
    ...
]
)
def test_something(value):
    ...

We no longer recommend to "install" things into the pytest namespace. 👍

One of the reasons is static type checkers.

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

3 participants