More types for synapse.util, part 1 (#10888)

The following modules now pass `disallow_untyped_defs`:

* synapse.util.caches.cached_call 
* synapse.util.caches.lrucache
* synapse.util.caches.response_cache 
* synapse.util.caches.stream_change_cache
* synapse.util.caches.ttlcache pass
* synapse.util.daemonize
* synapse.util.patch_inline_callbacks pass `no-untyped-defs`
* synapse.util.versionstring

Additional typing in synapse.util.metrics. Didn't get this to pass `no-untyped-defs`, think I'll need to watch #10847
This commit is contained in:
David Robertson 2021-10-06 11:20:49 +01:00 committed by GitHub
parent 6744273f0b
commit f8d0f72b27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 134 additions and 73 deletions

View file

@ -15,14 +15,18 @@
import logging
import os
import subprocess
from types import ModuleType
from typing import Dict
logger = logging.getLogger(__name__)
version_cache: Dict[ModuleType, str] = {}
def get_version_string(module) -> str:
def get_version_string(module: ModuleType) -> str:
"""Given a module calculate a git-aware version string for it.
If called on a module not in a git checkout will return `__verison__`.
If called on a module not in a git checkout will return `__version__`.
Args:
module (module)
@ -31,11 +35,13 @@ def get_version_string(module) -> str:
str
"""
cached_version = getattr(module, "_synapse_version_string_cache", None)
if cached_version:
cached_version = version_cache.get(module)
if cached_version is not None:
return cached_version
version_string = module.__version__
# We want this to fail loudly with an AttributeError. Type-ignore this so
# mypy only considers the happy path.
version_string = module.__version__ # type: ignore[attr-defined]
try:
null = open(os.devnull, "w")
@ -97,10 +103,15 @@ def get_version_string(module) -> str:
s for s in (git_branch, git_tag, git_commit, git_dirty) if s
)
version_string = "%s (%s)" % (module.__version__, git_version)
version_string = "%s (%s)" % (
# If the __version__ attribute doesn't exist, we'll have failed
# loudly above.
module.__version__, # type: ignore[attr-defined]
git_version,
)
except Exception as e:
logger.info("Failed to check for git repository: %s", e)
module._synapse_version_string_cache = version_string
version_cache[module] = version_string
return version_string