Mercurial > hg
view mercurial/urllibcompat.py @ 51891:ad83e4f9b40e
typing: correct pytype mistakes in `mercurial/vfs.py`
With the previous changes in this series (prior to merging the *.pyi file), this
wasn't too bad- the only definitively wrong things were the `data` argument to
`writelines()`, and the return type on `backgroundclosing()` (both of these
errors were dropped in the previous commit; for some reason pytype doesn't like
`contextlib._GeneratorContextManager`, even though that's what it determined it
is):
File "/mnt/c/Users/Matt/hg/mercurial/vfs.py", line 411, in abstractvfs:
Bad return type 'contextlib._GeneratorContextManager' for generator function abstractvfs.backgroundclosing [bad-yield-annotation]
Expected Generator, Iterable or Iterator
PyCharm thinks this is `Generator[backgroundfilecloser], Any, None]`, which can
be reduced to `Iterator[backgroundfilecloser]`, but pytype flagged the line that
calls `yield` without an argument unless it's also `Optional`. PyCharm is happy
either way. For some reason, `Iterable` didn't work for pytype:
File "/mnt/c/Users/Matt/hg/mercurial/vfs.py", line 390, in abstractvfs:
Function contextlib.contextmanager was called with the wrong arguments [wrong-arg-types]
Expected: (func: Callable[[Any], Iterator])
Actually passed: (func: Callable[[Any, Any, Any], Iterable[Optional[Any]]])
Attributes of protocol Iterator[_T_co] are not implemented on Iterable[Optional[Any]]: __next__
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 20 Sep 2024 16:36:28 -0400 |
parents | f4733654f144 |
children |
line wrap: on
line source
# urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3 # # Copyright 2017 Google, Inc. # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. from __future__ import annotations import http.server import urllib.error import urllib.parse import urllib.request import urllib.response from . import pycompat _sysstr = pycompat.sysstr class _pycompatstub: def __init__(self): self._aliases = {} def _registeraliases(self, origin, items): """Add items that will be populated at the first access""" items = map(_sysstr, items) self._aliases.update( (item.replace('_', '').lower(), (origin, item)) for item in items ) def _registeralias(self, origin, attr, name): """Alias ``origin``.``attr`` as ``name``""" self._aliases[_sysstr(name)] = (origin, _sysstr(attr)) def __getattr__(self, name): try: origin, item = self._aliases[name] except KeyError: raise AttributeError(name) self.__dict__[name] = obj = getattr(origin, item) return obj httpserver = _pycompatstub() urlreq = _pycompatstub() urlerr = _pycompatstub() urlreq._registeraliases( urllib.parse, ( b"splitattr", b"splitpasswd", b"splitport", b"splituser", b"urlparse", b"urlunparse", ), ) urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs") urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl") urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote") urlreq._registeraliases( urllib.request, ( b"AbstractHTTPHandler", b"BaseHandler", b"build_opener", b"FileHandler", b"FTPHandler", b"ftpwrapper", b"HTTPCookieProcessor", b"HTTPHandler", b"HTTPSHandler", b"install_opener", b"pathname2url", b"HTTPBasicAuthHandler", b"HTTPDigestAuthHandler", b"HTTPPasswordMgrWithDefaultRealm", b"ProxyHandler", b"Request", b"url2pathname", b"urlopen", ), ) urlreq._registeraliases( urllib.response, ( b"addclosehook", b"addinfourl", ), ) urlerr._registeraliases( urllib.error, ( b"HTTPError", b"URLError", ), ) httpserver._registeraliases( http.server, ( b"HTTPServer", b"BaseHTTPRequestHandler", b"SimpleHTTPRequestHandler", b"CGIHTTPRequestHandler", ), ) # urllib.parse.quote() accepts both str and bytes, decodes bytes # (if necessary), and returns str. This is wonky. We provide a custom # implementation that only accepts bytes and emits bytes. def quote(s, safe='/'): # bytestr has an __iter__ that emits characters. quote_from_bytes() # does an iteration and expects ints. We coerce to bytes to appease it. if isinstance(s, pycompat.bytestr): s = bytes(s) s = urllib.parse.quote_from_bytes(s, safe=safe) return s.encode('ascii', 'strict') # urllib.parse.urlencode() returns str. We use this function to make # sure we return bytes. def urlencode(query, doseq=False): s = urllib.parse.urlencode(query, doseq=doseq) return s.encode('ascii') urlreq.quote = quote urlreq.urlencode = urlencode def getfullurl(req): return req.full_url def gethost(req): return req.host def getselector(req): return req.selector def getdata(req): return req.data def hasdata(req): return req.data is not None