Mercurial > hg
changeset 51724:a3dc962cac62
typing: add type hints to `mercurial.policy`
Mostly trivial, but this seems like the logical module to use to inject the
hints from `cext`, `pure`, etc, given that this file has the fallback policy.
This is a first step.
There doesn't appear to be a predefined type for a module in py3.7, so those are
omitted for now.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 20 Jul 2024 17:03:30 -0400 |
parents | 9367571fea21 |
children | bbe59cc5d2e1 |
files | mercurial/policy.py |
diffstat | 1 files changed, 20 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/policy.py Sat Jul 20 01:55:09 2024 -0400 +++ b/mercurial/policy.py Sat Jul 20 17:03:30 2024 -0400 @@ -8,6 +8,14 @@ import os import sys +import typing + +if typing.TYPE_CHECKING: + from typing import ( + Dict, + Optional, + Tuple, + ) # Rules for how modules can be loaded. Values are: # @@ -23,8 +31,8 @@ # By default, fall back to the pure modules so the in-place build can # run without recompiling the C extensions. This will be overridden by # __modulepolicy__ generated by setup.py. -policy = b'allow' -_packageprefs = { +policy: bytes = b'allow' +_packageprefs: "Dict[bytes, Tuple[Optional[str], Optional[str]]]" = { # policy: (versioned package, pure package) b'c': ('cext', None), b'allow': ('cext', 'pure'), @@ -39,7 +47,7 @@ try: from . import __modulepolicy__ # type: ignore - policy = __modulepolicy__.modulepolicy + policy: bytes = __modulepolicy__.modulepolicy except ImportError: pass @@ -48,14 +56,14 @@ # The canonical way to do this is to test platform.python_implementation(). # But we don't import platform and don't bloat for it here. if '__pypy__' in sys.builtin_module_names: - policy = b'cffi' + policy: bytes = b'cffi' # Environment variable can always force settings. if 'HGMODULEPOLICY' in os.environ: - policy = os.environ['HGMODULEPOLICY'].encode('utf-8') + policy: bytes = os.environ['HGMODULEPOLICY'].encode('utf-8') -def _importfrom(pkgname, modname): +def _importfrom(pkgname: str, modname: str): # from .<pkgname> import <modname> (where . is looked through this module) fakelocals = {} pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1) @@ -69,7 +77,7 @@ # keep in sync with "version" in C modules -_cextversions = { +_cextversions: "Dict[Tuple[str, str], int]" = { ('cext', 'base85'): 1, ('cext', 'bdiff'): 3, ('cext', 'mpatch'): 1, @@ -78,7 +86,7 @@ } # map import request to other package or module -_modredirects = { +_modredirects: "Dict[Tuple[str, str], Tuple[str, str]]" = { ('cext', 'charencode'): ('cext', 'parsers'), ('cffi', 'base85'): ('pure', 'base85'), ('cffi', 'charencode'): ('pure', 'charencode'), @@ -86,7 +94,7 @@ } -def _checkmod(pkgname, modname, mod): +def _checkmod(pkgname: str, modname: str, mod) -> None: expected = _cextversions.get((pkgname, modname)) actual = getattr(mod, 'version', None) if actual != expected: @@ -97,7 +105,7 @@ ) -def importmod(modname): +def importmod(modname: str): """Import module according to policy and check API version""" try: verpkg, purepkg = _packageprefs[policy] @@ -118,12 +126,12 @@ return _importfrom(pn, mn) -def _isrustpermissive(): +def _isrustpermissive() -> bool: """Assuming the policy is a Rust one, tell if it's permissive.""" return policy.endswith(b'-allow') -def importrust(modname, member=None, default=None): +def importrust(modname: str, member: "Optional[str]" = None, default=None): """Import Rust module according to policy and availability. If policy isn't a Rust one, this returns `default`.