changeset 51884:f79f98733a5b

vfs: use @abstractmethod instead of homebrewing abstract methods The latter confuses PyCharm after adding more type annotations when, for example, `abstractvfs.rename()` calls `_auditpath()`- the latter unconditionally raised an error, so PyCharm thought the code that came after is unreachable. It also tricked pytype into marking the return type as `Never`, which isn't available until Python 3.11 (outside of `typing_extensions`). This also avoid PyCharm warnings that the call to the superclass constructor was missed (it couldn't be called because it raised an error to prevent instantiation). The statichttprepo module needed to be given an override for one of the abstract methods, so that it can be instantiated. In `abstractvfs`, this method is only called by `rename()`, so I think we can leave this empty. We raise an error in case somebody accidentally calls it in the future- it would have raised this same error prior to this change. I couldn't wrangle `import-checker.py` into accepting importing `ABC` and `abstractmethod`- for each subsequent import, it reports something like: stdlib import "contextlib" follows local import: abc I suspect the problem is that near the `if fullname != '__future__'` check, if the module doesn't fall into the error case, `seenlocal` gets set to the module name. That causes it to be treated like a local module on the next iteration, even though it is in `stdlib_modules`.
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 20 Sep 2024 00:07:39 -0400
parents 1edac12af730
children adbb183c2f27
files mercurial/statichttprepo.py mercurial/vfs.py
diffstat 2 files changed, 17 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/statichttprepo.py	Thu Sep 19 21:03:10 2024 -0400
+++ b/mercurial/statichttprepo.py	Fri Sep 20 00:07:39 2024 -0400
@@ -139,6 +139,9 @@
             f = b"/".join((self.base, urlreq.quote(path)))
             return httprangereader(f, urlopener)
 
+        def _auditpath(self, path: bytes, mode: bytes) -> None:
+            raise NotImplementedError
+
         def join(self, path, *insidef):
             if path:
                 return pathutil.join(self.base, path, *insidef)
--- a/mercurial/vfs.py	Thu Sep 19 21:03:10 2024 -0400
+++ b/mercurial/vfs.py	Fri Sep 20 00:07:39 2024 -0400
@@ -7,6 +7,7 @@
 
 from __future__ import annotations
 
+import abc
 import contextlib
 import os
 import shutil
@@ -46,7 +47,7 @@
         checkandavoid()
 
 
-class abstractvfs:
+class abstractvfs(abc.ABC):
     """Abstract base class; cannot be instantiated"""
 
     # default directory separator for vfs
@@ -57,19 +58,18 @@
     # encoded vfs (see issue6546)
     _dir_sep = b'/'
 
-    def __init__(self, *args, **kwargs):
-        '''Prevent instantiation; don't call this from subclasses.'''
-        raise NotImplementedError('attempted instantiating ' + str(type(self)))
-
     # TODO: type return, which is util.posixfile wrapped by a proxy
+    @abc.abstractmethod
     def __call__(self, path: bytes, mode: bytes = b'rb', **kwargs):
-        raise NotImplementedError
+        ...
 
+    @abc.abstractmethod
     def _auditpath(self, path: bytes, mode: bytes):
-        raise NotImplementedError
+        ...
 
+    @abc.abstractmethod
     def join(self, path: Optional[bytes], *insidef: bytes) -> bytes:
-        raise NotImplementedError
+        ...
 
     def tryread(self, path: bytes) -> bytes:
         '''gracefully return an empty string for missing files'''
@@ -625,7 +625,7 @@
 opener = vfs
 
 
-class proxyvfs(abstractvfs):
+class proxyvfs(abstractvfs, abc.ABC):
     def __init__(self, vfs: "vfs"):
         self.vfs = vfs
 
@@ -684,7 +684,7 @@
         return self.vfs.join(path, *insidef)
 
 
-class closewrapbase:
+class closewrapbase(abc.ABC):
     """Base class of wrapper, which hooks closing
 
     Do not instantiate outside of the vfs layer.
@@ -706,11 +706,13 @@
         self._origfh.__enter__()
         return self
 
+    @abc.abstractmethod
     def __exit__(self, exc_type, exc_value, exc_tb):
-        raise NotImplementedError('attempted instantiating ' + str(type(self)))
+        ...
 
+    @abc.abstractmethod
     def close(self):
-        raise NotImplementedError('attempted instantiating ' + str(type(self)))
+        ...
 
 
 class delayclosedfile(closewrapbase):