-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
10270: add annotation to twisted.web.http.Request.getHeader
#1669
Conversation
twisted.web.http.Request.getHeader
twisted.web.http.Request.getHeader
@@ -1051,7 +1051,7 @@ def unregisterProducer(self): | |||
|
|||
# The following is the public interface that people should be | |||
# writing to. | |||
def getHeader(self, key): | |||
def getHeader(self, key: AnyStr) -> Optional[AnyStr]: |
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 think this might need overloads to successfully capture that the type of the output matches the type of the input. (A str
key causes a Optional[str]
return value.)
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.
AnyStr
is a TypeVar, so I think this should be fine as is. See https://docs.python.org/3/library/typing.html?highlight=anystr#typing.AnyStr (and note the same is done in the definition of getRawHeaders()
.
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.
Ah, I hadn't realized AnyStr
didn't allow mixing. Seems perfect then! 👍
Adding the stub for `getHeader` means that mypy isn't looking at twisted's implementation to deduce what types instance attributes have. So let's add those to the stub. I don't want to maintain a stub out-of-tree forever, but I've submitted the `getHeader` annotation upstream in twisted/twisted#1669 .
Thank you all! |
Scope and purpose
Another drive-by contribution. Similar to #1668.
Contributor Checklist:
tox -e lint
to format my patch to meet the Twisted Coding Standard#
character).readBody
as per the docstring #1668.review
to the keywords field in Trac, and putting a link to this PR in the comment; it shows up in https://twisted.reviews/ now.The first line is automatically generated by GitHub based on PR ID and branch name.
The other lines generated by GitHub should be replaced.