-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Created pyi for SimpleHTTPServer [Python 2.7] #1186
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Stubs for SimpleHTTPServer (Python 2) | ||
|
||
from typing import Any, AnyStr, IO, Mapping, Optional, Union | ||
import BaseHTTPServer | ||
from StringIO import StringIO | ||
|
||
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | ||
server_version = ... # type: str | ||
def do_GET(self) -> None: ... | ||
def do_HEAD(self) -> None: ... | ||
def send_head(self) -> Optional[IO[str]]: ... | ||
def list_directory(self, path: AnyStr) -> Optional[StringIO]: ... | ||
def translate_path(self, path: AnyStr) -> str: ... | ||
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. The return value should also be AnyStr. In general, a single AnyStr doesn't make sense, because the point of the AnyStr type variable is to constrain two types to be the same (either str or unicode). As far as I'm aware, using only AnyStr in a signature is meaningful only when it's the type argument to a generic class like List. In that case, 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. That makes sense. I'm still wrapping my head around TypeVars, I guess. I keep forgetting that it binds to only one type of string and isn't the same as a Union. |
||
def copyfile(self, source: IO[AnyStr], outputfile: IO[AnyStr]): ... | ||
def guess_type(self, path: Union[str, unicode]) -> str: ... | ||
extensions_map = ... # type: Mapping[str, str] |
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.
This argument should be a Union. listdir takes AnyStr because its return value depends on the type passed in, but that's not true for this function.
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.
Got it.
Petition to rename
AnyStr
toExactlyOneStr
?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 kind of agree. I'm not sure AnyStr should have been added to typing, since it's frequently misunderstood and it's really much more common that you want
Union[str, unicode]
.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.
TypeVar(str, unicode)
certainly has its uses where you don't want to mix string types together, so it's really just a naming issue. Or it doesn't need to have been a default part of typing, since it's not actually something special.I'd rather have
AnyStr = Union[str, unicode]
andEitherStr = TypeVar(str, unicode)
. Kind of hard to distill "exactly one of str or unicode" into a pithy variable name, unfortunately.