Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make HTTPError instantiable. #1379

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

Closed
wants to merge 3 commits into from
Closed

Conversation

carljm
Copy link
Member

@carljm carljm commented Jun 1, 2017

Fixes #1377.

Demonstration of fix (../typeshed is a clone of typeshed with this branch checked out):

$ cat httperror.py 
from io import StringIO
from urllib.error import HTTPError

http_error = HTTPError("", 500, "message", dict(), StringIO(''))

$ mypy httperror.py 
httperror.py:4: error: Cannot instantiate abstract class 'HTTPError' with abstract attributes '__enter__', '__exit__', ... and 'writelines' (16 methods suppressed)

$ mypy --custom-typeshed-dir ../typeshed/ httperror.py 

def __enter__(self) -> BinaryIO: ...
def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException],
# TODO: traceback should be TracebackType but that's defined in types
traceback: Optional[Any]) -> bool: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TracebackType is already imported, so it should be fine to use.

@carljm
Copy link
Member Author

carljm commented Jun 1, 2017

Good point! Just copied that TODO from BinaryIO. Updated both locations to just use TracebackType.

@@ -404,6 +404,41 @@ class BinaryIO(IO[bytes]):
@abstractmethod
def __enter__(self) -> BinaryIO: ...

class _ConcreteBinaryIO(BinaryIO):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do this. Instead we should make some changes in io.pyi and inherit from io.BufferedRandom when we need a concrete subclass of BinaryIO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll see what I can put together that works.

@carljm carljm changed the title Make HTTPError instantiable by introducing fictional _ConcreteBinaryIO in typing.pyi. Make HTTPError instantiable by subclassing BufferedRandom instead of BinaryIO. Jun 2, 2017
@carljm
Copy link
Member Author

carljm commented Jun 2, 2017

So it turns out that just having addinfourl inherit BufferedRandom instead of BinaryIO fixes the immediate issue, without any other changes to io.pyi. Is that a workable fix? I looked briefly at the other TODOs in io.pyi, but it seemed like some significant refactoring would be needed to address them and avoid lots of "definition incompatible with base class" errors; doesn't seem like that refactoring needs to be conflated with this issue.

@gvanrossum
Copy link
Member

So if addinfourl() inherits from io.BufferedIO, it's probably not compatible with something that requires a BinaryIO or IO[bytes]. I'm not sure if that's important?

@carljm carljm changed the title Make HTTPError instantiable by subclassing BufferedRandom instead of BinaryIO. Make HTTPError instantiable. Jun 3, 2017
@carljm
Copy link
Member Author

carljm commented Jun 3, 2017

Ugh. Yeah, that probably matters.

It also works to subclass BytesIO (which inherits BinaryIO already) instead of BinaryIO. But that gives it some additional methods from BytesIO that it probably shouldn't have.

I'll see what I can figure out with the io.pyi refactor.

@gvanrossum
Copy link
Member

Thanks, given that you only encountered it once it's not a high priority to get it fixed, so then I'd rather do it right or not at all (you can keep the # type: ignore in the meantime :-).

@JelleZijlstra
Copy link
Member

@carljm any updates?

@gvanrossum
Copy link
Member

gvanrossum commented Jun 22, 2017 via email

@carljm
Copy link
Member Author

carljm commented Jun 22, 2017

I played with this for a bit but didn't get very far. If some of the io.pyi cleanup has been done for me, I'll take another look (probably next week).

@gvanrossum
Copy link
Member

gvanrossum commented Jun 23, 2017 via email

@JelleZijlstra JelleZijlstra mentioned this pull request Jul 11, 2017
25 tasks
@JelleZijlstra
Copy link
Member

This was just reported again, so perhaps we should try to apply a quick fix to make HTTPError instantiable. Perhaps we can just drop the IO base class?

@carljm
Copy link
Member Author

carljm commented Aug 9, 2017

Yeah, sorry... I took a look at this, realized that the io.pyi cleanup had only been done for Py2 and I care about Py3, so would have to figure out how to correctly reapply that cleanup for Py3, and then ran out of time.

@gvanrossum
Copy link
Member

Ping? We should probably just close this if nobody's going to get to it.

@carljm
Copy link
Member Author

carljm commented Mar 14, 2018

I'm not likely to get back to this soon, sorry. It's an unfortunate wart, but it doesn't crop up often (only one instance in our entire codebase AFAIK) and it's easy enough to # type: ignore.

@gvanrossum
Copy link
Member

OK, given the amount of back and forth this has already led to, I'm closing it.

@gvanrossum gvanrossum closed this Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

typeshed annotations do not permit instantiation of urllib.error.HTTPError
3 participants