Skip to content

Commit 2330c05

Browse files
authored
Fix typing3 (#58)
Finalizing the release. * typing * removed unused imports * misc
1 parent 4fb2b51 commit 2330c05

File tree

7 files changed

+50
-29
lines changed

7 files changed

+50
-29
lines changed

.github/workflows/python-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ jobs:
3737
- name: Install dependencies
3838
run: |
3939
python -m pip install --upgrade pip
40-
pip install --upgrade tox setuptools flake8 pytest
41-
pip list
40+
pip install --upgrade tox setuptools flake8 pytest mypy
41+
pip freeze
4242
- name: Test with pytest
4343
run: |
4444
tox -e py -- ${{ matrix.pytest-args }}

CHANGELOG.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1.0.0 (unreleased)
1+
1.0.0
22

33
IMPORTANT
44

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Users are advised to pin their installations to "metadata_parser<{MINOR +1}"
2626

2727
For example:
2828

29-
* if the current release is: `0.10.6`
30-
* the advised pin is: `metadata_parser<0.11`
29+
* if the current release is: `1.0.0`
30+
* the advised pin is: `metadata_parser<1.1.0`
3131

3232
PATCH releases will usually be bug fixes and new features that support backwards
3333
compatibility with Public Methods. Private Methods are not guaranteed to be

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
long_description=long_description,
5959
classifiers=[
6060
"Intended Audience :: Developers",
61-
"License :: OSI Approved :: MIT License",
6261
"Programming Language :: Python :: 3",
6362
"Programming Language :: Python :: 3.7",
6463
"Programming Language :: Python :: 3.8",

src/metadata_parser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# ==============================================================================
7070

7171

72-
__VERSION__ = "1.0.0dev"
72+
__VERSION__ = "1.0.0"
7373

7474

7575
# ------------------------------------------------------------------------------

src/metadata_parser/requests_extensions.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
import cgi # noqa: I202
55
import logging
66
import socket
7+
from typing import Any
78
from typing import Optional
89
from typing import Tuple
910
from typing import TYPE_CHECKING
1011

1112
# pypi
12-
import requests
1313
from requests_toolbelt.utils.deprecated import get_encodings_from_content
1414

1515
# local
1616
from . import config
1717
from .exceptions import AllowableError
18-
from .utils import DummyResponse
1918
from .utils import safe_sample
2019

2120
if TYPE_CHECKING:
@@ -35,16 +34,17 @@
3534
# peername hacks
3635
# only use for these stdlib packages
3736
# eventually will not be needed thanks to upstream changes in `requests`
37+
_compatible_sockets: Tuple[Any, ...]
3838
try:
39-
_compatible_sockets: Tuple = (
39+
_compatible_sockets = (
4040
_socket.socket,
4141
socket._socketobject, # type: ignore[attr-defined]
4242
)
4343
except AttributeError:
44-
_compatible_sockets: Tuple = (_socket.socket,) # type: ignore[no-redef]
44+
_compatible_sockets = (_socket.socket,) # type: ignore[no-redef]
4545

4646

47-
def derive_encoding__hook(resp: "TYPES_RESPONSE", *args, **kwargs) -> None:
47+
def derive_encoding__hook(resp: Any, *args, **kwargs) -> None:
4848
"""
4949
a note about `requests`
5050
@@ -58,14 +58,14 @@ def derive_encoding__hook(resp: "TYPES_RESPONSE", *args, **kwargs) -> None:
5858
servers to not follow RFC and for the default encoding to be different.
5959
"""
6060
if TYPE_CHECKING:
61-
assert hasattr(resp, "_encoding_fallback")
6261
assert hasattr(resp, "_encoding_content")
62+
assert hasattr(resp, "_encoding_fallback")
6363
assert hasattr(resp, "_encoding_headers")
6464

65+
resp._encoding_content = None
6566
resp._encoding_fallback = config.ENCODING_FALLBACK
6667
# modified version, returns `None` if no charset available
6768
resp._encoding_headers = get_encoding_from_headers(resp.headers)
68-
resp._encoding_content = None
6969
if not resp._encoding_headers and resp.content:
7070
# html5 spec requires a meta-charset in the first 1024 bytes
7171
_sample = safe_sample(resp.content)
@@ -120,7 +120,9 @@ def get_encoding_from_headers(headers: "CaseInsensitiveDict") -> Optional[str]:
120120
# ------------------------------------------------------------------------------
121121

122122

123-
def get_response_peername(resp: "TYPES_RESPONSE") -> Optional["TYPES_PEERNAME"]:
123+
def get_response_peername(
124+
resp: Any,
125+
) -> Optional["TYPES_PEERNAME"]:
124126
"""
125127
used to get the peername (ip+port) data from the request
126128
if a socket is found, caches this onto the request object
@@ -133,17 +135,19 @@ def get_response_peername(resp: "TYPES_RESPONSE") -> Optional["TYPES_PEERNAME"]:
133135
134136
* _mp_peername
135137
"""
136-
if not isinstance(resp, requests.Response) and not isinstance(resp, DummyResponse):
137-
# raise AllowableError("Not a HTTPResponse")
138-
log.debug("Not a supported HTTPResponse | %s", resp)
139-
log.debug("-> received a type of: %s", type(resp))
140-
return None
138+
# if not isinstance(resp, Response) and not isinstance(resp, DummyResponse):
139+
# # raise AllowableError("Not a HTTPResponse")
140+
# log.debug("Not a supported HTTPResponse | %s", resp)
141+
# log.debug("-> received a type of: %s", type(resp))
142+
# return None
141143

142144
if hasattr(resp, "_mp_peername"):
143145
return resp._mp_peername
144146

145147
def _get_socket() -> Optional[socket.socket]:
146-
if isinstance(resp, DummyResponse):
148+
# only socket to `requests.Response`
149+
# if not isinstance(resp, "Response"):
150+
if not hasattr(resp, "raw"):
147151
return None
148152
i = 0
149153
while True:
@@ -168,14 +172,14 @@ def _get_socket() -> Optional[socket.socket]:
168172
pass
169173
return None
170174

175+
_mp_peername: Optional["TYPES_PEERNAME"] = None
171176
sock = _get_socket()
172-
if sock:
177+
if sock is not None:
173178
# only cache if we have a sock
174179
# we may want/need to call again
175-
resp._mp_peername = sock.getpeername() # type: ignore [union-attr]
176-
else:
177-
resp._mp_peername = None # type: ignore [union-attr]
178-
return resp._mp_peername # type: ignore [union-attr]
180+
_mp_peername = sock.getpeername()
181+
setattr(resp, "_mp_peername", _mp_peername) # type: ignore[union-attr]
182+
return _mp_peername
179183

180184

181185
# ------------------------------------------------------------------------------

src/metadata_parser/typing.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66
from typing import Tuple
77
from typing import TYPE_CHECKING
8+
from typing import TypeVar
89
from typing import Union
910

1011
# pypi
@@ -13,13 +14,17 @@
1314
if TYPE_CHECKING:
1415
from urllib.parse import ParseResult
1516

16-
import requests
17+
from requests import Response
1718

18-
from . import DummyResponse
1919
from . import ResponseHistory
20+
from .utils import DummyResponse
21+
22+
# from requests.structures import CaseInsensitiveDict
2023

2124
# ==============================================================================
2225

26+
T = TypeVar("T")
27+
2328
# TYPE_ENCODER = Callable[[str, Optional[str]], str] # def encode(value, strategy)
2429
TYPE_ENCODER = Callable[
2530
[str, Optional[str]], Union[str, Dict]
@@ -30,9 +35,22 @@
3035
TYPE_URL_FETCH = Tuple[str, str, "ResponseHistory"]
3136
TYPE_URLPARSE = Callable[[str], "ParseResult"]
3237
TYPES_PEERNAME = Tuple[str, int] # (ip, port)
33-
TYPES_RESPONSE = Union["DummyResponse", "requests.Response"]
38+
TYPES_RESPONSE = Union["Response", "DummyResponse", T]
3439
TYPES_STRATEGY = Union[List[str], str, None]
3540

3641

42+
"""
43+
# TYPES_RESPONSE_EXTENDED = Union["TYPES_RESPONSE", "_SupportsContent", "T"]
44+
class _SupportsContent(Protocol):
45+
46+
_encoding_content: Optional[str]
47+
_encoding_fallback: str
48+
_encoding_headers: Optional[str]
49+
content: str
50+
encoding: Optional[str]
51+
headers: "CaseInsensitiveDict"
52+
"""
53+
54+
3755
class _UrlParserCacheable(Protocol):
3856
urlparse: TYPE_URLPARSE

0 commit comments

Comments
 (0)