core: add Protocol/TypedDict to compat

This commit is contained in:
Dima Gerasimov 2022-06-02 12:43:02 +01:00 committed by karlicoss
parent 186f561018
commit 3faebdd629
4 changed files with 51 additions and 42 deletions

View file

@ -1,8 +1,8 @@
''' '''
Some backwards compatibility stuff/deprecation helpers Some backwards compatibility stuff/deprecation helpers
''' '''
import sys
from types import ModuleType from types import ModuleType
from typing import Callable
from . import warnings from . import warnings
from .common import LazyLogger from .common import LazyLogger
@ -49,22 +49,6 @@ def _get_dal(cfg, module_name: str):
return import_module(f'my.config.repos.{module_name}.dal') return import_module(f'my.config.repos.{module_name}.dal')
import sys
from typing import TYPE_CHECKING
if sys.version_info[:2] >= (3, 8):
from typing import Literal
else:
if TYPE_CHECKING:
from typing_extensions import Literal
else:
# erm.. I guess as long as it's not crashing, whatever...
class _Literal:
def __getitem__(self, args):
pass
Literal = _Literal()
import os import os
windows = os.name == 'nt' windows = os.name == 'nt'
@ -103,3 +87,39 @@ else:
return property(functools.lru_cache(maxsize=1)(f)) # type: ignore return property(functools.lru_cache(maxsize=1)(f)) # type: ignore
del Cl del Cl
del R del R
from typing import TYPE_CHECKING
if sys.version_info[:2] >= (3, 8):
from typing import Literal
else:
if TYPE_CHECKING:
from typing_extensions import Literal
else:
# erm.. I guess as long as it's not crashing, whatever...
class _Literal:
def __getitem__(self, args):
pass
Literal = _Literal()
if sys.version_info[:2] >= (3, 8):
from typing import Protocol
else:
if TYPE_CHECKING:
from typing_extensions import Protocol # type: ignore[misc]
else:
# todo could also use NamedTuple?
Protocol = object
if sys.version_info[:2] >= (3, 8):
from typing import TypedDict
else:
if TYPE_CHECKING:
from typing_extensions import TypedDict # type: ignore[misc]
else:
from typing import Dict
TypedDict = Dict

View file

@ -1,16 +1,9 @@
from my.core import __NOT_HPI_MODULE__ from my.core import __NOT_HPI_MODULE__
from datetime import datetime from typing import Iterator, Optional
from typing import Iterator, Optional, TYPE_CHECKING
if TYPE_CHECKING: from my.core.compat import Protocol
try: from my.core import datetime_aware
from typing import Protocol
except ImportError:
# requirement of mypy
from typing_extensions import Protocol # type: ignore[misc]
else:
Protocol = object
class Thread(Protocol): class Thread(Protocol):
@ -26,7 +19,7 @@ class Message(Protocol):
def id(self) -> str: ... def id(self) -> str: ...
@property @property
def dt(self) -> datetime: ... def dt(self) -> datetime_aware: ...
@property @property
def text(self) -> Optional[str]: ... def text(self) -> Optional[str]: ...

View file

@ -2,22 +2,13 @@
This defines Protocol classes, which make sure that each different This defines Protocol classes, which make sure that each different
type of shared models have a standardized interface type of shared models have a standardized interface
""" """
from my.core import __NOT_HPI_MODULE__
from typing import Dict, Any, Set, Iterator, TYPE_CHECKING from typing import Set, Iterator
from itertools import chain from itertools import chain
from my.core.common import datetime_aware from my.core.compat import Protocol
from my.core import datetime_aware, Json
Json = Dict[str, Any]
if TYPE_CHECKING:
try:
from typing import Protocol
except ImportError:
# requirement of mypy
from typing_extensions import Protocol # type: ignore[misc]
else:
Protocol = object
# common fields across all the Protocol classes, so generic code can be written # common fields across all the Protocol classes, so generic code can be written

View file

@ -5,7 +5,11 @@ minversion = 3.5
toxworkdir={env:TOXWORKDIR_BASE:}{toxinidir}/.tox toxworkdir={env:TOXWORKDIR_BASE:}{toxinidir}/.tox
[testenv] [testenv]
passenv = CI CI_* passenv =
# useful for tests to know they are running under ci
CI CI_*
# respect user's cache dirs to prevent tox from crapping into project dir
MYPY_CACHE_DIR PYTHONPYCACHEPREFIX
# just the very core tests with minimal dependencies # just the very core tests with minimal dependencies
@ -113,6 +117,7 @@ commands =
-p my.browser \ -p my.browser \
-p my.endomondo \ -p my.endomondo \
-p my.github.ghexport \ -p my.github.ghexport \
-p my.github.gdpr \
-p my.hypothesis \ -p my.hypothesis \
-p my.instapaper \ -p my.instapaper \
-p my.pocket \ -p my.pocket \