Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions stubs/httpretty/@tests/test_cases/check_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing_extensions import assert_type

import httpretty


@httpretty.activate(allow_net_connect=False)
def decorated() -> int:
httpretty.register_uri(httpretty.GET, "https://example.com/", body="ok", status=200, content_type="text/plain")
assert_type(httpretty.last_request(), httpretty.HTTPrettyRequest | httpretty.HTTPrettyRequestEmpty)
assert_type(httpretty.latest_requests(), list[httpretty.HTTPrettyRequest])
assert_type(httpretty.has_request(), bool)
return 1


with httpretty.enabled(allow_net_connect=False):
response = httpretty.Response("ok", status=201)
assert_type(response, httpretty.Entry)
6 changes: 6 additions & 0 deletions stubs/httpretty/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = "1.1.4"
upstream-repository = "https://github.com/gabrielfalcao/HTTPretty"
partial-stub = true

[tool.stubtest]
ignore-missing-stub = true
37 changes: 37 additions & 0 deletions stubs/httpretty/httpretty/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from .core import (
EmptyRequestHeaders as EmptyRequestHeaders,
Entry as Entry,
HTTPrettyRequest as HTTPrettyRequest,
HTTPrettyRequestEmpty as HTTPrettyRequestEmpty,
URIInfo as URIInfo,
URIMatcher as URIMatcher,
get_default_thread_timeout as get_default_thread_timeout,
httprettified as httprettified,
httprettized as httprettized,
httpretty as httpretty,
set_default_thread_timeout as set_default_thread_timeout,
)
from .errors import HTTPrettyError as HTTPrettyError, UnmockedError as UnmockedError

__version__: str

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.

Suggested change
__version__: str
__version__: Final[str]

HTTPretty = httpretty
activate = httprettified
enabled = httprettized
enable = httpretty.enable
register_uri = httpretty.register_uri
disable = httpretty.disable
is_enabled = httpretty.is_enabled
reset = httpretty.reset
Response = httpretty.Response
GET: Final = "GET"

Check failure on line 26 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
PUT: Final = "PUT"

Check failure on line 27 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
POST: Final = "POST"

Check failure on line 28 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
DELETE: Final = "DELETE"

Check failure on line 29 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
HEAD: Final = "HEAD"

Check failure on line 30 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
PATCH: Final = "PATCH"

Check failure on line 31 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
OPTIONS: Final = "OPTIONS"

Check failure on line 32 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
CONNECT: Final = "CONNECT"

Check failure on line 33 in stubs/httpretty/httpretty/__init__.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)

def last_request() -> HTTPrettyRequest | HTTPrettyRequestEmpty: ...
def latest_requests() -> list[HTTPrettyRequest]: ...
def has_request() -> bool: ...
7 changes: 7 additions & 0 deletions stubs/httpretty/httpretty/compat.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import TypeVar

_T = TypeVar("_T")

class BaseClass: ...

def encode_obj(in_obj: _T) -> _T: ...
203 changes: 203 additions & 0 deletions stubs/httpretty/httpretty/core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import re
from collections.abc import Callable, Iterable, Mapping
from contextlib import AbstractContextManager
from http.client import HTTPMessage
from types import TracebackType
from typing import Any, Literal, TypeAlias, overload
from typing_extensions import ParamSpec

from .http import HttpBaseClass

_P = ParamSpec("_P")
_HTTPMethod: TypeAlias = Literal["GET", "PUT", "POST", "DELETE", "HEAD", "PATCH", "OPTIONS", "CONNECT"]
_URI: TypeAlias = str | re.Pattern[str]
_HeaderValue: TypeAlias = str | int | bool | None
_Headers: TypeAlias = Mapping[str, _HeaderValue]
_Body: TypeAlias = str | bytes
_ResponseBody: TypeAlias = _Body | Callable[[HTTPrettyRequest, str, _Headers], tuple[int, _Headers, _Body]]

