Skip to content

Commit 3c8c561

Browse files
Add stubs for httpretty (#16048)
1 parent 2bbf550 commit 3c8c561

8 files changed

Lines changed: 285 additions & 0 deletions

File tree

stubs/httpretty/METADATA.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version = "1.1.4"
2+
upstream-repository = "https://github.com/gabrielfalcao/HTTPretty"
3+
partial-stub = true
4+
5+
[tool.stubtest]
6+
ignore-missing-stub = true
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Final
2+
3+
from .core import (
4+
EmptyRequestHeaders as EmptyRequestHeaders,
5+
Entry as Entry,
6+
HTTPrettyRequest as HTTPrettyRequest,
7+
HTTPrettyRequestEmpty as HTTPrettyRequestEmpty,
8+
URIInfo as URIInfo,
9+
URIMatcher as URIMatcher,
10+
get_default_thread_timeout as get_default_thread_timeout,
11+
httprettified as httprettified,
12+
httprettized as httprettized,
13+
httpretty as httpretty,
14+
set_default_thread_timeout as set_default_thread_timeout,
15+
)
16+
from .errors import HTTPrettyError as HTTPrettyError, UnmockedError as UnmockedError
17+
18+
__version__: Final[str]
19+
HTTPretty = httpretty
20+
activate = httprettified
21+
enabled = httprettized
22+
enable = httpretty.enable
23+
register_uri = httpretty.register_uri
24+
disable = httpretty.disable
25+
is_enabled = httpretty.is_enabled
26+
reset = httpretty.reset
27+
Response = httpretty.Response
28+
GET: Final = "GET"
29+
PUT: Final = "PUT"
30+
POST: Final = "POST"
31+
DELETE: Final = "DELETE"
32+
HEAD: Final = "HEAD"
33+
PATCH: Final = "PATCH"
34+
OPTIONS: Final = "OPTIONS"
35+
CONNECT: Final = "CONNECT"
36+
37+
def last_request() -> HTTPrettyRequest | HTTPrettyRequestEmpty: ...
38+
def latest_requests() -> list[HTTPrettyRequest]: ...
39+
def has_request() -> bool: ...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import TypeVar
2+
3+
_T = TypeVar("_T")
4+
5+
class BaseClass: ...
6+
7+
def encode_obj(in_obj: _T) -> _T: ...

stubs/httpretty/httpretty/core.pyi

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import re
2+
from collections.abc import Callable, Iterable, Mapping
3+
from contextlib import AbstractContextManager
4+
from http.client import HTTPMessage
5+
from types import TracebackType
6+
from typing import Any, Protocol, TypeAlias, overload, type_check_only
7+
from typing_extensions import ParamSpec, TypeVar
8+
9+
from .http import HttpBaseClass, _HTTPMethod
10+
11+
_P = ParamSpec("_P")
12+
_R = TypeVar("_R")
13+
14+
@type_check_only
15+
class _WritableFileobj(Protocol):
16+
def write(self, b: bytes, /) -> object: ...
17+
def seek(self, offset: int, /) -> object: ...
18+
19+
_URI: TypeAlias = str | re.Pattern[str]
20+
_HeaderValue: TypeAlias = str | int | bool | None
21+
_Headers: TypeAlias = Mapping[str, _HeaderValue]
22+
_Body: TypeAlias = str | bytes
23+
_ResponseBody: TypeAlias = _Body | Callable[[HTTPrettyRequest, str, _Headers], tuple[int, _Headers, _Body]]
24+
25+
def set_default_thread_timeout(timeout: float) -> None: ...
26+
def get_default_thread_timeout() -> float: ...
27+
28+
class HTTPrettyRequest(HttpBaseClass):
29+
headers: HTTPMessage
30+
raw_headers: str
31+
path: str
32+
querystring: dict[str, list[str]]
33+
parsed_body: Any # It can be any object after parsing raw (str) body
34+
created_at: float
35+
def __init__(
36+
self, headers: str | bytes, body: _Body = "", sock: object | None = None, path_encoding: str = "iso-8859-1"
37+
) -> None: ...
38+
@property
39+
def method(self) -> str: ...
40+
@property
41+
def protocol(self) -> str: ...
42+
43+
@property
44+
def body(self) -> str: ...
45+
@body.setter
46+
def body(self, value: _Body) -> None: ...
47+
48+
@property
49+
def url(self) -> str: ...
50+
@property
51+
def host(self) -> str: ...
52+
def parse_querystring(self, qs: str) -> dict[str, list[str]]: ...
53+
def parse_request_body(self, body: str) -> Any: ... # Any object can be returned if deserialization is successful
54+
55+
class EmptyRequestHeaders(dict[str, str]): ...
56+
57+
class HTTPrettyRequestEmpty:
58+
method: str | None
59+
url: str | None
60+
body: str
61+
headers: EmptyRequestHeaders
62+
63+
class Entry(HttpBaseClass):
64+
method: _HTTPMethod
65+
uri: str
66+
request: HTTPrettyRequest
67+
body: _Body
68+
status: int
69+
streaming: bool
70+
adding_headers: dict[str, str]
71+
forcing_headers: dict[str, str]
72+
def __init__(
73+
self,
74+
method: str,
75+
uri: str,
76+
body: _ResponseBody,
77+
adding_headers: _Headers | None = None,
78+
forcing_headers: _Headers | None = None,
79+
status: int = 200,
80+
streaming: bool = False,
81+
**headers: str,
82+
) -> None: ...
83+
def validate(self) -> None: ...
84+
def normalize_headers(self, headers: _Headers) -> dict[str, str]: ...
85+
def fill_filekind(self, fk: _WritableFileobj) -> None: ...
86+
87+
class URIInfo(HttpBaseClass):
88+
default_str_attrs: tuple[str, ...]
89+
username: str
90+
password: str
91+
hostname: str
92+
port: int
93+
path: str
94+
query: str
95+
scheme: str
96+
fragment: str
97+
last_request: HTTPrettyRequest | None
98+
def __init__(
99+
self,
100+
username: str = "",
101+
password: str = "",
102+
hostname: str = "",
103+
port: int = 80,
104+
path: str = "/",
105+
query: str = "",
106+
fragment: str = "",
107+
scheme: str = "",
108+
last_request: HTTPrettyRequest | None = None,
109+
) -> None: ...
110+
def to_str(self, attrs: Iterable[str]) -> str: ...
111+
def str_with_query(self) -> str: ...
112+
def full_url(self, use_querystring: bool = True) -> str: ...
113+
def get_full_domain(self) -> str: ...
114+
@classmethod
115+
def from_uri(cls, uri: str, entry: Entry) -> URIInfo: ...
116+
117+
class URIMatcher:
118+
regex: re.Pattern[str] | None
119+
info: URIInfo | None
120+
entries: list[Entry]
121+
priority: int
122+
uri: _URI
123+
def __init__(self, uri: _URI, entries: Iterable[Entry], match_querystring: bool = False, priority: int = 0) -> None: ...
124+
def matches(self, info: URIInfo) -> bool: ...
125+
def get_next_entry(self, method: _HTTPMethod, info: URIInfo, request: HTTPrettyRequest) -> Entry: ...
126+
127+
class httpretty(HttpBaseClass):
128+
METHODS: tuple[_HTTPMethod, ...]
129+
latest_requests: list[HTTPrettyRequest]
130+
last_request: HTTPrettyRequest | HTTPrettyRequestEmpty
131+
allow_net_connect: bool
132+
@classmethod
133+
def match_uriinfo(cls, info: URIInfo) -> tuple[Entry | None, list[str]]: ...
134+
@classmethod
135+
def match_https_hostname(cls, hostname: str) -> bool: ...
136+
@classmethod
137+
def match_http_address(cls, hostname: str, port: int) -> bool: ...
138+
@classmethod
139+
def record(
140+
cls,
141+
filename: str,
142+
indentation: int = 4,
143+
encoding: str = "utf-8",
144+
verbose: bool = False,
145+
allow_net_connect: bool = True,
146+
# Passed to urllib3.PoolManager as kwargs, and connection pool's parameters have various types
147+
pool_manager_params: Mapping[str, Any] | None = None,
148+
) -> AbstractContextManager[None]: ...
149+
@classmethod
150+
def playback(cls, filename: str, allow_net_connect: bool = True, verbose: bool = False) -> AbstractContextManager[None]: ...
151+
@classmethod
152+
def reset(cls) -> None: ...
153+
@classmethod
154+
def historify_request(cls, headers: str | bytes, body: _Body = "", sock: object | None = None) -> HTTPrettyRequest: ...
155+
@classmethod
156+
def register_uri(
157+
cls,
158+
method: str,
159+
uri: _URI,
160+
body: _ResponseBody = '{"message": "HTTPretty :)"}',
161+
adding_headers: _Headers | None = None,
162+
forcing_headers: _Headers | None = None,
163+
status: int = 200,
164+
responses: Iterable[Entry] | None = None,
165+
match_querystring: bool = False,
166+
priority: int = 0,
167+
**headers: str,
168+
) -> None: ...
169+
@classmethod
170+
def Response(
171+
cls,
172+
body: _ResponseBody,
173+
method: _HTTPMethod | None = None,
174+
uri: str | None = None,
175+
adding_headers: _Headers | None = None,
176+
forcing_headers: _Headers | None = None,
177+
status: int = 200,
178+
streaming: bool = False,
179+
**headers: str,
180+
) -> Entry: ...
181+
@classmethod
182+
def disable(cls) -> None: ...
183+
@classmethod
184+
def is_enabled(cls) -> bool: ...
185+
@classmethod
186+
def enable(cls, allow_net_connect: bool = True, verbose: bool = False) -> None: ...
187+
188+
class httprettized:
189+
allow_net_connect: bool
190+
verbose: bool
191+
def __init__(self, allow_net_connect: bool = True, verbose: bool = False) -> None: ...
192+
def __enter__(self) -> None: ...
193+
def __exit__(
194+
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None
195+
) -> None: ...
196+
197+
@overload
198+
def httprettified(test: Callable[_P, _R]) -> Callable[_P, _R]: ...
199+
@overload
200+
def httprettified(
201+
test: None = None, allow_net_connect: bool = True, verbose: bool = False
202+
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class HTTPrettyError(Exception): ...
2+
3+
class UnmockedError(HTTPrettyError):
4+
def __init__(self, message: str = ..., request: object | None = None, address: object | None = None) -> None: ...

stubs/httpretty/httpretty/http.pyi

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from _typeshed import SupportsLenAndGetItem
2+
from typing import Final, Literal, TypeAlias
3+
from typing_extensions import TypeVar
4+
5+
_T = TypeVar("_T", str, bytes)
6+
_HTTPMethod: TypeAlias = Literal["GET", "PUT", "POST", "DELETE", "HEAD", "PATCH", "OPTIONS", "CONNECT"]
7+
8+
STATUSES: dict[int, str]
9+
10+
class HttpBaseClass:
11+
GET: Final = "GET"
12+
PUT: Final = "PUT"
13+
POST: Final = "POST"
14+
DELETE: Final = "DELETE"
15+
HEAD: Final = "HEAD"
16+
PATCH: Final = "PATCH"
17+
OPTIONS: Final = "OPTIONS"
18+
CONNECT: Final = "CONNECT"
19+
METHODS: tuple[_HTTPMethod, ...]
20+
21+
def parse_requestline(s: str) -> tuple[str, str, str]: ...
22+
def last_requestline(sent_data: SupportsLenAndGetItem[_T]) -> _T | None: ...
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def utf8(s: str | bytes) -> bytes: ...
2+
def decode_utf8(s: str | bytes) -> str: ...
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Final
2+
3+
version: Final[str]

0 commit comments

Comments
 (0)