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

Use a hack to make typing of pytest.fail.Exception & co work #6758

Merged
merged 1 commit into from
Feb 19, 2020

Conversation

bluetech
Copy link
Member

Mypy currently is unable to handle assigning attributes on function: python/mypy#2087.
pytest uses this for the outcome exceptions -- pytest.fail.Exception, pytest.exit.Exception etc, and this is the canonical name by which they are referred.

Initially we started working around this with type: ignores, and later by switching e.g. pytest.fail.Exception with the direct exception Failed. But this causes a lot of churn and is not as nice. And I also found that some code relies on it, in skipping.py:

    def pytest_configure(config):
        if config.option.runxfail:
            # yay a hack
            import pytest

            old = pytest.xfail
            config._cleanup.append(lambda: setattr(pytest, "xfail", old))

            def nop(*args, **kwargs):
                pass

            nop.Exception = xfail.Exception
            setattr(pytest, "xfail", nop)
        ...

So it seems better to support it. Use a hack to make it work. The rest of the commit rolls back all of the workarounds we added up to now.

pytest.raises.Exception also exists, but it's not used much so I kept it as-is for now.

Hopefully in the future mypy supports this and this ugliness can be removed.

@bluetech bluetech force-pushed the outcome-exception-callable-2 branch 2 times, most recently from d2b3e96 to 06f8e0a Compare February 18, 2020 20:53
Mypy currently is unable to handle assigning attributes on function:
python/mypy#2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.

Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:

    def pytest_configure(config):
        if config.option.runxfail:
            # yay a hack
            import pytest

            old = pytest.xfail
            config._cleanup.append(lambda: setattr(pytest, "xfail", old))

            def nop(*args, **kwargs):
                pass

            nop.Exception = xfail.Exception
            setattr(pytest, "xfail", nop)
        ...

So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.

`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.

Hopefully in the future mypy supports this and this ugliness can be
removed.
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lovely path forward by containing the problem with a abstraction that will eventually be removable

@bluetech bluetech merged commit af2b0e1 into pytest-dev:master Feb 19, 2020
@bluetech
Copy link
Member Author

Thanks @RonnyPfannschmidt.

I merged even though Travis failed, it seems unrelated.

@bluetech bluetech deleted the outcome-exception-callable-2 branch February 28, 2020 12:08
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

Successfully merging this pull request may close these issues.

2 participants