-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add stubs for WebTest #14541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daverball
wants to merge
5
commits into
python:main
Choose a base branch
from
Daverball:webtest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add stubs for WebTest #14541
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
50015ba
Add stubs for WebTest
Daverball 0968386
Fix typo in comment [skip ci]
Daverball 3e12bf3
Improves some types in `app.pyi` based on review
Daverball 2818de2
Switches to `beautifulsoup4` from `types-beautifulsoup4` in requirements
Daverball a5bd1a6
Merge branch 'main' into webtest
Daverball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# error: failed to find stub | ||
# ========================== | ||
# These modules have been migrated to external packages | ||
# and emit an `ImportError` if people try to use the | ||
# functions/classes defined within | ||
webtest.ext | ||
webtest.sel | ||
# Compatibility/utility modules for internal use that didn't | ||
# seem worth including in the stubs | ||
webtest.compat | ||
webtest.lint | ||
webtest.utils | ||
|
||
# error: variable differs from runtime type | ||
# ========================================= | ||
# Even though this can be `None`, it never should be during | ||
# normal use of WebTest, so it seems more pragmatic to treat | ||
# it as always non-`None` | ||
webtest.response.TestResponse.request | ||
webtest.TestResponse.request |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
version = "3.0.*" | ||
upstream_repository = "https://github.com/Pylons/webtest" | ||
requires = ["beautifulsoup4", "types-waitress", "types-WebOb"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from webtest.app import AppError as AppError, TestApp as TestApp, TestRequest as TestRequest | ||
from webtest.forms import ( | ||
Checkbox as Checkbox, | ||
Field as Field, | ||
Form as Form, | ||
Hidden as Hidden, | ||
Radio as Radio, | ||
Select as Select, | ||
Submit as Submit, | ||
Text as Text, | ||
Textarea as Textarea, | ||
Upload as Upload, | ||
) | ||
from webtest.response import TestResponse as TestResponse |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
import json | ||
from _typeshed import SupportsItems, SupportsKeysAndGetItem | ||
from _typeshed.wsgi import WSGIApplication, WSGIEnvironment | ||
from collections.abc import Iterable, Sequence | ||
from http.cookiejar import CookieJar, DefaultCookiePolicy | ||
from typing import Any, Generic, Literal, TypeVar | ||
from typing_extensions import TypeAlias | ||
|
||
from webob.request import BaseRequest | ||
from webtest.forms import File, Upload | ||
from webtest.response import TestResponse | ||
|
||
# NOTE: While it is possible to pass different kinds of values depending on | ||
# the exact configuration of the request, it seems more robust to | ||
# restrict them to the types that are supported by all code paths. | ||
# I don't expect anyone to try to pass different kinds of values | ||
# in a non-JSON request. | ||
_ParamValue: TypeAlias = File | Upload | int | bytes | str | ||
_Params: TypeAlias = SupportsItems[str | bytes, _ParamValue] | Sequence[tuple[str | bytes, _ParamValue]] | ||
# NOTE: Using `Collection` rather than `Iterable` would probably be slightly | ||
# safer since WebTest will check this parameter for truthyness. But since | ||
# objects are truthy by default, this should only lead to issues in truly | ||
# exotic cases. | ||
_ExtraEnviron: TypeAlias = SupportsKeysAndGetItem[str, Any] | Iterable[tuple[str, Any]] | ||
_Files: TypeAlias = Sequence[tuple[str, str] | tuple[str, str, bytes]] | ||
_AppT = TypeVar("_AppT", bound=WSGIApplication, default=WSGIApplication) | ||
|
||
__all__ = ["TestApp", "TestRequest"] | ||
|
||
class AppError(Exception): | ||
def __init__(self, message: str, *args: object) -> None: ... | ||
|
||
class CookiePolicy(DefaultCookiePolicy): ... | ||
|
||
class TestRequest(BaseRequest): | ||
ResponseClass: type[TestResponse] | ||
__test__: Literal[False] | ||
|
||
class TestApp(Generic[_AppT]): | ||
RequestClass: type[TestRequest] | ||
app: _AppT | ||
lint: bool | ||
relative_to: str | None | ||
extra_environ: WSGIEnvironment | ||
use_unicode: bool | ||
cookiejar: CookieJar | ||
JSONEncoder: json.JSONEncoder | ||
__test__: Literal[False] | ||
def __init__( | ||
self, | ||
app: _AppT, | ||
# NOTE: this extra_environ is different from the others and needs to | ||
# support __delitem__, it seems easiest to just treat this like | ||
# a regular WSGIEnvironment. The docs also say that this should | ||
# be a dictionary. | ||
extra_environ: WSGIEnvironment | None = None, | ||
relative_to: str | None = None, | ||
use_unicode: bool = True, | ||
cookiejar: CookieJar | None = None, | ||
parser_features: Sequence[str] | str | None = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. More instances below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches what beautifulsoup4 expects for the |
||
json_encoder: json.JSONEncoder | None = None, | ||
lint: bool = True, | ||
) -> None: ... | ||
def get_authorization(self) -> tuple[str, str | tuple[str, str]]: ... | ||
def set_authorization(self, value: tuple[str, str | tuple[str, str]]) -> None: ... | ||
@property | ||
def authorization(self) -> tuple[str, str | tuple[str, str]]: ... | ||
@authorization.setter | ||
def authorization(self, value: tuple[str, str | tuple[str, str]]) -> None: ... | ||
@property | ||
def cookies(self) -> dict[str, str | None]: ... | ||
def set_cookie(self, name: str, value: str | None) -> None: ... | ||
def reset(self) -> None: ... | ||
def set_parser_features(self, parser_features: Sequence[str] | str) -> None: ... | ||
def get( | ||
self, | ||
url: str, | ||
params: _Params | str | None = None, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def post( | ||
self, | ||
url: str, | ||
params: _Params | str = "", | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
upload_files: _Files | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def put( | ||
self, | ||
url: str, | ||
params: _Params | str = "", | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
upload_files: _Files | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def patch( | ||
self, | ||
url: str, | ||
params: _Params | str = "", | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
upload_files: _Files | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def delete( | ||
self, | ||
url: str, | ||
params: _Params | str = "", | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def options( | ||
self, | ||
url: str, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def head( | ||
self, | ||
url: str, | ||
params: _Params | str | None = None, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def post_json( | ||
self, | ||
url: str, | ||
params: Any = ..., | ||
*, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def put_json( | ||
self, | ||
url: str, | ||
params: Any = ..., | ||
*, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def patch_json( | ||
self, | ||
url: str, | ||
params: Any = ..., | ||
*, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def delete_json( | ||
self, | ||
url: str, | ||
params: Any = ..., | ||
*, | ||
headers: dict[str, str] | None = None, | ||
extra_environ: _ExtraEnviron | None = None, | ||
status: int | str | None = None, | ||
expect_errors: bool = False, | ||
content_type: str | None = None, | ||
xhr: bool = False, | ||
) -> TestResponse: ... | ||
def encode_multipart(self, params: Iterable[tuple[str | bytes, _ParamValue]], files: _Files) -> tuple[str, bytes]: ... | ||
def request( | ||
self, url_or_req: str | TestRequest, status: int | str | None = None, expect_errors: bool = False, **req_params: Any | ||
) -> TestResponse: ... | ||
def do_request( | ||
self, req: TestRequest, status: int | str | None = None, expect_errors: bool | None = None | ||
) -> TestResponse: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from _typeshed import StrOrBytesPath | ||
from _typeshed.wsgi import StartResponse, WSGIEnvironment | ||
from collections.abc import Iterable | ||
from typing import TypedDict | ||
from typing_extensions import Unpack | ||
|
||
class _DebugAppParams(TypedDict, total=False): | ||
form: StrOrBytesPath | bytes | None | ||
show_form: bool | ||
|
||
__all__ = ["DebugApp", "make_debug_app"] | ||
|
||
class DebugApp: | ||
form: bytes | None | ||
show_form: bool | ||
def __init__(self, form: StrOrBytesPath | bytes | None = None, show_form: bool = False) -> None: ... | ||
def __call__(self, environ: WSGIEnvironment, start_response: StartResponse) -> Iterable[bytes]: ... | ||
|
||
debug_app: DebugApp | ||
|
||
def make_debug_app(global_conf: object, **local_conf: Unpack[_DebugAppParams]) -> DebugApp: ... |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that
Sequence
is not a protocol, using a real protocol (fromcollections.abc
or_typeshed
) would be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered using
Collection
, the minimum requirement appears to beIterable & Sized
at first glance, but since the docs and error message saylist
I wanted to be a little more conservative, in case some implementation details change, without making it annoying to pass in lists that contain a type that's compatible withtuple[str, str] | tuple[str, str, bytes]
.