[PATCH 3 of 3 yams] [mypy] uses typed dict in __init__.py

Laurent Peuch cortex at worlddomination.be
Tue Feb 25 23:39:57 CET 2020


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1582646033 -3600
#      Tue Feb 25 16:53:53 2020 +0100
# Node ID 97a3dc17efbe757e6237f48f37c80dd34e591145
# Parent  49f89e7d679234e122e17938bfd5cf8db35c2d54
# Available At https://hg.logilab.org/users/lpeuch/yams
#              hg pull https://hg.logilab.org/users/lpeuch/yams -r 97a3dc17efbe
[mypy] uses typed dict in __init__.py

diff --git a/yams/__init__.py b/yams/__init__.py
--- a/yams/__init__.py
+++ b/yams/__init__.py
@@ -21,6 +21,7 @@ import warnings
 from datetime import datetime, date
 
 from typing import Set, Type, Dict, Tuple, Union, Callable, Any, cast, Iterable, TypeVar
+from typing_extensions import TypedDict
 
 import pkg_resources
 
@@ -32,7 +33,13 @@ from yams.types import _RdefType
 
 __docformat__: str = "restructuredtext en"
 
-_PermissionsType = Dict[str, Tuple[str, ...]]
+
+class _PermissionsType(TypedDict, total=False):
+    read: Tuple[str, ...]
+    update: Tuple[str, ...]
+    delete: Tuple[str, ...]
+    add: Tuple[str, ...]
+
 
 __version__: str = pkg_resources.get_distribution('yams').version
 
@@ -67,10 +74,21 @@ DEFAULT_COMPUTED_ATTRPERMS: _Permissions
                                                 'add': (),
                                                 'update': ()}
 
+
+class _CurrentDateOrDatetimeConstructorType(TypedDict, total=False):
+    NOW: Callable[[], datetime]
+    TODAY: Callable[[], date]
+
+
+class _KeywordMapType(TypedDict):
+    Datetime: _CurrentDateOrDatetimeConstructorType
+    TZDatetime: _CurrentDateOrDatetimeConstructorType
+    Date: _CurrentDateOrDatetimeConstructorType
+
+
 # This provides a way to specify callable objects as default values
 # First level is the final type, second is the keyword to callable map
-_current_date_or_datetime_constructor_type = Callable[[], Union[date, datetime]]
-KEYWORD_MAP: Dict[str, Dict[str, _current_date_or_datetime_constructor_type]] = {
+KEYWORD_MAP: _KeywordMapType = {
     'Datetime': {'NOW': datetime.now,
                  'TODAY': datetime.today},
     'TZDatetime': {'NOW': datetime.utcnow,
@@ -103,7 +121,8 @@ def convert_default_value(rdef: _RdefTyp
         # including things that look like keywords for other base types
         if rdef.object in KEYWORD_MAP:
             try:
-                return KEYWORD_MAP[rdef.object][default.upper()]()
+                # mypy doesn't seems able to detect the if just before
+                return KEYWORD_MAP[rdef.object][default.upper()]()  # type: ignore
             except KeyError:
                 # the default was likely not a special constant
                 # like TODAY but some literal



More information about the cubicweb-devel mailing list