diff --git a/my/core/compat.py b/my/core/compat.py index a2a627c..a7775c8 100644 --- a/my/core/compat.py +++ b/my/core/compat.py @@ -1,8 +1,8 @@ ''' Some backwards compatibility stuff/deprecation helpers ''' +import sys from types import ModuleType -from typing import Callable from . import warnings 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') -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 windows = os.name == 'nt' @@ -103,3 +87,39 @@ else: return property(functools.lru_cache(maxsize=1)(f)) # type: ignore del Cl 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 diff --git a/my/fbmessenger/common.py b/my/fbmessenger/common.py index 5f8bd85..748b9d9 100644 --- a/my/fbmessenger/common.py +++ b/my/fbmessenger/common.py @@ -1,16 +1,9 @@ from my.core import __NOT_HPI_MODULE__ -from datetime import datetime -from typing import Iterator, Optional, TYPE_CHECKING +from typing import Iterator, Optional -if TYPE_CHECKING: - try: - from typing import Protocol - except ImportError: - # requirement of mypy - from typing_extensions import Protocol # type: ignore[misc] -else: - Protocol = object +from my.core.compat import Protocol +from my.core import datetime_aware class Thread(Protocol): @@ -26,7 +19,7 @@ class Message(Protocol): def id(self) -> str: ... @property - def dt(self) -> datetime: ... + def dt(self) -> datetime_aware: ... @property def text(self) -> Optional[str]: ... diff --git a/my/reddit/common.py b/my/reddit/common.py index 3bb0279..d33e02b 100644 --- a/my/reddit/common.py +++ b/my/reddit/common.py @@ -2,22 +2,13 @@ This defines Protocol classes, which make sure that each different 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 my.core.common import datetime_aware - -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 +from my.core.compat import Protocol +from my.core import datetime_aware, Json # common fields across all the Protocol classes, so generic code can be written diff --git a/tox.ini b/tox.ini index 33c2c71..427be71 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,11 @@ minversion = 3.5 toxworkdir={env:TOXWORKDIR_BASE:}{toxinidir}/.tox [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 @@ -113,6 +117,7 @@ commands = -p my.browser \ -p my.endomondo \ -p my.github.ghexport \ + -p my.github.gdpr \ -p my.hypothesis \ -p my.instapaper \ -p my.pocket \