mypy: Add type hints - #477
Conversation
|
I think found a typo in your stubs- You define a Protocol here: https://github.com/python/typeshed/blob/main/stubs/WebOb/webob/cookies.pyi#L29-L31 class _Serializer(Protocol):
def loads(self, appstruct: Any, /) -> bytes: ...
def dumps(self, bstruct: bytes, /) -> Any: ...I think that should read: class _Serializer(Protocol):
def dumps(self, appstruct: Any, /) -> bytes: ...
def loads(self, bstruct: bytes, /) -> Any: ...It's basically just swapping the function names. Notice how the input/output are reversed of the other functions: https://github.com/python/typeshed/blob/main/stubs/WebOb/webob/cookies.pyi#L96-L104 class JSONSerializer:
def dumps(self, appstruct: Any) -> bytes: ...
def loads(self, bstruct: bytes | str) -> Any: ...
class Base64Serializer:
serializer: _Serializer
def __init__(self, serializer: _Serializer | None = None) -> None: ...
def dumps(self, appstruct: Any) -> bytes: ...
def loads(self, bstruct: bytes) -> Any: ...Note that https://github.com/Pylons/webob/blob/main/src/webob/cookies.py#L705-L706 And https://github.com/Pylons/webob/blob/main/src/webob/cookies.py#L813-L814 |
|
@jvanasco Good catch. Feel free to open an issue or PR on typeshed. |
|
Following up from #457, which I closed in favour of this PR — thanks for putting this together, having the typeshed stubs merged back inline is a big step. One gap worth flagging: this PR adds Harmless today, since nothing reads our inline types. With All six fail the same way. Plain The part that makes it easy to miss: our own CI won't catch it. Nothing for you to do about the With #497 applied on top of this branch, the same consumer file is clean under both pyright and One thing I would like to ask of you here, though: the It makes more sense on this branch than on #497, which has no mypy dependency to hang it off — so I've left it out of that PR. Note it'll need |
Adds `BaseResponse` to `__all__` to match future expectations. Resolves merge conflicts.
|
I think I've made all of the changes you asked for and resolved the merge conflicts. I went ahead and already added |
parse_date, parse_date_delta, serialize_date, serialize_date_delta and timedelta_to_seconds moved to webob.datetime_utils in 0.9.7 (0196b09) and have been re-exported from webob for backwards compatibility ever since. They were never documented and never listed in webob.__all__. Import them from webob.datetime_utils instead. The UTC tzinfo object and the year/month/week/day/hour/minute/second timedelta constants are unaffected and remain importable from webob. Also add BaseRequest to webob.__all__. It was already importable from webob, but its absence from __all__ means type checkers reject "from webob import BaseRequest" once WebOb ships a py.typed marker, as proposed in Pylons#477. See Pylons#457
Closes: #474
My curiosity got the better of me. So this is the stubs I contributed to typeshed automatically merged into the source using
merge_pyi.Afterwards I cleaned up any mistakes and completed the type hints for the internal API. I added a couple of type tests, but we could definitely add more.
I tried to keep changes to the actual code to a bare minimum. I did however change a couple of minor things. Like remove an unnecessary check for Python 2.7
You can do with these what you want, the diff is fairly large, so it will take some time to review.