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__
# hgdemandimport - global demand-loading of modules for Mercurial
#
# Copyright 2017 Facebook Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
'''demandimport - automatic demand-loading of modules'''
# This is in a separate package from mercurial because in Python 3,
# demand loading is per-package. Keeping demandimport in the mercurial package
# would disable demand loading for any modules in mercurial.
from __future__ import annotations
import os
import sys
from . import demandimportpy3 as demandimport
# Full module names which can't be lazy imported.
# Extensions can add to this set.
IGNORES = {
'__future__',
'_hashlib',
# ImportError during pkg_resources/__init__.py:fixup_namespace_package
'_imp',
'_xmlplus',
'fcntl',
'nt', # pathlib2 tests the existence of built-in 'nt' module
'win32com.gen_py',
'win32com.shell', # 'appdirs' tries to import win32com.shell
'_winreg', # 2.7 mimetypes needs immediate ImportError
'pythoncom',
# imported by tarfile, not available under Windows
'pwd',
'grp',
# imported by profile, itself imported by hotshot.stats,
# not available under Windows
'resource',
# this trips up many extension authors
'gtk',
# setuptools' pkg_resources.py expects "from __main__ import x" to
# raise ImportError if x not defined
'__main__',
'_ast', # https://bugs.python.org/issue41631
'_ssl', # conditional imports in the stdlib, issue1964
'_sre', # issue4920
'rfc822',
'mimetools',
'sqlalchemy.events', # has import-time side effects (issue5085)
'sqlalchemy.dialects', # similar problems as above
# setuptools 8 expects this module to explode early when not on windows
'distutils.msvc9compiler',
'__builtin__',
'builtins',
'urwid.command_map', # for pudb
'lzma',
# setuptools uses this hack to inject it's own distutils at import time
'setuptools',
'_distutils_hack.override',
# threading is locally imported by importlib.util.LazyLoader.exec_module
'_weakrefset',
'warnings',
'threading',
}
_pypy = '__pypy__' in sys.builtin_module_names
if _pypy:
# _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
IGNORES.add('_ctypes.pointer')
# pure Python module on PyPy, must be loaded to raise ModuleNotFoundError
# on non-Windows platforms
IGNORES.add('msvcrt')
demandimport.init(IGNORES)
# Re-export.
isenabled = demandimport.isenabled
disable = demandimport.disable
deactivated = demandimport.deactivated
def enable():
# chg pre-imports modules so do not enable demandimport for it
if (
'CHGINTERNALMARK' not in os.environ
and os.environ.get('HGDEMANDIMPORT') != 'disable'
):
demandimport.enable()