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

Update assert.rst #12742

Closed
wants to merge 1 commit into from
Closed

Update assert.rst #12742

wants to merge 1 commit into from

Conversation

alanmehio
Copy link

pytest 8.3.2
Python 3.12.1
os : Window 10 Enterprise

pytest 8.3.2
Python 3.12.1
os : Window 10 Enterprise
@alanmehio
Copy link
Author

Should be verified on Linux also . Test fails on Window 10 but has not been verified on Linux

@@ -112,7 +112,7 @@ If you want to check if a block of code is raising an exact exception type, you
foo()
assert excinfo.type is RuntimeError

The :func:`pytest.raises` call will succeed, even though the function raises :class:`NotImplementedError`, because
The :func:`pytest.raises` call will fail, even though the function raises :class:`NotImplementedError`, eventhough
Copy link
Member

@graingert graingert Aug 27, 2024

Choose a reason for hiding this comment

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

"Eventhough" is not a valid word in English

And it's incorrect to say the pytest.raises call fails

@The-Compiler
Copy link
Member

I'm afraid I can't make sense of this at all. Those changes both are unneeded and in fact incorrect, but I'm confused as your description/comment ("Should be verified on Linux also . Test fails on Window 10 but has not been verified on Linux") seems to be entirely unrelated to what you're changing (a simple doc update, without any code involved). Can you elaborate?

@alanmehio
Copy link
Author

alanmehio commented Aug 27, 2024

Yes I can. The test case fails as it gives the below error when you run it on window 10 see below console output

pytest test_foo_not_implemented.py
================================================= test session starts =================================================
platform win32 -- Python 3.12.1, pytest-8.3.2, pluggy-1.5.0
rootdir: .... \tox-proj\tests
collected 1 item

test_foo_not_implemented.py F [100%]

====================================================== FAILURES =======================================================
______________________________________________ test_foo_not_implemented _______________________________________________

def test_foo_not_implemented():
    def foo():
        raise NotImplementedError

    with pytest.raises(RuntimeError) as excinfo:
        foo()
  assert excinfo.type is RuntimeError

E AssertionError: assert <class 'NotImplementedError'> is RuntimeError
E + where <class 'NotImplementedError'> = <ExceptionInfo NotImplementedError() tblen=2>.type

test_foo_not_implemented.py:8: AssertionError
=============================================== short test summary info ===============================================
FAILED test_foo_not_implemented.py::test_foo_not_implemented - AssertionError: assert <class 'NotImplementedError'> is RuntimeError
================================================== 1 failed in 0.22s ==================================================

The "test_foo_not_implemented.py" is as below which is copied from the documentation:

import pytest 
def test_foo_not_implemented():
    def foo():
        raise NotImplementedError

    with pytest.raises(RuntimeError) as excinfo:
        foo()
    assert excinfo.type is RuntimeError

@graingert
Copy link
Member

Ah the example code is incorrect, but the wording is correct

@graingert
Copy link
Member

Line 113 should be assert excinfo.type is NotImplementedError

@The-Compiler
Copy link
Member

No, both the text and the code are correct. The whole point of that example is that this fails, as explained right there...

NotImplementedError is a subclass of RuntimeError; however the following assert statement will
catch the problem.

@graingert
Copy link
Member

Ah yes indeed

@alanmehio
Copy link
Author

The code should be changed from . Otherwise the assertion statement fails. Please reconsider the code change
assert excinfo.type is RuntimeError
to
assert issubclass(excinfo.type, RuntimeError)

@The-Compiler
Copy link
Member

Yes, it fails. That's the whole point, as explained in the text around it.

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.

3 participants