-
Notifications
You must be signed in to change notification settings - Fork 2
Sourcery Starbot ⭐ refactored Catstyle/pymaid #30
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
base: develop
Are you sure you want to change the base?
Conversation
| for filename in filenames: | ||
| if filename.endswith('.proto'): | ||
| protos.append(os.path.join(root, filename)) | ||
| protos.extend( | ||
| os.path.join(root, filename) | ||
| for filename in filenames | ||
| if filename.endswith('.proto') | ||
| ) | ||
|
|
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.
Function get_protos refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend)
| def connection_made(self, transport): | ||
| sock = transport.get_extra_info('socket') | ||
| args.debug('Connection to {}'.format(sock)) | ||
| args.debug(f'Connection to {sock}') |
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.
Function EchoProtocol.connection_made refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| req = b'a' * args.msize | ||
| receive_event = protocol.receive_event = asyncio.Event() | ||
| for x in range(count): | ||
| for _ in range(count): |
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.
Function wrapper refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| tasks = [] | ||
| for x in range(args.concurrency): | ||
| tasks.append(asyncio.create_task( | ||
| tasks = [asyncio.create_task( | ||
| wrapper(loop, args.address, args.request) | ||
| )) | ||
|
|
||
| ) for _ in range(args.concurrency)] |
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.
Function main refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Replace unused for index with underscore (
for-index-underscore)
| def connection_made(self, transport): | ||
| sock = transport.get_extra_info('socket') | ||
| args.debug('Connection from {}'.format(sock)) | ||
| args.debug(f'Connection from {sock}') |
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.
Function EchoProtocol.connection_made refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| tasks = [] | ||
| address = args.address | ||
| request = args.request | ||
| for x in range(args.concurrency): | ||
| tasks.append( | ||
| pymaid.create_task( | ||
| tasks = [pymaid.create_task( | ||
| worker( | ||
| address, | ||
| service, | ||
| request, | ||
| transport_class=WebSocket | Connection, | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ) for _ in range(args.concurrency)] |
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.
Function main refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Move assignment closer to its usage within a block (
move-assign-in-block) - Replace unused for index with underscore (
for-index-underscore)
| for filename in filenames: | ||
| if filename.endswith('_pb2.py'): | ||
| modules.append(os.path.join(root, filename)) | ||
| modules.extend( | ||
| os.path.join(root, filename) | ||
| for filename in filenames | ||
| if filename.endswith('_pb2.py') | ||
| ) | ||
|
|
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.
Function get_modules refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend)
| fields.extend(extra_message(field.message_type, indent + ' ')) | ||
| fields.extend(extra_message(field.message_type, f'{indent} ')) |
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.
Function extra_message refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| req = star_indent + 'req: ' + method.input_type.name + star_indent | ||
| req = f'{star_indent}req: {method.input_type.name}{star_indent}' | ||
| req += star_indent.join(extra_message(method.input_type)) | ||
| resp = star_indent + 'resp: ' + method.output_type.name + star_indent | ||
| resp = f'{star_indent}resp: {method.output_type.name}{star_indent}' | ||
| resp += star_indent.join(extra_message(method.output_type)) | ||
| input_type = prefix + '.' + method.input_type.full_name | ||
| output_type = prefix + '.' + method.output_type.full_name | ||
| input_type = f'{prefix}.{method.input_type.full_name}' | ||
| output_type = f'{prefix}.{method.output_type.full_name}' |
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.
Function generate_jsimpl refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| with open(file_path + '_broadcast.js', 'w') as fp: | ||
| with open(f'{file_path}_broadcast.js', 'w') as fp: |
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.
Function generate refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| for filename in filenames: | ||
| if filename.endswith('_pb2.py'): | ||
| modules.append(os.path.join(root, filename)) | ||
| modules.extend( | ||
| os.path.join(root, filename) | ||
| for filename in filenames | ||
| if filename.endswith('_pb2.py') | ||
| ) | ||
|
|
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.
Function get_modules refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend)
| input_type = prefix + '.' + method.input_type.full_name | ||
| output_type = prefix + '.' + method.output_type.full_name | ||
| input_type = f'{prefix}.{method.input_type.full_name}' | ||
| output_type = f'{prefix}.{method.output_type.full_name}' |
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.
Function generate_js_rpc refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| with open(file_path + '_rpc.js', 'w') as fp: | ||
| with open(f'{file_path}_rpc.js', 'w') as fp: |
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.
Function generate refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| if self.subparsers: | ||
| subcmd = self.get_subcmd(args) | ||
| if subcmd: | ||
| if subcmd := self.get_subcmd(args): |
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.
Function ArgumentParser.on_parse_callback refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| nid = item['notificationId'] | ||
| data = get_data(ns, self.subscriptions[ns]['format']) | ||
| if data: | ||
| if data := get_data(ns, self.subscriptions[ns]['format']): |
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.
Function ApolloBackend.run refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| fragment = parsed.fragment | ||
|
|
||
| if 'unix' == scheme: | ||
| if scheme == 'unix': |
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.
Function parse_uri refactored with the following changes:
- Ensure constant in comparison is on the right (
flip-comparison)
| if ('websocket' != headers.get('Upgrade', '').lower() | ||
| or 'upgrade' != headers.get('Connection', '').lower()): | ||
| if ( | ||
| headers.get('Upgrade', '').lower() != 'websocket' | ||
| or headers.get('Connection', '').lower() != 'upgrade' | ||
| ): |
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.
Function WSProtocol.build_response refactored with the following changes:
- Ensure constant in comparison is on the right (
flip-comparison)
| if ('websocket' != headers.get('Upgrade', '').lower() | ||
| or 'upgrade' != headers.get('Connection', '').lower()): | ||
| if ( | ||
| headers.get('Upgrade', '').lower() != 'websocket' | ||
| or headers.get('Connection', '').lower() != 'upgrade' | ||
| ): |
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.
Function WSProtocol.validate_upgrade refactored with the following changes:
- Ensure constant in comparison is on the right (
flip-comparison)
| if self.opcode == self.OPCODE_CLOSE: | ||
| payload = self.payload | ||
| length = self.length | ||
| if not length: | ||
| self.close_reason = CloseReason.NO_STATUS_RCVD | ||
| elif length == 1: | ||
| raise ProtocolError(f'Invalid close frame: {self} {payload}') | ||
| if self.opcode != self.OPCODE_CLOSE: | ||
| return | ||
| payload = self.payload | ||
| length = self.length | ||
| if not length: | ||
| self.close_reason = CloseReason.NO_STATUS_RCVD | ||
| elif length == 1: | ||
| raise ProtocolError(f'Invalid close frame: {self} {payload}') | ||
| else: | ||
| code = unpack_H(payload[:2])[0] | ||
| if code < MIN_CLOSE_REASON or code > MAX_CLOSE_REASON: | ||
| raise ProtocolError('invalid close code range') | ||
| try: | ||
| code = CloseReason(code) | ||
| except ValueError: | ||
| pass | ||
| if code in LOCAL_ONLY_CLOSE_REASONS: | ||
| raise ProtocolError('remote CLOSE with local-only reason') | ||
| if (not isinstance(code, CloseReason) | ||
| and code <= MAX_PROTOCOL_CLOSE_REASON): | ||
| raise ProtocolError('CLOSE with unknown reserved code') | ||
| try: | ||
| reason = payload[2:].decode('utf-8') | ||
| except UnicodeDecodeError: | ||
| raise ProtocolError( | ||
| 'close reason is not valid UTF-8', | ||
| CloseReason.INVALID_FRAME_PAYLOAD_DATA, | ||
| ) | ||
| if isinstance(code, CloseReason): | ||
| code.reason = reason | ||
| else: | ||
| code = unpack_H(payload[:2])[0] | ||
| if code < MIN_CLOSE_REASON or code > MAX_CLOSE_REASON: | ||
| raise ProtocolError('invalid close code range') | ||
| try: | ||
| code = CloseReason(code) | ||
| except ValueError: | ||
| pass | ||
| if code in LOCAL_ONLY_CLOSE_REASONS: | ||
| raise ProtocolError('remote CLOSE with local-only reason') | ||
| if (not isinstance(code, CloseReason) | ||
| and code <= MAX_PROTOCOL_CLOSE_REASON): | ||
| raise ProtocolError('CLOSE with unknown reserved code') | ||
| try: | ||
| reason = payload[2:].decode('utf-8') | ||
| except UnicodeDecodeError: | ||
| raise ProtocolError( | ||
| 'close reason is not valid UTF-8', | ||
| CloseReason.INVALID_FRAME_PAYLOAD_DATA, | ||
| ) | ||
| if isinstance(code, CloseReason): | ||
| code.reason = reason | ||
| else: | ||
| code = (code, reason) | ||
| self.close_reason = code | ||
| code = (code, reason) | ||
| self.close_reason = code |
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.
Function Frame.prepare refactored with the following changes:
- Add guard clause (
last-if-guard)
| self.outbound_transmission_id = 1 | ||
| else: | ||
| self.outbound_transmission_id = 2 | ||
| self.outbound_transmission_id = 1 if initiative else 2 |
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.
Function ContextManager.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| routes[method.full_name] = method | ||
| # js/lua pb lib will format as '.service.method' | ||
| routes['.' + method.full_name] = method | ||
| routes[f'.{method.full_name}'] = method |
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.
Function Router.include_service refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| elif not mdp.client_streaming and mdp.server_streaming: | ||
| elif not mdp.client_streaming: | ||
| method_class = UnaryStreamMethod | ||
| elif mdp.client_streaming and not mdp.server_streaming: | ||
| elif not mdp.server_streaming: | ||
| method_class = StreamUnaryMethod | ||
| elif mdp.client_streaming and mdp.server_streaming: | ||
| method_class = StreamStreamMethod | ||
| else: | ||
| assert False, 'should be one of above' | ||
|
|
||
| method_class = StreamStreamMethod | ||
| request_class = service.GetRequestClass(method) | ||
| response_class = service.GetResponseClass(method) | ||
| method_ins = method_class( | ||
| yield method_class( |
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.
Function PBRouter.get_service_methods refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if) - Inline variable that is immediately yielded (
inline-immediately-yielded-variable)
| elif not mdp.client_streaming and mdp.server_streaming: | ||
| elif not mdp.client_streaming: | ||
| method_class = UnaryStreamMethodStub | ||
| elif mdp.client_streaming and not mdp.server_streaming: | ||
| elif not mdp.server_streaming: | ||
| method_class = StreamUnaryMethodStub | ||
| elif mdp.client_streaming and mdp.server_streaming: | ||
| method_class = StreamStreamMethodStub | ||
| else: | ||
| assert False, 'should be one of above' | ||
|
|
||
| method_class = StreamStreamMethodStub | ||
| request_class = stub.GetRequestClass(method) | ||
| response_class = stub.GetResponseClass(method) | ||
| method_stub = method_class( | ||
| yield method_class( |
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.
Function PBRouterStub.get_router_stubs refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if) - Inline variable that is immediately yielded (
inline-immediately-yielded-variable)
| raise ValueError('Not a cached Python file extension', ext) | ||
| # Should we look for .pyw files? | ||
| return basename + '.py' | ||
| return f'{basename}.py' |
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.
Function source_from_cache refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| PY3 = True | ||
| else: | ||
| PY3 = False | ||
| PY3 = sys.version_info[0] >= 3 |
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.
Lines 48-51 refactored with the following changes:
- Simplify boolean if expression (
boolean-if-exp-identity) - Replace if statement with if expression (
assign-if-exp)
| if not isinstance(other, HashNode): | ||
| return NotImplemented | ||
| return self.hashed_key == other.hashed_key | ||
| return ( | ||
| self.hashed_key == other.hashed_key | ||
| if isinstance(other, HashNode) | ||
| else NotImplemented | ||
| ) |
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.
Function HashNode.__eq__ refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| offset = hash_func('cat' + key) % entry_count | ||
| skip = (hash_func('lee' + key) % (entry_count - 1)) + 1 | ||
| offset = hash_func(f'cat{key}') % entry_count | ||
| skip = hash_func(f'lee{key}') % (entry_count - 1) + 1 |
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.
Function MaglevHash.rehash refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| key = self.hash_func('cat' + key) | ||
| key = self.hash_func(f'cat{key}') |
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.
Function MaglevHash.get_node refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| deadline = get_running_loop().time() + delay # type: Optional[float] | ||
| else: | ||
| deadline = None | ||
| deadline = get_running_loop().time() + delay if delay is not None else None |
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.
Function timeout refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
This removes the following comments ( why? ):
# type: Optional[float]
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
developbranch, then run: