django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.11k stars 800 forks source link

Use the async session.asave method if it exists #2092

Closed bigfootjon closed 3 months ago

bigfootjon commented 7 months ago

Support was added in https://github.com/django/django/pull/17372 (slated for release in 5.1)

We can use this API if it exists, otherwise, we fallback to a sync_to_async bridge.

This does break backwards compatibility, which I'm not terribly happy about but I'm not sure how best to fix it.

If users overrode save_session in a subclass they would expect that to still work, so I thought the best way to do this was to make it async in the base class and then await it, so at least it will be obvious that it doesn't work (python will raise an exception about awaiting a non-awaitable), but I'm open to other solutions.

bigfootjon commented 7 months ago

@carltongibson: I've added the missing test coverage (such as I could, see inline comment) and fixed the thing you noted.

I'm not sure this should be merged before https://github.com/django/channels/pull/2090 is resolved since sessions can use the Django ORM under the hood.

carltongibson commented 7 months ago

Yes, I too was thinking we need to settle that first 👍

carltongibson commented 4 months ago

Ref the breaking change here: I think if we're super clear in the release notes that may have to do.

bigfootjon commented 4 months ago

Alright, we've got full test coverage now!

bigfootjon commented 4 months ago

Once this PR is merged I'll add the backwards compat notices to: https://github.com/django/channels/pull/2111