Skip to content

Move remaining utility functions from _utils.py to _models.py #3387

Merged
lovelydinosaur merged 2 commits intoencode:masterfrom
RafaelWO:change/move-utils-to-models
Nov 1, 2024
Merged

Move remaining utility functions from _utils.py to _models.py #3387
lovelydinosaur merged 2 commits intoencode:masterfrom
RafaelWO:change/move-utils-to-models

Conversation

@RafaelWO
Copy link
Copy Markdown
Contributor

@RafaelWO RafaelWO commented Nov 1, 2024

Summary

Related issue: #3381

This PR moves the following remaining utility functions from _utils.py to _models.py that are only used in _models.py (except to_bytes_or_str):

  • is_known_encoding
  • parse_header_links
  • parse_content_type_charset
  • obfuscate_sensitive_headers

Related tests are also moved accordingly, i.e. from tests/test_utils.py to tests/models/test_headers.py.


Additionally, this PR fixes the script scripts/lint by using ruff check due to the following error:

error: `ruff <path>` has been removed. Use `ruff check <path>` instead.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@RafaelWO RafaelWO force-pushed the change/move-utils-to-models branch from bd63ac9 to faa69b5 Compare November 1, 2024 15:45
@RafaelWO RafaelWO marked this pull request as draft November 1, 2024 16:25
The following functions are moved because they are only used in _models.py
* is_known_encoding
* parse_header_links
* parse_content_type_charset
* obfuscate_sensitive_headers

Related tests are also moved accordingly.
`ruff <path>` has been removed. Use `ruff check <path>` instead.
@RafaelWO RafaelWO force-pushed the change/move-utils-to-models branch from faa69b5 to 81bb8d1 Compare November 1, 2024 16:31
@RafaelWO RafaelWO marked this pull request as ready for review November 1, 2024 16:33
@RafaelWO
Copy link
Copy Markdown
Contributor Author

RafaelWO commented Nov 1, 2024

@tomchristie I first overlooked that I should also move some of the related tests. I did this now 🙂

Comment thread tests/test_utils.py
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm note sure if test_encoded, test_bad_utf_like_encoding, and test_guess_by_bom are related to the moved function is_known_encoding and therefore should be moved into tests related to models?

Copy link
Copy Markdown
Contributor

@lovelydinosaur lovelydinosaur left a comment

Choose a reason for hiding this comment

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

Thanks. ☺️ Prompted one review thought, tho let's get this in.

Comment thread httpx/_models.py
return links


def _obfuscate_sensitive_headers(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is now a good time to review this? Eg does flask provide similar behaviour onto its headers data structures? Does Django?

(Possibly too off topic??)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems that flask does not obfuscate header values for repr (see werkzeug)

>>> from werkzeug.datastructures import Headers
>>>
>>> head = Headers({"authorization": "s3kr3t"})
>>> head
Headers([('authorization', 's3kr3t')])
>>> repr(head)
"Headers([('authorization', 's3kr3t')])"

Same for Django (see HttpHeaders or ResponseHeaders)

>>> from django.http.response import ResponseHeaders
>>> head = ResponseHeaders({"Authorization": "s3kr3t"})
>>> repr(head)
"{'Authorization': 's3kr3t'}"

@lovelydinosaur lovelydinosaur merged commit 41597ad into encode:master Nov 1, 2024
@RafaelWO RafaelWO deleted the change/move-utils-to-models branch November 1, 2024 20:49
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