Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/bundlerepo.py @ 51907:9d4ad05bc91c
typing: make `bundlerepository` subclass `localrepository` while type checking
Currently, `mercurial/bundlerepo.py` is excluded from pytype, mostly because it
complains that various `ui` and `vfs` fields in `localrepository` are missing.
(`bundlerepository` dynamically subclasses `localrepository` when it is
instantiated, so it works at runtime.) This makes that class hierarchy known to
pytype.
Having a protocol for `Repository` is probably the right thing to do, but that
will be a lot of work and this still reflects the class at runtime. Subclassing
also has the benefit of making sure any method overrides have a matching
signature, so maybe this is a situation where we do both of these things. (I'm
not sure how clear the diagnostics are if a class *almost* implements a
protocol, but is missing a method argument or similar.) The subclassing is not
done outside of type checking runs to avoid any side effects on already complex
code.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Wed, 18 Sep 2024 17:50:57 -0400 |
parents | f4733654f144 |
children | cfd30df0f8e4 |
comparison
equal
deleted
inserted
replaced
51906:db7dbe6f7bb2 | 51907:9d4ad05bc91c |
---|---|
14 from __future__ import annotations | 14 from __future__ import annotations |
15 | 15 |
16 import contextlib | 16 import contextlib |
17 import os | 17 import os |
18 import shutil | 18 import shutil |
19 import typing | |
19 | 20 |
20 from .i18n import _ | 21 from .i18n import _ |
21 from .node import ( | 22 from .node import ( |
22 hex, | 23 hex, |
23 nullrev, | 24 nullrev, |
285 for chunk in iter(lambda: cgunpacker.deltachunk(None), {}): | 286 for chunk in iter(lambda: cgunpacker.deltachunk(None), {}): |
286 pass | 287 pass |
287 return filespos | 288 return filespos |
288 | 289 |
289 | 290 |
290 class bundlerepository: | 291 _bundle_repo_baseclass = object |
292 | |
293 if typing.TYPE_CHECKING: | |
294 _bundle_repo_baseclass = localrepo.localrepository | |
295 | |
296 | |
297 class bundlerepository(_bundle_repo_baseclass): | |
291 """A repository instance that is a union of a local repo and a bundle. | 298 """A repository instance that is a union of a local repo and a bundle. |
292 | 299 |
293 Instances represent a read-only repository composed of a local repository | 300 Instances represent a read-only repository composed of a local repository |
294 with the contents of a bundle file applied. The repository instance is | 301 with the contents of a bundle file applied. The repository instance is |
295 conceptually similar to the state of a repository after an | 302 conceptually similar to the state of a repository after an |