Skip to content

BUG: Make assert_string_equal check str equality simply without regex #11556

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

Merged
merged 1 commit into from
Jul 14, 2018

Conversation

sh0nk
Copy link
Contributor

@sh0nk sh0nk commented Jul 13, 2018

assert_string_equal on testing utils.py is expected to test string equality, but it fails if there are special characters which are used for regex syntax.

This is because this method uses regex pattern on desired input to check the equality as it is. There is another way to fix this by escaping the regex pattern: re.match(r'\A'+re.escape(desired)+r'\Z', actual, re.M) but just to use string equality check (implying __eq__) should be enough and lightweight.

The added test case worked the other way around before fixing.

@sh0nk sh0nk force-pushed the fix-assert_string_equal branch from ec9d466 to daba50a Compare July 13, 2018 15:59
@sh0nk sh0nk changed the title BUG: Make assert_string_equal check string eqaulity simply without regex BUG: Make assert_string_equal check str equality simply without regex Jul 13, 2018
@sh0nk sh0nk force-pushed the fix-assert_string_equal branch from daba50a to 37e4d58 Compare July 13, 2018 17:56
Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

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

Just added a few thoughts / comments; not necessarily problems though.

@@ -1081,6 +1081,12 @@ def test_simple(self):

assert_raises(AssertionError,
lambda: assert_string_equal("foo", "hello"))

def test_regex(self):
assert_string_equal("a+*b", "a+*b")
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it is worth using a parametrized test (from pytest) to probe a few more of the regular expression special characters used in Python. On the other hand, if I'm not mistaken, there may not be much drive for this kind of support in numpy.testing moving forward with increased adoption of pytest.

Copy link
Member

Choose a reason for hiding this comment

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

there may not be much drive for this kind of support in numpy.testing

How so? Does pytest offer an equivalent for the function being tested?

Copy link
Member

Choose a reason for hiding this comment

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

Probably OK to skip complete coverage here unless there is a return to regular expressions.

@tylerjereddy Now that you are working a BIDS, do you need commit privileges?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that's up to the team, but I'm happy to continue without for now. I don't think pytest has a replacement functionality for what NumPy does here.

assert_string_equal("a+*b", "a+*b")

assert_raises(AssertionError,
lambda: assert_string_equal("aaa", "a+b"))
Copy link
Contributor

@tylerjereddy tylerjereddy Jul 13, 2018

Choose a reason for hiding this comment

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

Might be a bit less awkward to use a context manager (available from assert_raises or the pytest equivalent) instead of using lambda for a callable. The docstring for assert_string_equal also claims to return the diff between strings--not sure if we need to test that case with special regex chars.

Copy link
Contributor

Choose a reason for hiding this comment

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

That said, the lambda form used here is consistent with the rest of the file.

Copy link
Member

Choose a reason for hiding this comment

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

The tests function is pretty old (2009), so could use a rewrite sometime in the future. The fixed code dates back to 2007 ...

@charris charris merged commit a1d0314 into numpy:master Jul 14, 2018
@charris
Copy link
Member

charris commented Jul 14, 2018

Thanks @sh0nk .

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