def set_default_thread_timeout(timeout: float) -> None: ...
def get_default_thread_timeout() -> float: ...

class HTTPrettyRequest(HttpBaseClass):
headers: HTTPMessage
raw_headers: str
path: str
querystring: dict[str, list[str]]
parsed_body: Any # It can be any object after parsing raw (str) body
created_at: float
def __init__(
self, headers: str | bytes, body: _Body = "", sock: object | None = None, path_encoding: str = "iso-8859-1"
) -> None: ...
@property
def method(self) -> str: ...
@property
def protocol(self) -> str: ...

@property
def body(self) -> str: ...
@body.setter
def body(self, value: _Body) -> None: ...

@property
def url(self) -> str: ...
@property
def host(self) -> str: ...
def parse_querystring(self, qs: str) -> dict[str, list[str]]: ...
def parse_request_body(self, body: str) -> Any: ...

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.

Let's add some explanatory comments:

Suggested change
def parse_request_body(self, body: str) -> Any: ...
def parse_request_body(self, body: str) -> Any: ... # Any object can be returned if deserialization is successful


class EmptyRequestHeaders(dict[str, str]): ...

class HTTPrettyRequestEmpty:
method: str | None
url: str | None
body: str
headers: EmptyRequestHeaders

class Entry(HttpBaseClass):
method: _HTTPMethod
uri: str
request: HTTPrettyRequest
body: _Body
status: int
streaming: bool
adding_headers: dict[str, str]
forcing_headers: dict[str, str]
def __init__(
self,
method: str,
uri: str,
body: _ResponseBody,
adding_headers: _Headers | None = None,
forcing_headers: _Headers | None = None,
status: int = 200,
streaming: bool = False,
**headers: str,
) -> None: ...
def validate(self) -> None: ...
def normalize_headers(self, headers: _Headers) -> dict[str, str]: ...
def fill_filekind(self, fk: _WritableFileobj) -> None: ...

Check failure on line 79 in stubs/httpretty/httpretty/core.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"_WritableFileobj" is not defined (reportUndefinedVariable)

class URIInfo(HttpBaseClass):
default_str_attrs: tuple[str, ...]
username: str
password: str
hostname: str
port: int
path: str
query: str
scheme: str
fragment: str
last_request: HTTPrettyRequest | None
def __init__(
self,
username: str = "",
password: str = "",
hostname: str = "",
port: int = 80,
path: str = "/",
query: str = "",
fragment: str = "",
scheme: str = "",
last_request: HTTPrettyRequest | None = None,
) -> None: ...
def to_str(self, attrs: Iterable[str]) -> str: ...
def str_with_query(self) -> str: ...
def full_url(self, use_querystring: bool = True) -> str: ...
def get_full_domain(self) -> str: ...
@classmethod
def from_uri(cls, uri: str, entry: Entry) -> URIInfo: ...

class URIMatcher:
regex: re.Pattern[str] | None
info: URIInfo | None
entries: list[Entry]
priority: int
uri: _URI
def __init__(self, uri: _URI, entries: Iterable[Entry], match_querystring: bool = False, priority: int = 0) -> None: ...
def matches(self, info: URIInfo) -> bool: ...
def get_next_entry(self, method: _HTTPMethod, info: URIInfo, request: HTTPrettyRequest) -> Entry: ...

class httpretty(HttpBaseClass):
GET: Final = "GET"

Check failure on line 122 in stubs/httpretty/httpretty/core.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Final" is not defined (reportUndefinedVariable)
PUT: Final = "PUT"
POST: Final = "POST"
DELETE: Final = "DELETE"
HEAD: Final = "HEAD"
PATCH: Final = "PATCH"
OPTIONS: Final = "OPTIONS"
CONNECT: Final = "CONNECT"
METHODS: tuple[_HTTPMethod, ...]
latest_requests: list[HTTPrettyRequest]
last_request: HTTPrettyRequest | HTTPrettyRequestEmpty
allow_net_connect: bool
@classmethod
def match_uriinfo(cls, info: URIInfo) -> tuple[Entry | None, list[str]]: ...
@classmethod
def match_https_hostname(cls, hostname: str) -> bool: ...
@classmethod
def match_http_address(cls, hostname: str, port: int) -> bool: ...
@classmethod
def record(
cls,
filename: str,
indentation: int = 4,
encoding: str = "utf-8",
verbose: bool = False,
allow_net_connect: bool = True,
pool_manager_params: Mapping[str, Any] | None = None,

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.

Another explanatory comment:

Suggested change
pool_manager_params: Mapping[str, Any] | None = None,
# Passed to urllib3.PoolManager as kwargs, and connection pool's parameters have various types
pool_manager_params: Mapping[str, Any] | None = None,

) -> AbstractContextManager[None]: ...
@classmethod
def playback(cls, filename: str, allow_net_connect: bool = True, verbose: bool = False) -> AbstractContextManager[None]: ...
@classmethod
def reset(cls) -> None: ...
@classmethod
def historify_request(cls, headers: str | bytes, body: _Body = "", sock: object | None = None) -> HTTPrettyRequest: ...
@classmethod
def register_uri(
cls,
method: str,
uri: _URI,
body: _ResponseBody = '{"message": "HTTPretty :)"}',
adding_headers: _Headers | None = None,
forcing_headers: _Headers | None = None,
status: int = 200,
responses: Iterable[Entry] | None = None,
match_querystring: bool = False,
priority: int = 0,
**headers: str,
) -> None: ...
@classmethod
def Response(
cls,
body: _ResponseBody,
method: _HTTPMethod | None = None,
uri: str | None = None,
adding_headers: _Headers | None = None,
forcing_headers: _Headers | None = None,
status: int = 200,
streaming: bool = False,
**headers: str,
) -> Entry: ...
@classmethod
def disable(cls) -> None: ...
@classmethod
def is_enabled(cls) -> bool: ...
@classmethod
def enable(cls, allow_net_connect: bool = True, verbose: bool = False) -> None: ...

class httprettized:
allow_net_connect: bool
verbose: bool
def __init__(self, allow_net_connect: bool = True, verbose: bool = False) -> None: ...
def __enter__(self) -> None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None
) -> None: ...

@overload
def httprettified(test: Callable[_P, Any]) -> Callable[_P, Any]: ...
@overload
def httprettified(
test: None = None, allow_net_connect: bool = True, verbose: bool = False
) -> Callable[[Callable[_P, Any]], Callable[_P, Any]]: ...
4 changes: 4 additions & 0 deletions stubs/httpretty/httpretty/errors.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class HTTPrettyError(Exception): ...

class UnmockedError(HTTPrettyError):
def __init__(self, message: str = ..., request: object | None = None, address: object | None = None) -> None: ...
20 changes: 20 additions & 0 deletions stubs/httpretty/httpretty/http.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections.abc import Sequence
from typing_extensions import TypeVar

_T = TypeVar("_T", str, bytes)

STATUSES: dict[int, str]

class HttpBaseClass:
GET: Final = "GET"
PUT: Final = "PUT"
POST: Final = "POST"
DELETE: Final = "DELETE"
HEAD: Final = "HEAD"
PATCH: Final = "PATCH"
OPTIONS: Final = "OPTIONS"
CONNECT: Final = "CONNECT"
METHODS: tuple[_HTTPMethod, ...]

def parse_requestline(s: str) -> tuple[str, str, str]: ...
def last_requestline(sent_data: Sequence[_T]) -> _T | None: ...
2 changes: 2 additions & 0 deletions stubs/httpretty/httpretty/utils.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def utf8(s: str | bytes) -> bytes: ...
def decode_utf8(s: str | bytes) -> str: ...
1 change: 1 addition & 0 deletions stubs/httpretty/httpretty/version.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: str

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.

Suggested change
version: str
from typing import Final
version: Final[str]

Loading