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

[MRG][feature] Change warns signature to mimic the raises call #2711

Merged

Conversation

massich
Copy link
Contributor

@massich massich commented Aug 22, 2017

fixes #2708 providing warns with message and match parameters (similarly to pytest.raises. see #2227)

@massich massich changed the title [feature] Change warns signature to mimic the raises call [WIP][feature] Change warns signature to mimic the raises call Aug 22, 2017
@massich
Copy link
Contributor Author

massich commented Aug 22, 2017

@nicoddemus Before diving into the code, I would like to get some feedback with the functionality itself. Could you check the tests I've written and ping to the appropriated developers to provide feedback before writing the actual feature. Maybe you guys have different view of the problem (like what you pointed in #2708)


def test_message_using(self):
source = "warnings.warn('my runtime warning', RuntimeWarning)"
pytest.warns(RuntimeWarning, source, message='my runtime warning')
Copy link
Member

Choose a reason for hiding this comment

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

warns should not replicate the old call style , and even more so for the execute code snippets style, its just an accident of history we should stay clear of

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so, warnings should always be used within a context manager?

Copy link
Contributor Author

@massich massich Aug 22, 2017

Choose a reason for hiding this comment

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

I've changed test_message_using in favor of test_message. (The using was a copy-paste error)

@massich massich force-pushed the mimic_raises_signature_in_warns branch from 44e4e2a to 705c214 Compare August 22, 2017 12:23
@massich massich changed the title [WIP][feature] Change warns signature to mimic the raises call [RFC][feature] Change warns signature to mimic the raises call Aug 23, 2017
@nicoddemus
Copy link
Member

Hi @massich sorry for not responding earlier, this one fell through the cracks.

The tests look good to me. The question of whatever we should match a single warning or error out vs match any of the warnings, I think the second is more practical so I would go for it.

I definitely agree we should support only the context manager case and treat function-form and (specially) string-eval forms as history.

cc @flub @hpk42 @The-Compiler would you guys like to share your opinions here?

@nicoddemus nicoddemus added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Aug 31, 2017
@massich massich force-pushed the mimic_raises_signature_in_warns branch from 705c214 to 47e17cb Compare September 4, 2017 13:23
@massich massich force-pushed the mimic_raises_signature_in_warns branch from 47e17cb to d8ecca5 Compare September 4, 2017 13:26
@The-Compiler
Copy link
Member

I don't really have an opinion on this, FWIW - I treat all warnings as errors 😉

@massich massich changed the title [RFC][feature] Change warns signature to mimic the raises call [WIP][feature] Change warns signature to mimic the raises call Sep 5, 2017
@massich
Copy link
Contributor Author

massich commented Sep 5, 2017

I got test_match_regex working and I broke some other test, but these could be adressed afterwards. Could you give me some feedback regarding the functionality implementation. cc:@nicoddemus

@flub
Copy link
Member

flub commented Sep 6, 2017

Hi, tests seem fine but I do t see any for the message kwarg. Should that not have been included according to the description?

@massich
Copy link
Contributor Author

massich commented Sep 6, 2017

@flub here warns is called with match=r'must be \d+$'

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 92.063% when pulling 8a77dfa on massich:mimic_raises_signature_in_warns into 9d373d8 on pytest-dev:features.

@massich massich changed the title [WIP][feature] Change warns signature to mimic the raises call [MRG][feature] Change warns signature to mimic the raises call Sep 6, 2017
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

This is looking great!

This needs some docs and a changelog entry, aside my other minor comments.

with pytest.warns(UserWarning, match=r'must be \d+$'):
warnings.warn("value must be 42", UserWarning)

with pytest.raises(pytest.fail.Exception):
Copy link
Member

Choose a reason for hiding this comment

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

Please include a case which matches with match but the warning raised is wrong:

with pytest.raises(pytest.fail.Exception):
   with pytest.warns(UserWarning, match=r'must be \d+$'):
       warnings.warn("value must be 42", RuntimeWarning)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

elif self.match_expr is not None:
for r in self:
if issubclass(r.category, self.expected_warning):
if compile(self.match_expr).search(str(r.message)):
Copy link
Member

Choose a reason for hiding this comment

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

This is minor, but you are shadowing the builtin compile. I suggest changing to re.compile and import re instead.

@massich massich force-pushed the mimic_raises_signature_in_warns branch from 8a77dfa to aa6a670 Compare September 7, 2017 07:58
@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 92.057% when pulling aa6a670 on massich:mimic_raises_signature_in_warns into b55a4f8 on pytest-dev:features.

@massich
Copy link
Contributor Author

massich commented Sep 7, 2017

The changelog of the feature branch is outdated. Souldn't it have a 3.3.0 section?
Could you update it so that I can rebase feature, or point out where should we state this feature in the changelog

@RonnyPfannschmidt
Copy link
Member

@massich we use towncrier to stop the merge pain - please add a news fragment file in the changelog folder

@massich
Copy link
Contributor Author

massich commented Sep 7, 2017

Woo thats cool. Thanks.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 92.057% when pulling 80d1654 on massich:mimic_raises_signature_in_warns into b55a4f8 on pytest-dev:features.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 92.057% when pulling a0c6758 on massich:mimic_raises_signature_in_warns into b55a4f8 on pytest-dev:features.

@RonnyPfannschmidt RonnyPfannschmidt merged commit 6967f30 into pytest-dev:features Sep 7, 2017
@RonnyPfannschmidt
Copy link
Member

well done, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants