summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Chabot <simon.chabot@logilab.fr>2020-06-11 09:02:31 +0200
committerSimon Chabot <simon.chabot@logilab.fr>2020-06-11 09:02:31 +0200
commitec880f203dc2175ed76480d9180809336e586b03 (patch)
tree78a8f9194badd6c8243507de696ec794b48223ee
parent6ff4aae429f343f59853eea75d6f279854ea627d (diff)
downloadlogilab-common-ec880f203dc2175ed76480d9180809336e586b03.tar.gz
fix(typing): explicitly define Match and Pattern on Import Error (python < 3.7)
for python < 3.6 those two types are not defined. In case of import error, let's define them (in the exact same way they are defined in the re module of python >= 3.7).
-rw-r--r--logilab/common/pytest.py12
-rw-r--r--logilab/common/textutils.py13
2 files changed, 23 insertions, 2 deletions
diff --git a/logilab/common/pytest.py b/logilab/common/pytest.py
index 88e22f9..b79e835 100644
--- a/logilab/common/pytest.py
+++ b/logilab/common/pytest.py
@@ -100,7 +100,17 @@ import re
import sys
import os.path as osp
from time import process_time, time
-from re import Match
+
+try:
+ from re import Match
+except ImportError:
+ # Match is python > 3.6 only.
+ #
+ # To be compatible with python <= 3.6, and still provide some typing, we
+ # manually define Match, in the same manner it is defined in the re module
+ # of python > 3.7
+ # cf https://github.com/python/cpython/blob/3.7/Lib/re.py#L264
+ Match = type(re.sre_compile.compile("", 0).match("")) # type: ignore
import warnings
import types
import inspect
diff --git a/logilab/common/textutils.py b/logilab/common/textutils.py
index 95464e5..3ac6ad9 100644
--- a/logilab/common/textutils.py
+++ b/logilab/common/textutils.py
@@ -46,7 +46,18 @@ __docformat__ = "restructuredtext en"
import sys
import re
import os.path as osp
-from re import Pattern, Match
+
+try:
+ from re import Pattern, Match
+except ImportError:
+ # Pattern and Match are python > 3.6 only.
+ #
+ # To be compatible with python <= 3.6, and still provide some typing, we
+ # manually define Pattern and Match, in the same manner they are defined in
+ # the re module of python > 3.7
+ # cf https://github.com/python/cpython/blob/3.7/Lib/re.py#L264
+ Pattern = type(re.sre_compile.compile("", 0)) # type: ignore
+ Match = type(re.sre_compile.compile("", 0).match("")) # type: ignore
from warnings import warn
from unicodedata import normalize as _uninormalize
from typing import Any, Optional, Tuple, List, Callable, Dict, Union