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

Add warning for the length of the group name #2122

Merged
merged 12 commits into from
Jan 28, 2025
Merged

Conversation

IronJam11
Copy link
Contributor

Resolves #1845
Corresponding screenshot :-

Screenshot 2025-01-10 at 8 07 13 PM

Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

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

Hi @IronJam11. Thanks for this. Needs a regression test, demonstrating the fix.

@@ -201,6 +205,7 @@ async def flush(self):
raise NotImplementedError("flush() not implemented (flush extension)")

async def group_add(self, group, channel):
print("Hello")
Copy link
Member

Choose a reason for hiding this comment

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

This (and others) need removing.

@IronJam11
Copy link
Contributor Author

I will work on it. thank you for your review. if you could elaborate on regression test it will be helpful for me as I am a bit new to this org

@carltongibson
Copy link
Member

Look at the existing test suite for examples, I'd suggest.

https://channels.readthedocs.io/en/latest/contributing.html#how-do-i-get-started-and-run-the-tests

@IronJam11
Copy link
Contributor Author

I have successfully tested it on my local, there are issues with the tests. :) And I removed all the unnecessary statements as well

@IronJam11
Copy link
Contributor Author

Correct me if I am wrong, so i ran pytest (all tests passed with 2 warning only), does that qualify to be fit for a valid pr?

@carltongibson
Copy link
Member

@IronJam11 You need to add a new test showing the issue. It should fail without your patch and pass with it.

Django has a tutorial for writing your first patch:

https://docs.djangoproject.com/en/dev//intro/contributing/#writing-some-tests-for-your-ticket

It's not exactly the same here, but the general ideas are the same.

@IronJam11
Copy link
Contributor Author

I have added a regression test as mentioned. Is there anything else I will have to attach?

@bigfootjon
Copy link
Collaborator

@IronJam11 it looks like you've REPLACED an existing test with your new test, we don't want to lose the old one!

@IronJam11
Copy link
Contributor Author

Apologies, I added the erased test

@carltongibson
Copy link
Member

Looking close @IronJam11 — thanks.

I'm bemused by the existing assert code. The valid_group_name() method either returns True or raises, so the asserts can never fail — the TypeError is raised before the AssertionError would be. So the assert error message is never needed. 🤔 @bigfootjon Am I just misreading that?

(Not an issue with this PR @IronJam11!)

Comment on lines 375 to 377
assert self.valid_group_name(group), (
f"Group name must be" f"less than {self.MAX_NAME_LENGTH} characters."
)
Copy link
Member

@carltongibson carltongibson Jan 15, 2025

Choose a reason for hiding this comment

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

As per the point about the TypeError being raised before the AssertionError, it's not clear we need to make any change here, or on lines 344/349 and 353/360 above.

@IronJam11
Copy link
Contributor Author

You are absolutely right ! My bad there (I had actually used it to debug initially), i ran the tests again and it seems to be working just fine :)

Copy link
Collaborator

@bigfootjon bigfootjon left a comment

Choose a reason for hiding this comment

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

So the assert error message is never needed. 🤔 @bigfootjon Am I just misreading that?

Lol, yeah you're reading that right. We should probably remove the assert altogether and change the method name to require_valid_group_name so that behavior is more clear.

channels/layers.py Outdated Show resolved Hide resolved
@IronJam11
Copy link
Contributor Author

I inlined it as well :), also other than this, if there are other issues to tackle, I will be more than happy to contribute :}

@IronJam11
Copy link
Contributor Author

So the assert error message is never needed. 🤔 @bigfootjon Am I just misreading that?

Lol, yeah you're reading that right. We should probably remove the assert altogether and change the method name to require_valid_group_name so that behavior is more clear.

I made the necessary changes

Copy link
Collaborator

@bigfootjon bigfootjon left a comment

Choose a reason for hiding this comment

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

Looking better!

assert self.valid_group_name(group), "Group name not valid"
assert self.valid_channel_name(channel), "Channel name not valid"
self.require_valid_group_name(group)
self.valid_channel_name(channel), "Channel name not valid"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you rename valid_channel_name to require_valid_channel_name to match?

And also remove the , "Channel name not valid" since it isn't used?

assert self.valid_group_name(group), "Group name not valid"
assert self.valid_channel_name(channel), "Channel name not valid"
self.require_valid_group_name(group)
self.valid_channel_name(channel), "Channel name not valid"
# Add to group dict
self.groups.setdefault(group, {})
self.groups[group][channel] = time.time()

async def group_discard(self, group, channel):
# Both should be text and valid
assert self.valid_channel_name(channel), "Invalid channel name"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here as aboev

@IronJam11
Copy link
Contributor Author

  • Added another test for channels
  • Made the error message inlined
  • Removed the unnecessary asserts

@IronJam11 IronJam11 requested a review from bigfootjon January 22, 2025 12:23
Copy link
Collaborator

@bigfootjon bigfootjon left a comment

Choose a reason for hiding this comment

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

One final comment!


assert all(
self.valid_channel_name(channel, receive=receive) for channel in names
all(
Copy link
Collaborator

Choose a reason for hiding this comment

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

The all doesn't have semantic value, can you refactor this as a normal for loop?

@IronJam11
Copy link
Contributor Author

Done as you said :)

Copy link
Collaborator

@bigfootjon bigfootjon left a comment

Choose a reason for hiding this comment

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

@carltongibson any final thoughts?

@IronJam11 please fix the lint errors as indicated in the failing signal on this PR

@IronJam11
Copy link
Contributor Author

sorry sir, done !!

Copy link
Member

@carltongibson carltongibson 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. Just one small query?

return True
for channel in names:
self.require_valid_channel_name(channel, receive=receive)
return
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 mean return True? return None (explicitly)? Which?

@IronJam11
Copy link
Contributor Author

yeah absolutely it is supposed to be return True.

@bigfootjon bigfootjon merged commit a144b4b into django:main Jan 28, 2025
7 checks passed
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.

When group name is too long, don't raise TypeError
3 participants