google / google/yapf

method call is incorrectly merged with method definition

Open
#472 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

Note: I _cannot_ provide a reproducible example of this behavior.

I encountered this in 0.18, then upgraded in 0.19 and found the same behavior. But I lost the original example somewhere in my terminal history and cannot reproduce it anymore. Original code was syntactically correct before the reformat, but yapf output wasn't. This happened consistently before I edited the file. Afterwards I made a single-char edit (changed the capitalization of the method name) and the bug was not presenting itself. I then reverted the change to the bug-inducing code again, but it wasn't happening anymore.

I'm opening the bug report anyway in case this sounds worth investigating anyway, or rings any bell.

Culprit code snippet (code is derived from py-amqp):

```
class AsyncConnection(Connection):
async def connect(self):
if self.connected:
return
self.transport = self.Transport(
self.host, self.connect_timeout, self.ssl,
self.read_timeout, self.write_timeout,
socket_settings=self.socket_settings,
)
await self.transport.connect()
self.on_inbound_frame = self.frame_handler_cls(
self, self.on_inbound_method)
self.frame_writer = self.frame_writer_cls(self, self.transport)
while not self._handshake_complete:
await self.drain_events(timeout=self.connect_timeout)

def Transport(self, host, connect_timeout,
ssl=False, read_timeout=None, write_timeout=None,
socket_settings=None, **kwargs):
return AsyncTransport(
host, connect_timeout=connect_timeout, ssl=ssl,
read_timeout=read_timeout, write_timeout=write_timeout,
socket_settings=socket_settings, **kwargs)
```

yapf reformatted it into this syntactically incorrect bit that swallowed a number of lines. Note: this is not the actual output, but the recollection to the best of my memory.

```
class AsyncConnection(Connection):
async def connect(self):
if self.connected:
return
self.transport = self.Transport(
self,
host,
connect_timeout,
ssl=False,
read_timeout=None,
write_timeout=None,
socket_settings=None,
**kwargs
):
return AsyncTransport(
host,
connect_timeout=connect_timeout,
ssl=ssl,
read_timeout=read_timeout,
write_timeout=write_timeout,
socket_settings=socket_settings,
**kwargs
)
```

Using this `.style.yapf`:

```
[style]
based_on_style = pep8
column_limit = 100
split_penalty_for_added_line_split = 300
space_between_ending_comma_and_closing_bracket = false
each_dict_entry_on_separate_line = false
dedent_closing_brackets = true
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.