-
Notifications
You must be signed in to change notification settings - Fork 2.6k
async fixes, remove __del__ and other things #2870
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
async fixes, remove __del__ and other things #2870
Conversation
6edf221
to
5b28ba7
Compare
Codecov ReportPatch coverage:
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #2870 +/- ##
===========================================
- Coverage 91.38% 77.72% -13.66%
===========================================
Files 126 126
Lines 32469 32474 +5
===========================================
- Hits 29671 25240 -4431
- Misses 2798 7234 +4436
☔ View full report in Codecov by Sentry. |
4ee679d
to
04079c0
Compare
04079c0
to
f29a239
Compare
redis/asyncio/cluster.py
Outdated
@@ -958,17 +960,18 @@ def __eq__(self, obj: Any) -> bool: | |||
|
|||
_DEL_MESSAGE = "Unclosed ClusterNode object" | |||
|
|||
def __del__(self) -> None: | |||
def __del__( | |||
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we split these lines to too (how did black pass this!). I found it.. very difficult to find where _grl was defined - so IMHO there's an ergonomic issue here. I fully accept that this is a nit + preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. This is a pattern seen in the standard lib, the idea is to capture some methods into the function so that they are available even during module teardown. Pretty ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't split the args, black formats it straight back to one line :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to add comma after last arg to make black put them on separate lines. same for list, tuples etc. final comma splits line
redis/asyncio/connection.py
Outdated
|
||
def clear_connect_callbacks(self): | ||
self._connect_callbacks = [] | ||
def deregister_connect_callback(self, callback): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the single use, I'd argue that this isn't a breaking change - but as it's meant to be internal to the library and not reused, let's fix that here. WDYT about an underscore prefix? Yes, I could argue that register_connect_callback might have the same need :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, also for non-async to keep consistency.
reader = mock.AsyncMock() | ||
writer = mock.AsyncMock() | ||
writer.transport = mock.Mock() | ||
reader = mock.Mock(spec=asyncio.StreamReader) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Teach we swami? Why is this pattern better than AsyncMock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep the AsyncMock the key here is the "spec". When a spec is used, they become synonymous.
The use of 'spec' is essential when mocking classes with some methods sync, ad some async, since it will create the
correct mock methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this went away when I rebased. But yes, when using "spec", to mock a class instance, it will create async/sync methods as appropritate. Here is the salient text:
Setting the spec of a Mock, MagicMock, or AsyncMock to a class with asynchronous and synchronous functions will automatically detect the synchronous functions and set them as MagicMock (if the parent mock is AsyncMock or MagicMock) or Mock (if the parent mock is Mock). All asynchronous functions will be AsyncMock.
tests/test_asyncio/test_cwe_404.py
Outdated
self.task.cancel() | ||
try: | ||
await self.task | ||
except asyncio.CancelledError: | ||
pass | ||
await self.server.wait_closed() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
definitely a better idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kristjanvalur I have some questions in the PR - I think they weren't sent because it was draft? Mind checking -I see them
0357301
to
044aa9d
Compare
@kristjanvalur Conflicts here too... |
c64bdb9
to
0d735ad
Compare
@kristjanvalur The CI failed |
0d735ad
to
43b3b83
Compare
TestPubSubAutoReconnect , I guess this test could do with a review, I think there is a race condition there, causing sporadic failure. |
@kristjanvalur conflicts again... |
43b3b83
to
dca114d
Compare
Pull Request check-list
Please make sure to review and check all of these items:
$ tox
pass with this change (including linting)?NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.
Description of change
__del__
handlers are rarely needed. inasyncio
they should not be used to perform IO cleanup, only emit resource usagewarnings. Code executing at
__del__
time can be very fragile and is best avoided.This PR fixes those warning emitters present, making them more "shutdown" safe.
__del__
handler from the async parser. There is no need to explicitly clear member variables.PubSub
, eliminating the need for a__del__
handler there.