realpython / pytest-mypy

Mypy static type checker plugin for Pytest
https://pypi.org/project/pytest-mypy/
MIT License
249 stars 32 forks source link

Improve pytest failure output for mypy #99

Open GergelyKalmar opened 4 years ago

GergelyKalmar commented 4 years ago

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?

dmtucker commented 4 years ago

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.

GergelyKalmar commented 4 years ago

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):

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!