Mercurial > hg-stable
changeset 51932:ee7e106b372b
typing: make the localrepo classes known to pytype
9d4ad05bc91c and 1b17309cdaab both mentioned making `bundlerepository` and
`unionrepository` subclass `localrepository` during the type checking phase, but
that didn't apply to pytype in practice. See bcaa5d408657 and friends for how
the zope interfaces confuse pytype, and end up converting the classes they
decorate into `Any`.
This commit is slightly more complex though, because `localrepository` has mixin
classes applied to it when it is instantiated. Specifically, `RevlogFileStorage`
is added, which adds `def file(f)` (which isn't defined on `localrepository`).
Therefore a list of `localrepository` superclasses is provided during type
checking to account for the mixins. Without this, the `bundlerepository` class
gets flagged when it attempts to call its superclass implementation of `file()`.
Note that pytype doesn't understand these mixin superclasses (it marks the
superclass of `localrepository` as `Any`, because they are zope interfaces it
doesn't understand), but that's enough to get it to not flag `bundlerepository`.
PyCharm also stops flagging it as a missing function, though it seems like it is
able to handle the zope interfaces.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 20 Sep 2024 21:31:58 -0400 |
parents | 992fcf6b2473 |
children | 8583d138f436 |
files | mercurial/localrepo.py |
diffstat | 1 files changed, 69 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/localrepo.py Mon Sep 23 14:58:37 2024 -0400 +++ b/mercurial/localrepo.py Fri Sep 20 21:31:58 2024 -0400 @@ -14,6 +14,7 @@ import re import sys import time +import typing import weakref from concurrent import futures @@ -255,8 +256,7 @@ legacycaps = moderncaps.union({b'changegroupsubset'}) -@interfaceutil.implementer(repository.ipeercommandexecutor) -class localcommandexecutor: +class LocalCommandExecutor: def __init__(self, peer): self._peer = peer self._sent = False @@ -301,12 +301,20 @@ self._closed = True -@interfaceutil.implementer(repository.ipeercommands) -class localpeer(repository.peer): +localcommandexecutor = interfaceutil.implementer( + repository.ipeercommandexecutor +)(LocalCommandExecutor) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + localcommandexecutor = LocalCommandExecutor + + +class LocalPeer(repository.peer): '''peer for a local repo; reflects only the most recent API''' def __init__(self, repo, caps=None, path=None, remotehidden=False): - super(localpeer, self).__init__( + super(LocalPeer, self).__init__( repo.ui, path=path, remotehidden=remotehidden ) @@ -456,13 +464,19 @@ # End of peer interface. -@interfaceutil.implementer(repository.ipeerlegacycommands) -class locallegacypeer(localpeer): +localpeer = interfaceutil.implementer(repository.ipeercommands)(LocalPeer) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + localpeer = LocalPeer + + +class LocalLegacyPeer(localpeer): """peer extension which implements legacy methods too; used for tests with restricted capabilities""" def __init__(self, repo, path=None, remotehidden=False): - super(locallegacypeer, self).__init__( + super(LocalLegacyPeer, self).__init__( repo, caps=legacycaps, path=path, remotehidden=remotehidden ) @@ -489,6 +503,14 @@ # End of baselegacywirecommands interface. +locallegacypeer = interfaceutil.implementer(repository.ipeerlegacycommands)( + LocalLegacyPeer +) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + locallegacypeer = LocalLegacyPeer + # Functions receiving (ui, features) that extensions can register to impact # the ability to load repositories with custom requirements. Only # functions defined in loaded extensions are called. @@ -1241,8 +1263,7 @@ return localrepository -@interfaceutil.implementer(repository.ilocalrepositoryfilestorage) -class revlogfilestorage: +class RevlogFileStorage: """File storage when using revlogs.""" def file(self, path): @@ -1257,8 +1278,16 @@ return filelog.filelog(self.svfs, path, try_split=try_split) -@interfaceutil.implementer(repository.ilocalrepositoryfilestorage) -class revlognarrowfilestorage: +revlogfilestorage = interfaceutil.implementer( + repository.ilocalrepositoryfilestorage +)(RevlogFileStorage) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + revlogfilestorage = RevlogFileStorage + + +class RevlogNarrowFileStorage: """File storage when using revlogs and narrow files.""" def file(self, path): @@ -1274,6 +1303,15 @@ ) +revlognarrowfilestorage = interfaceutil.implementer( + repository.ilocalrepositoryfilestorage +)(RevlogNarrowFileStorage) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + revlognarrowfilestorage = RevlogNarrowFileStorage + + def makefilestorage(requirements, features, **kwargs): """Produce a type conforming to ``ilocalrepositoryfilestorage``.""" features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE) @@ -1295,9 +1333,16 @@ (repository.ilocalrepositoryfilestorage, lambda: makefilestorage), ] - -@interfaceutil.implementer(repository.ilocalrepositorymain) -class localrepository: +_localrepo_base_classes = object + +if typing.TYPE_CHECKING: + _localrepo_base_classes = [ + repository.ilocalrepositorymain, + repository.ilocalrepositoryfilestorage, + ] + + +class LocalRepository(_localrepo_base_classes): """Main class for representing local repositories. All local repositories are instances of this class. @@ -3598,6 +3643,15 @@ self._sidedata_computers[kind][category] = (keys, computer, flags) +localrepository = interfaceutil.implementer(repository.ilocalrepositorymain)( + LocalRepository +) + +if typing.TYPE_CHECKING: + # Help pytype by hiding the interface stuff that confuses it. + localrepository = LocalRepository + + def undoname(fn: bytes) -> bytes: base, name = os.path.split(fn) assert name.startswith(b'journal')