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

make KeyboardInterrupt cancel pytest #10279

Closed
wants to merge 2 commits into from
Closed

Conversation

arvidn
Copy link
Contributor

@arvidn arvidn commented Feb 17, 2022

No description provided.

Comment on lines +7 to +9
from _pytest.runner import CallInfo
from _pytest.runner import CollectReport
from _pytest.runner import TestReport
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like these may all be available in the public API instead of _pytest. pytest-dev/pytest#7469

Perhaps not applicable after checking above, but I expect isort might prefer these to be all together? If not we may want to adjust a setting for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in the version of pytest I have they are not exposed under pytest. That was my first attempt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't pay attention whether pre-commit ran isort for me or not. But it should have, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find I frequently have to explicitly run pre-commit run --all to have checks be run..

Copy link
Contributor

Choose a reason for hiding this comment

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

Python 3.9.5 (default, Jun  3 2021, 15:18:23) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
>>> pytest.__version__
'7.0.1'
>>> from pytest import CallInfo, CollectReport, TestReport
>>>

If you have latest pytest and it isn't working for you I would be curious to know. We could dig into that on keybase.

Suggested change
from _pytest.runner import CallInfo
from _pytest.runner import CollectReport
from _pytest.runner import TestReport
from pytest import CallInfo, CollectReport, TestReport

On the pre-commit, it's default is to only run on 'modified' files. I'm not sure the definition of that. I personally just manually run it with --all.

call: CallInfo[Any],
report: Union[CollectReport, TestReport],
) -> None:
pytest.exit("Cancelled by exception")
Copy link
Contributor

Choose a reason for hiding this comment

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

https://docs.pytest.org/en/6.2.x/reference.html#pytest.hookspec.pytest_exception_interact

It sounds like this would terminate tests early on an uncaught exception in a test? This is really required as opposed to just the sys.exit() removal above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the sys.exit() code that I removed did not contribute meaningfully to anything. That exception was also caught by pytest, just like KeyboardInterrupt is. This hook is called later, for tests (or collections) that resulted in an exception. pytest.exit() throws a special exception that pytest recognizes means it should shut down.

Are you suggesting I make this conditional on KeyboardInterrupt specifically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually, I think this is only called for "interactive" exceptions. This is what the docs say.

Called when an exception was raised which can potentially be interactively handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recall pytest itself having an issue with terminating on ctrl+c. Rather, I had just assumed that the issue was that we were consuming the KeyboardInterrupt ourselves. We had at least one place that we just claimed a signal handler without any cooperative considerations when starting a service. That was changed recently in #10163 with a Windows regression addressed in #10263. Maybe there's something with pytest-asyncio related to this so I'll look there to get a bit more confidence I understand what is going on.

I saw that phrase you quoted about interactively handleable errors. I have no idea what it means. I'll ask around and see what I can figure out.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2022

This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed by the relevant parties.

@github-actions github-actions bot added the stale-pr Flagged as stale and in need of manual review label Apr 4, 2022
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the merge_conflict Branch has conflicts that prevent merge to main label Apr 30, 2022
@arvidn arvidn closed this May 24, 2022
@arvidn arvidn deleted the make-test-cancellable branch May 24, 2022 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge_conflict Branch has conflicts that prevent merge to main stale-pr Flagged as stale and in need of manual review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants