Sourcery refactored main branch - #1
Conversation
| '--wsgi-file app.py --callable app') | ||
| else: | ||
| print('Unknown async_mode: ' + sio.async_mode) | ||
| print(f'Unknown async_mode: {sio.async_mode}') |
There was a problem hiding this comment.
Lines 128-128 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| '--wsgi-file latency.py --callable app') | ||
| else: | ||
| print('Unknown async_mode: ' + sio.async_mode) | ||
| print(f'Unknown async_mode: {sio.async_mode}') |
There was a problem hiding this comment.
Lines 57-57 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| '--wsgi-file latency.py --callable app') | ||
| else: | ||
| print('Unknown async_mode: ' + sio.async_mode) | ||
| print(f'Unknown async_mode: {sio.async_mode}') |
There was a problem hiding this comment.
Lines 51-51 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| def push(self, type, count=1): | ||
| timestamp = int(time.time()) * 1000 | ||
| key = '{};{}'.format(timestamp, type) | ||
| key = f'{timestamp};{type}' |
There was a problem hiding this comment.
Function EventBuffer.push refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| buffer = self.buffer | ||
| self.buffer = {} | ||
| return [value for value in buffer.values()] | ||
| return list(buffer.values()) |
There was a problem hiding this comment.
Function EventBuffer.get_and_clear refactored with the following changes:
- Replace identity comprehension with call to collection constructor (
identity-comprehension)
| await self.sio.emit('socket_disconnected', ( | ||
| namespace, | ||
| sid, | ||
| 'N/A', | ||
| datetime.utcnow().isoformat() + 'Z', | ||
| ), namespace=self.admin_namespace) | ||
| await self.sio.emit( | ||
| 'socket_disconnected', | ||
| (namespace, sid, 'N/A', f'{datetime.utcnow().isoformat()}Z'), | ||
| namespace=self.admin_namespace, | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._disconnect refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| self.admin_queue.append(('room_joined', ( | ||
| namespace, | ||
| room, | ||
| sid, | ||
| datetime.utcnow().isoformat() + 'Z', | ||
| ))) | ||
| self.admin_queue.append( | ||
| ( | ||
| 'room_joined', | ||
| (namespace, room, sid, f'{datetime.utcnow().isoformat()}Z'), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._basic_enter_room refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| self.admin_queue.append(('room_left', ( | ||
| namespace, | ||
| room, | ||
| sid, | ||
| datetime.utcnow().isoformat() + 'Z', | ||
| ))) | ||
| self.admin_queue.append( | ||
| ( | ||
| 'room_left', | ||
| (namespace, room, sid, f'{datetime.utcnow().isoformat()}Z'), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._basic_leave_room refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| else [data] | ||
| else [data] | ||
| if not isinstance(skip_sid, list): # pragma: no branch | ||
| skip_sid = [skip_sid] | ||
| for sid, _ in self.sio.manager.get_participants(namespace, room): | ||
| if sid not in skip_sid: | ||
| await self.sio.emit('event_sent', ( | ||
| namespace, | ||
| sid, | ||
| event_data, | ||
| datetime.utcnow().isoformat() + 'Z', | ||
| ), namespace=self.admin_namespace) | ||
| await self.sio.emit( | ||
| 'event_sent', | ||
| ( | ||
| namespace, | ||
| sid, | ||
| event_data, | ||
| f'{datetime.utcnow().isoformat()}Z', | ||
| ), | ||
| namespace=self.admin_namespace, | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._emit refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| await self.sio.emit('event_received', ( | ||
| namespace, | ||
| sid, | ||
| data, | ||
| datetime.utcnow().isoformat() + 'Z', | ||
| ), namespace=self.admin_namespace) | ||
| await self.sio.emit( | ||
| 'event_received', | ||
| (namespace, sid, data, f'{datetime.utcnow().isoformat()}Z'), | ||
| namespace=self.admin_namespace, | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._handle_event_internal refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| sid = self.sio.manager.sid_from_eio_sid(eio_sid, namespace) | ||
| if sid: | ||
| if sid := self.sio.manager.sid_from_eio_sid(eio_sid, namespace): | ||
| serialized_socket = self.serialize_socket(sid, namespace, | ||
| eio_sid) | ||
| await self.sio.emit('socket_connected', ( | ||
| serialized_socket, | ||
| datetime.utcfromtimestamp(t).isoformat() + 'Z', | ||
| ), namespace=self.admin_namespace) | ||
| await self.sio.emit( | ||
| 'socket_connected', | ||
| ( | ||
| serialized_socket, | ||
| f'{datetime.utcfromtimestamp(t).isoformat()}Z', | ||
| ), | ||
| namespace=self.admin_namespace, | ||
| ) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._eio_send_ping refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| namespaces = list(self.sio.handlers.keys()) | ||
| namespaces.sort() | ||
| namespaces = sorted(self.sio.handlers.keys()) |
There was a problem hiding this comment.
Function InstrumentedAsyncServer._emit_server_stats refactored with the following changes:
- Remove an unnecessary list construction call prior to sorting (
skip-sorted-list-construction)
| self.sio.manager._timestamps else 0 | ||
| self.sio.manager._timestamps else 0 |
There was a problem hiding this comment.
Function InstrumentedAsyncServer.serialize_socket refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| 'Cannot receive from rabbitmq... ' | ||
| 'retrying in {} secs'.format(retry_sleep)) | ||
| f'Cannot receive from rabbitmq... retrying in {retry_sleep} secs' | ||
| ) |
There was a problem hiding this comment.
Function AsyncAioPikaManager._listen refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| namespaces = list(set(self.handlers.keys()).union( | ||
| set(self.namespace_handlers.keys()))) | ||
| if len(namespaces) == 0: | ||
| if not namespaces: |
There was a problem hiding this comment.
Function AsyncClient.connect refactored with the following changes:
- Simplify sequence length comparison (
simplify-len-comparison)
| else: | ||
| message = {'method': 'enter_room', 'sid': sid, 'room': room, | ||
| 'namespace': namespace or '/', 'host_id': self.host_id} | ||
| await self._publish(message) # notify other hosts | ||
| message = {'method': 'enter_room', 'sid': sid, 'room': room, | ||
| 'namespace': namespace or '/', 'host_id': self.host_id} | ||
| await self._publish(message) # notify other hosts |
There was a problem hiding this comment.
Function AsyncPubSubManager.enter_room refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| else: | ||
| message = {'method': 'leave_room', 'sid': sid, 'room': room, | ||
| 'namespace': namespace or '/', 'host_id': self.host_id} | ||
| await self._publish(message) # notify other hosts | ||
| message = {'method': 'leave_room', 'sid': sid, 'room': room, | ||
| 'namespace': namespace or '/', 'host_id': self.host_id} | ||
| await self._publish(message) # notify other hosts |
There was a problem hiding this comment.
Function AsyncPubSubManager.leave_room refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| self._get_logger().debug('pubsub message: {}'.format( | ||
| data['method'])) | ||
| self._get_logger().debug(f"pubsub message: {data['method']}") |
There was a problem hiding this comment.
Function AsyncPubSubManager._thread refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| self._get_logger().error('Cannot receive from redis... ' | ||
| 'retrying in ' | ||
| '{} secs'.format(retry_sleep)) | ||
| self._get_logger().error( | ||
| f'Cannot receive from redis... retrying in {retry_sleep} secs' | ||
| ) | ||
| connect = True | ||
| await asyncio.sleep(retry_sleep) | ||
| retry_sleep *= 2 | ||
| if retry_sleep > 60: | ||
| retry_sleep = 60 | ||
| retry_sleep = min(retry_sleep, 60) |
There was a problem hiding this comment.
Function AsyncRedisManager._redis_listen_with_retries refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting) - Replace comparison with min/max call (
min-max-identity)
| sid = None | ||
| if namespace in self.handlers or namespace in self.namespace_handlers \ | ||
| or self.namespaces == '*' or namespace in self.namespaces: | ||
| or self.namespaces == '*' or namespace in self.namespaces: |
There was a problem hiding this comment.
Function AsyncServer._handle_connect refactored with the following changes:
- Simplify comparison to boolean (
simplify-boolean-comparison)
| '*' in self.handlers[namespace]: | ||
| '*' in self.handlers[namespace]: | ||
| handler = self.handlers[namespace]['*'] | ||
| args = (event, *args) | ||
| if handler: | ||
| if asyncio.iscoroutinefunction(handler): | ||
| try: | ||
| ret = await handler(*args) | ||
| except asyncio.CancelledError: # pragma: no cover | ||
| ret = None | ||
| else: | ||
| ret = handler(*args) | ||
| return ret | ||
| else: | ||
| if not handler: | ||
| return self.not_handled | ||
|
|
||
| # or else, forward the event to a namepsace handler if one exists | ||
| if asyncio.iscoroutinefunction(handler): | ||
| try: | ||
| ret = await handler(*args) | ||
| except asyncio.CancelledError: # pragma: no cover | ||
| ret = None | ||
| else: | ||
| ret = handler(*args) | ||
| return ret |
There was a problem hiding this comment.
Function AsyncServer._trigger_event refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
This removes the following comments ( why? ):
# or else, forward the event to a namepsace handler if one exists
| elif pkt.packet_type == packet.BINARY_EVENT or \ | ||
| pkt.packet_type == packet.BINARY_ACK: | ||
| elif pkt.packet_type in [packet.BINARY_EVENT, packet.BINARY_ACK]: |
There was a problem hiding this comment.
Function AsyncServer._handle_eio_message refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| if len(args) == 1 and len(kwargs) == 0 and callable(args[0]): | ||
| if len(args) == 1 and not kwargs and callable(args[0]): | ||
| # the decorator was invoked without arguments | ||
| # args[0] is the decorated function | ||
| return self.on(args[0].__name__)(args[0]) | ||
| else: | ||
| # the decorator was invoked with arguments | ||
| def set_handler(handler): | ||
| return self.on(handler.__name__, *args, **kwargs)(handler) | ||
| # the decorator was invoked with arguments | ||
| def set_handler(handler): | ||
| return self.on(handler.__name__, *args, **kwargs)(handler) | ||
|
|
||
| return set_handler | ||
| return set_handler |
There was a problem hiding this comment.
Function BaseClient.event refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Simplify sequence length comparison (
simplify-len-comparison)
| participants = ns[room]._fwdm.copy() if room in ns else {} | ||
| for sid, eio_sid in participants.items(): | ||
| yield sid, eio_sid | ||
| yield from participants.items() |
There was a problem hiding this comment.
Function BaseManager.get_participants refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
| rooms = [] | ||
| for room_name, room in self.rooms[namespace].copy().items(): | ||
| if sid in room: | ||
| rooms.append(room_name) | ||
| rooms = [ | ||
| room_name | ||
| for room_name, room in self.rooms[namespace].copy().items() | ||
| if sid in room | ||
| ] | ||
| for room in rooms: | ||
| self.basic_leave_room(sid, namespace, room) | ||
| if sid in self.callbacks: | ||
| del self.callbacks[sid] | ||
| if namespace in self.pending_disconnect and \ | ||
| sid in self.pending_disconnect[namespace]: | ||
| sid in self.pending_disconnect[namespace]: |
There was a problem hiding this comment.
Function BaseManager.basic_disconnect refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| if len(args) == 0: | ||
| if not args: | ||
| self.error_args = {'message': 'Connection rejected by server'} | ||
| elif len(args) == 1: | ||
| self.error_args = {'message': str(args[0])} | ||
| else: | ||
| self.error_args = {'message': str(args[0])} | ||
| if len(args) == 2: | ||
| self.error_args['data'] = args[1] | ||
| else: | ||
| self.error_args['data'] = args[1:] | ||
| self.error_args = { | ||
| 'message': str(args[0]), | ||
| 'data': args[1] if len(args) == 2 else args[1:], | ||
| } |
There was a problem hiding this comment.
Function ConnectionRefusedError.__init__ refactored with the following changes:
- Simplify sequence length comparison (
simplify-len-comparison) - Merge dictionary assignment with declaration (
merge-dict-assign) - Replace if statement with if expression (
assign-if-exp)
| def _kafka_listen(self): | ||
| for message in self.consumer: | ||
| yield message | ||
| yield from self.consumer |
There was a problem hiding this comment.
Function KafkaManager._kafka_listen refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
|
|
||
| def _queue(self): | ||
| queue_name = 'flask-socketio.' + str(uuid.uuid4()) | ||
| queue_name = f'flask-socketio.{str(uuid.uuid4())}' |
There was a problem hiding this comment.
Function KombuManager._queue refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| retry_sleep = 1 | ||
| while True: | ||
| message = queue.get(block=True) | ||
| message.ack() | ||
| yield message.payload | ||
| retry_sleep = 1 | ||
| except (OSError, kombu.exceptions.KombuError): | ||
| self._get_logger().error( | ||
| 'Cannot receive from rabbitmq... ' | ||
| 'retrying in {} secs'.format(retry_sleep)) | ||
| f'Cannot receive from rabbitmq... retrying in {retry_sleep} secs' | ||
| ) |
There was a problem hiding this comment.
Function KombuManager._listen refactored with the following changes:
- Hoist statements out of for/while loops (
hoist-statement-from-loop) - Replace call to format with f-string (
use-fstring-for-formatting)
| having a single method that catches all events is desired. | ||
| """ | ||
| handler_name = 'on_' + event | ||
| handler_name = f'on_{event}' |
There was a problem hiding this comment.
Function Namespace.trigger_event refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
Branch
mainrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
mainbranch, then run:Help us improve this pull request!