Skip to content

Commit cd6e790

Browse files
committed
Added fallbacks for old valid channel/group name checks.
Internal methods are used by channel layers, so restore and deprecate in order to allow updating.
1 parent 81c61c2 commit cd6e790

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

CHANGELOG.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Full release notes, with more details and upgrade information, are available at:
22
https://channels.readthedocs.io/en/latest/releases
33

4+
UNRELEASED
5+
----------
6+
7+
* Added fallbacks for old valid channel/group name checks.
8+
9+
These were renamed in 4.2.1 but (as internal methods) without deprecation.
10+
They are restored (and deprecated) here to allow updating channel layers
11+
using them.
412

513
4.2.1 (2025-03-29)
614
------------------

channels/layers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import string
66
import time
7+
import warnings
78
from copy import deepcopy
89

910
from django.conf import settings
@@ -205,6 +206,29 @@ async def group_discard(self, group, channel):
205206
async def group_send(self, group, message):
206207
raise NotImplementedError("group_send() not implemented (groups extension)")
207208

209+
# Deprecated methods.
210+
def valid_channel_name(self, channel_name, receive=False):
211+
"""
212+
Deprecated: Use require_valid_channel_name instead.
213+
"""
214+
warnings.warn(
215+
"valid_channel_name is deprecated, use require_valid_channel_name instead.",
216+
DeprecationWarning,
217+
stacklevel=2,
218+
)
219+
return self.require_valid_channel_name(channel_name)
220+
221+
def valid_group_name(self, group_name):
222+
"""
223+
Deprecated: Use require_valid_group_name instead..
224+
"""
225+
warnings.warn(
226+
"valid_group_name is deprecated, use require_valid_group_name instead.",
227+
DeprecationWarning,
228+
stacklevel=2,
229+
)
230+
return self.require_valid_group_name(group_name)
231+
208232

209233
class InMemoryChannelLayer(BaseChannelLayer):
210234
"""

tests/test_layers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,37 @@ def test_channel_name_length_error_message(name):
123123

124124
with pytest.raises(TypeError, match=expected_error_message):
125125
layer.require_valid_channel_name(name)
126+
127+
128+
def test_deprecated_valid_channel_name():
129+
"""
130+
Test that the deprecated valid_channel_name method works
131+
but raises a deprecation warning.
132+
"""
133+
layer = BaseChannelLayer()
134+
135+
# Should work with valid name but raise warning
136+
with pytest.warns(DeprecationWarning, match="valid_channel_name is deprecated"):
137+
assert layer.valid_channel_name("valid-channel")
138+
139+
# Should raise TypeError for invalid names
140+
with pytest.warns(DeprecationWarning):
141+
with pytest.raises(TypeError):
142+
layer.valid_channel_name(\\_(ツ)_/¯")
143+
144+
145+
def test_deprecated_valid_group_name():
146+
"""
147+
Test that the deprecated valid_group_name method works
148+
but raises a deprecation warning.
149+
"""
150+
layer = BaseChannelLayer()
151+
152+
# Should work with valid name but raise warning
153+
with pytest.warns(DeprecationWarning, match="valid_group_name is deprecated"):
154+
assert layer.valid_group_name("valid-group")
155+
156+
# Should raise TypeError for invalid names
157+
with pytest.warns(DeprecationWarning):
158+
with pytest.raises(TypeError):
159+
layer.valid_group_name(\\_(ツ)_/¯")

0 commit comments

Comments
 (0)