sbordeyne / requests_har

MIT License
1 stars 3 forks source link

[deprecate-cgi] get charset without using cgi #5

Closed jeffhung closed 2 months ago

jeffhung commented 2 months ago

Instead of the deprecated cgi.parse_header(), use email.message.EmailMessage to parse Content-Type header and extract charset.

The Problem

See PEP 594, the cgi module is deprecated since version 3.11 and will be removed in version 3.13.

Thus creating the following lint error:

$ make lint
poetry run pylint requests_har
************* Module requests_har.har
requests_har/har.py:9:0: W4901: Deprecated module 'cgi' (deprecated-module)

------------------------------------------------------------------
Your code has been rated at 9.94/10 (previous run: 9.94/10, +0.00)

make: *** [lint] Error 4

And test warning:

$ make test
poetry run pytest
======================================================= test session starts ========================================================
platform darwin -- Python 3.11.6, pytest-7.4.4, pluggy-1.5.0
rootdir: /Users/jeff_hung/wc/NoteBrainer/requests_har
plugins: pylama-8.4.1
collected 1 item

tests/test_har.py .                                                                                                          [100%]

========================================================= warnings summary =========================================================
requests_har/har.py:9
  /Users/jeff_hung/wc/NoteBrainer/requests_har/requests_har/har.py:9: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
    from cgi import parse_header

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================== 1 passed, 1 warning in 0.16s ===================================================

The mitigation, as suggested in cgi.parse_header() document, is to use email.message.EmailMessage instead.

See also:

After Fix

$ make lint
poetry run pylint requests_har

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

$ make test
poetry run pytest
======================================================= test session starts ========================================================
platform darwin -- Python 3.11.6, pytest-7.4.4, pluggy-1.5.0
rootdir: /Users/jeff_hung/wc/NoteBrainer/requests_har
plugins: pylama-8.4.1
collected 1 item

tests/test_har.py .                                                                                                          [100%]

======================================================== 1 passed in 0.15s =========================================================
jeffhung commented 2 months ago

This PR fixes #4.