pytest-mypy icon indicating copy to clipboard operation
pytest-mypy copied to clipboard

Improve pytest failure output for mypy

Open GergelyKalmar opened this issue 5 years ago • 2 comments
trafficstars

It seems that the failures for pylint have [pylint] prepended to the filename in the failure output:

image

It would be great to have [mypy] prepended to the filenames of the errors that belong to mypy (the second list of errors on the picture above). Or is this something that can be somehow configured in mypy and/or somewhere in pytest?

GergelyKalmar avatar Aug 18 '20 13:08 GergelyKalmar

This should work in a conftest.py:

def custom_file_reportinfo(file_item):
    return (
        file_item.fspath,
        None,
        '[mypy] ' + file_item.config.invocation_dir.bestrelpath(
            file_item.fspath,
        ),
    )


def pytest_configure(config):
    plugin = config.pluginmanager.getplugin("mypy")
    plugin.MypyFileItem.reportinfo = custom_file_reportinfo

Let's leave this open until a test for that is added (to ensure continued support) or a better API is proposed.

dmtucker avatar Aug 18 '20 15:08 dmtucker

This indeed does work well, thank you! Could we consider to make this the default option? I've found it to be quite useful to see which plugin a given failure belongs to.

Other plugins I use seem to do this by default (somewhat):

  • pytest-pylint does it as shown above (https://github.com/carsongee/pytest-pylint/blob/master/pytest_pylint/plugin.py#L378)
  • pytest-flake8 does show the plugin name in the reportinfo (https://github.com/tholo/pytest-flake8/blob/master/pytest_flake8.py#L146), although the format is a bit different
  • pytest-isort uses the plugin name only (https://github.com/moccu/pytest-isort/blob/master/pytest_isort.py#L184), but I opened an issue there too to make this a bit nicer (https://github.com/moccu/pytest-isort/issues/26)

Of course, as an alternative I'll just patch each of them with an improved version of your custom reportinfo function, hehe:

def reportinfo_unifier(prefix):
    def reportinfo(file_item):
        return (
            file_item.fspath,
            None,
            prefix + ' ' + file_item.config.invocation_dir.bestrelpath(file_item.fspath),
        )
    return reportinfo


def pytest_configure(config):
    plugin = config.pluginmanager.getplugin
    plugin('mypy').MypyFileItem.reportinfo = reportinfo_unifier(prefix='[mypy]')
    plugin('isort').IsortItem.reportinfo = reportinfo_unifier(prefix='[isort]')

Dynamic languages are such fun!

GergelyKalmar avatar Aug 18 '20 16:08 GergelyKalmar