Skip to content

Commit 2eb305f

Browse files
committed
Add stubs for HTTPretty
1 parent a75102f commit 2eb305f

9 files changed

Lines changed: 300 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing_extensions import assert_type
2+
3+
import httpretty
4+
5+
6+
@httpretty.activate(allow_net_connect=False)
7+
def decorated() -> int:
8+
httpretty.register_uri(httpretty.GET, "https://example.com/", body="ok", status=200, content_type="text/plain")
9+
assert_type(httpretty.last_request(), httpretty.HTTPrettyRequest | httpretty.HTTPrettyRequestEmpty)
10+
assert_type(httpretty.latest_requests(), list[httpretty.HTTPrettyRequest])
11+
assert_type(httpretty.has_request(), bool)
12+
return 1
13+
14+
15+
with httpretty.enabled(allow_net_connect=False):
16+
response = httpretty.Response("ok", status=201)
17+
assert_type(response, httpretty.Entry)

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

0 commit comments

Comments
 (0)