FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Changed fstring to make error clearer · Pull Request #21968 · matplotlib/matplotlib · GitHub

Repository navigation

Changed fstring to make error clearer - #21968

Closed
ghost wants to merge 6 commits into
mainfrom
unknown repository
Closed

ghost wants to merge 6 commits into
mainfrom
unknown repository

Conversation

ghost commented Dec 15, 2021

Copy link
Copy Markdown

#21959 @tacaswell @e5f6bd

Changed the fstring in error message

Copy link
Copy Markdown
Member

Thanks @sveneschlbeck !

Could you possibly add (or modify an existing) test so that we verify that the repr rather than the str version is in the error message? I could imagine a future where someone drops something that has a very bad repr into c and goes "Why is this repring, the 'str' version is better!" and we flip back. By putting in a test you pin down that we intentionally chose repr here (sorry if this is something obvious to you, I do not know you yet so I do not know what you know and I am writing as much for a future reader as you)

I think using a color with a '\n' or a '\t' is a perfect example to test.

tacaswell added this to the v3.6.0 milestone Dec 15, 2021

ghost commented Dec 15, 2021

Copy link
Copy Markdown
Author

@tacaswell Thanks for the explanation. I'm happy to do that. Where would the test ideally be located?

Copy link
Copy Markdown
Member

I would put them someplace around here:

def test_scatter_color(self):
# Try to catch cases where 'c' kwarg should have been used.
with pytest.raises(ValueError):
plt.scatter([1, 2], [1, 2], color=[0.1, 0.2])
with pytest.raises(ValueError):
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])

pytest.raises takes an optional argument that is a regex for the string in the exception to check that it has the content you expect.

ghost commented Dec 16, 2021

Copy link
Copy Markdown
Author

Thanks!

Comment thread lib/matplotlib/tests/test_axes.py Outdated
with pytest.raises(ValueError):
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
with pytest.raises(ValueError):
plt.scatter([1, 2], [1, 2], color='blue\n')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This raised a value error before, so maybe test the error message with a matches=... to the pytest.raises?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Isn't this just to test whether an error gets raised when given the blue\n argument? I know that the error was thrown before but in addition to changing the error message slightly, we wanted to add a test.
Maybe, I am not understanding you correctly...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think we also want to test that the repr is being used, not the str...

ghost Dec 16, 2021 •
edited by ghost
Loading

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Okay, now I got what you mean. I changed it but I now realise that this is misplaced here. It now reads

with pytest.raises(ValueError, match=f"'c' argument must be a color, "
                           f"a sequence of colors, or a sequence of numbers, "
                           f"not {c!r}"):
            plt.scatter([1, 2], [1, 2], color='blue\n')

But the entire section is about examples, where color was used and c should have been used. However, this is not the case here. This case is about c already being used but the error message being too unspecific. The example should read instead

with pytest.raises(ValueError, match=f"'c' argument must be a color, "
                           f"a sequence of colors, or a sequence of numbers, "
                           f"not {c!r}"):
            plt.scatter([1, 2], [1, 2], c='blue\n')

but be placed in a different section, maybe underneath a comment like this:

# Check for the specificity of error message if invalid 'c' argument is given

Since I don't know if such a section already exists and - if not - where to place it, I wanted to wait for your opinion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would go with a slightly simpler test and only search for a literal new line (''\n'") rather than trying to match the whole thing.

The input here is also a regular expression which can require escaping a lot of symbols which can get tedious. I am also not sure we want to pin down in a test the exact wording just that we got back the reper of the string you put in.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Okay, I understand. Nevertheless, the question where to put it remains. Should I just append the example to the previous ones separated by a comment?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It is not super important either way. The benefit of putting it in an existing test is we have fewer tests and the test suite runs marginally faster, the benefit of putting it in its own test is if it fails it is much more obvious what part of the test is failing!

Copy link
Copy Markdown
Contributor

@sveneschlbeck are you still working on this issue ?

ghost commented Dec 20, 2021

Copy link
Copy Markdown
Author

@gajendra0180 Yes, I am

plt.scatter([1, 2], [1, 2], color=[0.1, 0.2])
with pytest.raises(ValueError):
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
with pytest.raises(ValueError, match=f"{c!r}"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I have encountered this issue, and thanks for the PR! Would this simple test below work? @sveneschlbeck @tacaswell

Suggested change
with pytest.raises(ValueError, match=f"{c!r}"):
with pytest.raises(ValueError, match=r"\\n|\\t|\\r"):
plt.scatter([1, 2], [1, 2], c='blue\n')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@yuanx749 Thanks for the input...
@tacaswell Is that still the same logic? I think this is not exactly the same/equivalent message, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Picking the conditions on the test is a bit of an art.

Too specific and you can end up with a really brittle test suite that breaks on tiny changes that no user will ever care about. A test suite that is always broken or you are always having to change with the code is not really doing its job and causing you a whole lot of work!

Too general it will always pass, even if you make changes that significantly break users. This is also not great as again you test suite is not doing its job and still causing you a whole bunch of work.

As written the tests are failing because https://github.com/matplotlib/matplotlib/runs/4566462001?check_suite_focus=true#step:12:142

________________________ TestScatter.test_scatter_color ________________________
143
[gw1] linux -- Python 3.10.1 /opt/hostedtoolcache/Python/3.10.1/x64/bin/python

self = <matplotlib.tests.test_axes.TestScatter object at 0x7f345c9acbb0>
    def test_scatter_color(self):
        # Try to catch cases where 'c' kwarg should have been used.
        with pytest.raises(ValueError):
            plt.scatter([1, 2], [1, 2], color=[0.1, 0.2])
        with pytest.raises(ValueError):
            plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
>       with pytest.raises(ValueError, match=f"{c!r}"):
E       NameError: name 'c' is not defined
lib/matplotlib/tests/test_axes.py:2215: NameError

in the namespace where the fstring is being evaluated we do not have c defined. You could change the test so the you do

c = 'blue\n'
with pytest.raises(ValueError, match=f"{c!r}"):
    plt.scatter([1, 2], [1, 2], c=c)

which tests that the literal string 'blue\\n' shows up. With @yuanx749 's suggestion you do not have to change anything else and it checks that at least one of new line, tab, or carriage return is literally in the message.

Between these two I would lean slightly towards searching for 'blue\n' (assume the escaping is right), in case we in the future add a note like "be careful you do not have a trailing \t or \n!" into the error message. In that case we would be able to stop putting the repr in and still have the test pass. However, I would be happy to merge this in either case so long as the tests pass now, the new test would fail without your other changes, and you can make a case as to why you wrote the test the way you did if asked.

jklymak commented Jan 5, 2022

Copy link
Copy Markdown
Member

I'm popping to draft until tests pass. Feel free to make active again. Thanks!

jklymak marked this pull request as draft January 5, 2022 08:55
oscargus mentioned this pull request Mar 27, 2022
2 tasks done
oscargus mentioned this pull request Apr 24, 2022
6 tasks
QuLogic modified the milestones: v3.6.0, v3.7.0 Jul 5, 2022
This pull request was closed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

5 participants


Back | FazBrowse Home | New Git URL