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

allowing Matches to accept re.compile #135

Merged
merged 4 commits into from
Apr 30, 2024

Conversation

bandophahita
Copy link
Contributor

The resolving function matches_regexp of Matches can accept either str or Pattern. This update makes it possible to pass the pattern directly into Matches.

@bandophahita bandophahita changed the title allowing Matches to accept re.compile allowing Matches to accept re.compile Mar 19, 2024
@bandophahita bandophahita requested a review from a team March 19, 2024 22:34
Copy link
Member

@perrygoy perrygoy left a comment

Choose a reason for hiding this comment

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

Some changes requested and a couple questions!

screenpy/resolutions/matches.py Show resolved Hide resolved
Comment on lines +30 to +37
if isinstance(self.pattern, Pattern):
return f"r'{self.pattern.pattern}'"
return f"r'{self.pattern}'"
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to move this logic to represent_prop?

Copy link
Contributor Author

@bandophahita bandophahita Apr 2, 2024

Choose a reason for hiding this comment

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

There is a slight issue with doing that. I thought it best to show the pattern in the log as r'pattern', which includes the r before the string quotes.

... hoping it's text matching the pattern r'\tpattern'. -- GOOD
... hoping it's text matching the pattern '\\tpattern'. -- EW!
... hoping it's text matching the pattern ' pattern'. -- BAD!

This is doable with Pattern objects but when the pattern is a string, there is no way for represent_prop to differentiate r-strings from regular strings.

    @property
    def pattern_to_log(self) -> str:
        """Represent the item in a log-friendly way."""
        return represent_prop(self.pattern)

The above will fail the logging tests:

Expected :"Text matching the pattern r'(spam)+'."
Actual   :"Text matching the pattern '(spam)+'."

To counter this, I considered adding a switch argument to represent_prop which would gives the caller the option to turn on/off the r in front of strings but I felt like this was an over complicated solution for a single prop.

Copy link
Member

Choose a reason for hiding this comment

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

I still think we should move this logic to represent_prop, we have other Pattern-accepting -ables.

Maybe we can do something like this, here:

def pattern_to_log(self):
    """Represent the item in a log-friendly way."""
    loggable_pattern  = represent_prop(self.pattern)
    if not loggable_pattern.startswith("r"):
        loggable_pattern = f"r'{loggable_pattern}'"
    return loggable_pattern

How does that sit with you?

Copy link
Contributor Author

@bandophahita bandophahita Apr 3, 2024

Choose a reason for hiding this comment

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

When self.pattern is a string, represent_prop will call repr

    if isinstance(item, str):
        return repr(item)

Which is unfortunately not how we want the pattern to appear in the log.

represent_prop(r"\tpattern") would return "\\tpattern". Putting an r in front of the string wouldn't fix that.

screenpy/resolutions/matches.py Outdated Show resolved Hide resolved
screenpy/resolutions/matches.py Show resolved Hide resolved
@bandophahita
Copy link
Contributor Author

@perrygoy back to you.

@bandophahita bandophahita force-pushed the regex_resolution_update branch from f1925f4 to 5dd6f5e Compare April 3, 2024 19:12
@bandophahita bandophahita requested a review from perrygoy April 10, 2024 16:35
Copy link
Member

@perrygoy perrygoy left a comment

Choose a reason for hiding this comment

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

Looks good! I only had a couple comments for consideration, they are not blocking.

Comment on lines +575 to +579
def test_the_test_with_compile(self) -> None:
m = Matches(re.compile(r"([Ss]pam ?)+")).resolve()

assert m.matches("Spam spam spam spam baked beans and spam")
assert not m.matches("What do you mean Eugh?!")
Copy link
Member

Choose a reason for hiding this comment

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

I personally wouldn't mind having this be part of the "test the test" test above, that's the pattern we've followed in all the other tests. But i don't mind it being a separate test.

Comment on lines +598 to +603
def test_description_with_compile(self) -> None:
test_match = re.compile(r"(spam)+")

m = Matches(test_match)

expected_description = "Text matching the pattern r'(spam)+'."
Copy link
Member

Choose a reason for hiding this comment

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

Same goes for this test; we've just grouped all the "describe correctly" tests in one in other ones (though currently the only examples i can think of are for testing SeeAllOf and SeeAnyOf).

@perrygoy perrygoy merged commit 37e2bc8 into ScreenPyHQ:trunk Apr 30, 2024
12 checks passed
@bandophahita bandophahita deleted the regex_resolution_update branch August 2, 2024 15:51
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.

2 participants