Skip to content

Refactor header exception logic into get_safe method - #779

Closed
cool-RR wants to merge 2 commits into
python-websockets:masterfrom
cool-RR:2020-06-13-get-single
Closed

cool-RR wants to merge 2 commits into
python-websockets:masterfrom
cool-RR:2020-06-13-get-single

Conversation

@cool-RR

@cool-RR cool-RR commented Jun 13, 2020 •

Copy link
Copy Markdown
Contributor

While doing the raise from fixes, I noticed that this logic can be refactored. Let's see whether the tests pass, and whether you find this code more elegant than the existing code :)

@cool-RR
cool-RR force-pushed the 2020-06-13-get-single branch 2 times, most recently from 0128379 to ce1e1d3 Compare June 13, 2020 20:15
@cool-RR
cool-RR force-pushed the 2020-06-13-get-single branch from ce1e1d3 to ba9e901 Compare June 13, 2020 20:19
@codecov

codecov Bot commented Jun 13, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #779 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #779   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           29        29           
  Lines         4388      4381    -7     
  Branches       406       404    -2     
=========================================
- Hits          4388      4381    -7     
Impacted Files Coverage Δ
src/websockets/handshake.py 100.00% <100.00%> (ø)
src/websockets/http.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 017a072...ba9e901. Read the comment docs.

@cool-RR
cool-RR marked this pull request as ready for review June 13, 2020 20:22
Comment thread src/websockets/http.py
return copy

def get_safe(self, header: str) -> str:
from .exceptions import InvalidHeader

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess you've had to do this to avoid an import cycle. This is a very common issue since I implemented static type checking with mypy.

The websockets.exceptions module is imported at the bottom at websockets.http, so you remove this import use websockets.exceptions.InvalidHeader.

It's ugly but I'd rather have a module-level import at the bottom of a module than scattered imports inside functions.

But if you're moving the code to websockets.handshake, the problem goes away.

Comment thread src/websockets/http.py
copy._list = self._list.copy()
return copy

def get_safe(self, header: str) -> str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The only difference between get_safe and __getitem__ is that get_safe translates KeyError or MultipleValuesError into InvalidHeader. Having two methods that do the same thing in the same class is weird.

websockets.http.Headers is intended as a low-level utility that only knows about HTTP. This is why it uses websockets.http.MultipleValuesError instead of one of the websockets-specific exceptions in websockets.exceptions.

To factor out this behavior, I'd write a utility function in websockets.handshake. The job of this function would be to raise an exception from websockets.exceptions and to provide a good error message when the WebSocket protocol specifies that a given header must be provided exactly once, but it's there zero or multiple times.

You can call it get_unique_header for example.

@aaugustin

Copy link
Copy Markdown
Member

Also, I resumed working on #676, and I may ask you to rebase on top of #676 once it's done rather than the other way round, because the changes are much larger over there than here.

@aaugustin

Copy link
Copy Markdown
Member

I'm going to close this for now — we lost momentum — but I'm open to a PR implementing https://github.com/aaugustin/websockets/pull/779/files#r439800454

@aaugustin aaugustin closed this Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants