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

Fix error when using multiline lambdas in until method #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ppopov212
Copy link

I know multi-line lambdas in Python sounds strange, but hear me out. In a project of mine, I use Black for code formatting, and Black sometimes can format l long lambdas like this

wait().at_most(5).until_asserted(
        lambda: (
            super_long_assert_assert_function(
                param1,
                param2,
                param3,
                param4,
            )
        )
    )

So in this case busypie fails with the following message

AttributeError: 'NoneType' object has no attribute 'group'

After some digging, I found that busypie inspects the lambda function and runs a regex on the source. In this case, the regex is failing to match. I'm talking about this function in func.py file

def _content_of(lambda_func: Callable) -> str:
    source_line = inspect.getsource(lambda_func)
    r = re.search(r'lambda[^:]*:\s*(.+)\s*\)', source_line) # <- this regex fails to math the source code of the lambda
    return r.group(1)   

My naive solution for a fix is to replace one or more whitespace characters with a single whitespace character. This removes the new lines and the regex is matching now. I've run the tests and they all pass

@rockem
Copy link
Owner

rockem commented Feb 21, 2024

@ppopov212 Thanks for the PR, can you also add a test for this new functionality?

@ppopov212
Copy link
Author

Sure, I'll add one

@ppopov212
Copy link
Author

@rockem I've added a test, I hope it's ok 🤞

tests/test_description.py Outdated Show resolved Hide resolved
tests/test_description.py Outdated Show resolved Hide resolved
@ppopov212
Copy link
Author

@rockem I've made changes, you can check them 🙏

@@ -40,5 +41,12 @@ def test_lambda_content_with_captures():
assert 'x == y' == e.value.description


def test_multi_line_lambda_content_description():
Copy link
Owner

Choose a reason for hiding this comment

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

Very nice!

@@ -28,5 +28,7 @@ def _is_a_lambda(func: Callable) -> bool:

def _content_of(lambda_func: Callable) -> str:
source_line = inspect.getsource(lambda_func)
source_line = re.sub(r'\\', '', source_line)
Copy link
Owner

@rockem rockem Feb 23, 2024

Choose a reason for hiding this comment

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

The problem with this solution is that it will work on strings and comments
So in order to resolve it we need something more sophisticated.
So, I think we can live with it in the description for now, and keep this PR simple
wdyt?

Copy link
Owner

Choose a reason for hiding this comment

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

?

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