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

Refactor src/_pytest/config/__init__.py to use the warnings module instead of stderr for warnings #7396

Merged
merged 8 commits into from
Jun 28, 2020

Conversation

gnikonorov
Copy link
Member

Closes #7295

Refactors src/_pytest/config/__init__.py to use the warnings module instead of writing out warnings to stderr

@gnikonorov
Copy link
Member Author

Hey @nicoddemus @RonnyPfannschmidt - any idea why codecov says I only hit 66% of the diff? I ( think I ) have adequate testing

@RonnyPfannschmidt
Copy link
Member

based on the coverage diff view i would guess that the assertion disabled case was not hit by coverage, which it should be if i remember correct

as such i believe a investigation is needed into that,

@RonnyPfannschmidt
Copy link
Member

unfortunately i wont be able to do any deep work on pytest until the next weekend

@gnikonorov
Copy link
Member Author

Do we want to allow this to merge before that? Or should we hold off until we check codecov

@RonnyPfannschmidt
Copy link
Member

I'll defer to @nicoddemus on that one

@gnikonorov
Copy link
Member Author

This definitely looks like an issue with codecov, and not the PR.

I updated this PR with 33de350 to parametrize some of the tests, and it looks like that dropped coverage from 66% to 60% even though no coverage was added or taken away.

I suggest we open an issue to track this and treat it separately @nicoddemus and @RonnyPfannschmidt

@gnikonorov
Copy link
Member Author

@nicoddemus are you opposed to merging this in and treating the codecov as a separate issue?

@gnikonorov
Copy link
Member Author

Gentle ping @nicoddemus

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Nice cleanup! I left a few minor comments/questions.

As a follow up I think it would be good to move _issue_warning_captured from _pytest.warning to some more suitable place, probably _pytest.config, since it's obviously misplaced now - every single caller of it imports it lazily due to import cycles...

from _pytest.warnings import _issue_warning_captured

_issue_warning_captured(
PytestConfigWarning(message), self.hook, stacklevel=2,
Copy link
Member

Choose a reason for hiding this comment

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

I'm always not 100% which value it should be, so just making sure, did you check the stacklevel=2 is the best value here? I think it would be best to point at the caller of _warn_or_fail_if_strict.

Copy link
Member Author

Choose a reason for hiding this comment

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

the caller of _validate_keys is _preparse, which already had stacklevel=2, so we'd want 2 here as well right @bluetech ?

src/_pytest/config/__init__.py Outdated Show resolved Hide resolved
" using python -O?"
)
_issue_warning_captured(
PytestConfigWarning(warning_text), self.hook, stacklevel=2,
Copy link
Member

Choose a reason for hiding this comment

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

Same question here re. stacklevel.

Copy link
Member Author

Choose a reason for hiding this comment

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

same reply as to your original question. That would mean we want stacklevel 2 here as well right?

@bluetech
Copy link
Member

BTW when I checked it out there was a minor conflict with master around the create_terminal_writer function, so a rebase would be good.

@nicoddemus
Copy link
Member

As a follow up I think it would be good to move _issue_warning_captured from _pytest.warning to some more suitable place, probably _pytest.config, since it's obviously misplaced now - every single caller of it imports it lazily due to import cycles...

Definitely agree, every caller also has access to config, so it makes perfect sense.

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.

Looks great @gnikonorov thanks!

Please take a look at the comments made by @bluetech, I think they are worth doing/checking.

We should follow up with making _issue_warning_captured a method of Config after this gets merged.

@gnikonorov
Copy link
Member Author

gnikonorov commented Jun 27, 2020

BTW when I checked it out there was a minor conflict with master around the create_terminal_writer function, so a rebase would be good.

@bluetech - I looked at the diff and I don't see any changes/conflicts, and just merged in upstream. What do you see is inconsistent?

@bluetech
Copy link
Member

I looked at the diff and I don't see any changes/conflicts, and just merged in upstream. What do you see is inconsistent?

I guess it was just local, github didn't complain as well. Anyway it's good now, thanks!

(BTW, I personally prefer rebasing PRs instead of merging master into them, I find that a little cleaner, but whatever you prefer is OK).

the caller of _validate_keys is _preparse, which already had stacklevel=2, so we'd want 2 here as well right

I'm not sure I understand, so let me explain more what I mean. The stacklevel determines which line the warning message will appear to come from for the user. For example for the strict config warning, with stacklevel=2 the warning looks like this:

src/_pytest/config/__init__.py:1188
  /home/ran/src/pytest/src/_pytest/config/__init__.py:1188: PytestConfigWarning: Unknown config ini key: bla
  
    _issue_warning_captured(

the question is whatever _issue_warning_captured is the best place to point to. For reference, here it is with different values.

0:

src/_pytest/warnings.py:205
  /home/ran/src/pytest/src/_pytest/warnings.py:205: PytestConfigWarning: Unknown config ini key: bla
  
    warnings.warn(warning, stacklevel=0)

1:

src/_pytest/warnings.py:205
  /home/ran/src/pytest/src/_pytest/warnings.py:205: PytestConfigWarning: Unknown config ini key: bla
  
    warnings.warn(warning, stacklevel=1)

2: See above.

3:

src/_pytest/config/__init__.py:1148
  /home/ran/src/pytest/src/_pytest/config/__init__.py:1148: PytestConfigWarning: Unknown config ini key: bla
  
    self._warn_or_fail_if_strict("Unknown config ini key: {}\n".format(key))

4:

src/_pytest/config/__init__.py:1125
  /home/ran/src/pytest/src/_pytest/config/__init__.py:1125: PytestConfigWarning: Unknown config ini key: bla
  
    self._validate_keys()

5:

src/_pytest/config/__init__.py:1204
  /home/ran/src/pytest/src/_pytest/config/__init__.py:1204: PytestConfigWarning: Unknown config ini key: bla
  
    self._preparse(args, addopts=addopts)

etc... In this case IMO the best one is stacklevel=3.

@gnikonorov
Copy link
Member Author

Thanks for the explanation @bluetech ! I agree, and changed the stack level to 3 for my changes

I also made a note in #7426 to re-evaluate the stack levels we currently use. We use 2 in this file. perhaps here ( and elsewhere ) we can change the stack level to be more appropriate

@bluetech bluetech merged commit e6e300e into pytest-dev:master Jun 28, 2020
@bluetech
Copy link
Member

Merged myself to override the patch coverage issue

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.

refactor src/_pytest/config/__init__.py to use the warnings module
4 participants