Skip to content

[mypy] fix small folders #4292

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

Merged
merged 15 commits into from
Mar 23, 2021
Merged
2 changes: 1 addition & 1 deletion file_transfer/receive_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
print("Receiving data...")
while True:
data = sock.recv(1024)
print(f"data={data}")
print(f"data={str(data)}")
Copy link
Member

@cclauss cclauss Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Suggested change
print(f"data={str(data)}")
print(f"{data = }")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and similar changes are redundant as string interpolation converts the object to string using the __str__ method of the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why but without it we get the following mypy-errors:
file_transfer/send_file.py:16: error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
file_transfer/receive_file.py:16: error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it has something to do with the method from the socket-package that returns the data-object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes vs. str is the most time-consuming part of porting code from Python 2 to Python 3.
% python3

>>> type(b"abc")
<class 'bytes'>
>>> type("abc")
<class 'str'>

if not data:
break
out_file.write(data) # Write data to a file
Expand Down
2 changes: 1 addition & 1 deletion file_transfer/send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def send_file(filename: str = "mytext.txt", testing: bool = False) -> None:
conn, addr = sock.accept() # Establish connection with client.
print(f"Got connection from {addr}")
data = conn.recv(1024)
print(f"Server received {data}")
print(f"Server received {str(data)}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"Server received {str(data)}")
print(f"Server received: {data = }")


with open(filename, "rb") as in_file:
data = in_file.read(1024)
Expand Down