Commit 39de3442 authored by Simon Chabot's avatar Simon Chabot
Browse files

fix(registry): use TypedDict when python version >= 3.8, otherwise a Dict

- TypedDict is provided by the typing module for python >= 3.8
- TypedDict is provided by the typing_extensions module for typing_extensions >=
  3.7.4

but this module is not available as debian buster package.

- mypy does not support condition types defined like that:

```
if CONDITION:
    type = Dict
else:
    type = TypedDict
```

and therefore it's not possible to conditionally declare the type.

but… it does support checks with python version or system platform (cf:
https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks).

Let's import TypedDict from typing when it's available (python >= 3.8), and use
it. Otherwise, let's use a simple dict.

related #9
parent df0288f41e6f
Pipeline #27956 passed with stages
in 2 minutes and 4 seconds
......@@ -93,7 +93,12 @@ from logging import getLogger
from warnings import warn
from typing import List, Tuple, Any, Iterable, Callable
from types import ModuleType
from typing_extensions import TypedDict
if sys.version_info >= (3, 8):
from typing import TypedDict
# TypedDict is also available in typing_extension > 3.7.4,
# but this package is not available on debian buster.
from logilab.common.modutils import modpath_from_file
from logilab.common.logging_ext import set_log_methods
......@@ -478,19 +483,22 @@ class RegistrableInstance(RegistrableObject):
super(RegistrableInstance, self).__init__()
SelectBestReport = TypedDict(
"SelectBestReport",
{
"all_objects": List,
"end_score": int,
"winners": List,
"winner": Optional[Any],
"self": "Registry",
"args": List,
"kwargs": Dict,
"registry": "Registry",
},
)
if sys.version_info >= (3, 8):
SelectBestReport = TypedDict(
"SelectBestReport",
{
"all_objects": List,
"end_score": int,
"winners": List,
"winner": Optional[Any],
"self": "Registry",
"args": List,
"kwargs": Dict,
"registry": "Registry",
},
)
else:
SelectBestReport = Dict[str, Union[List, int, Optional[Any], "Registry", Dict]]
class Registry(dict):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment