Skip to content

Commit 37401de

Browse files
Merge pull request #263 from MetRonnie/sre_constants
Remove deprecated `sre_constants` usage
2 parents 8c0bb08 + 276c342 commit 37401de

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

metomi/isodatetime/parsers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"""This provides ISO 8601 parsing functionality."""
2020

2121
import re
22-
import sre_constants
2322

2423
from . import data
2524
from . import parser_spec
@@ -338,12 +337,13 @@ def strptime(self, strptime_data_string, strptime_format_string,
338337
regex, strptime_data_string,
339338
dump_format=dump_format, source=strptime_format_string)
340339

341-
def _parse_from_custom_regex(self, regex, data_string, dump_format=None,
342-
source=None):
340+
def _parse_from_custom_regex(
341+
self, regex, data_string, dump_format=None, source=None
342+
):
343343
"""Parse data_string according to the regular expression in regex."""
344344
try:
345345
compiled_regex = re.compile(regex)
346-
except sre_constants.error:
346+
except Exception:
347347
raise StrptimeConversionError(source, regex)
348348
result = compiled_regex.match(data_string)
349349
if not result:

metomi/isodatetime/tests/conftest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
# You should have received a copy of the GNU Lesser General Public License
1414
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

16-
import pytest
1716
import time
1817
from unittest.mock import Mock
1918

19+
import pytest
20+
21+
from metomi.isodatetime.parsers import TimePointParser
22+
2023

2124
@pytest.fixture
2225
def mock_local_time_zone(monkeypatch: pytest.MonkeyPatch):
@@ -39,3 +42,12 @@ def _mock_local_time_zone(seconds: int, dst_seconds: int = 0) -> None:
3942
mock_time.localtime.return_value = Mock(tm_isdst=is_dst)
4043
monkeypatch.setattr('metomi.isodatetime.timezone.time', mock_time)
4144
return _mock_local_time_zone
45+
46+
47+
@pytest.fixture(scope='session')
48+
def tp_parser():
49+
"""Returns a TimePointParser instance for the session.
50+
51+
This saves time by only creating the parser once for the entire session.
52+
"""
53+
return TimePointParser()

metomi/isodatetime/tests/test_00.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
import datetime
2020
from itertools import chain
2121
import unittest
22+
2223
import pytest
2324

24-
from metomi.isodatetime import data
25-
from metomi.isodatetime import dumpers
26-
from metomi.isodatetime import parsers
27-
from metomi.isodatetime import parser_spec
25+
from metomi.isodatetime import (
26+
data,
27+
dumpers,
28+
parser_spec,
29+
parsers,
30+
)
2831
from metomi.isodatetime.exceptions import (
29-
ISO8601SyntaxError, TimePointDumperBoundsError, BadInputError)
32+
BadInputError,
33+
ISO8601SyntaxError,
34+
StrptimeConversionError,
35+
TimePointDumperBoundsError,
36+
)
3037

3138

3239
def get_timedurationparser_tests():
@@ -1523,5 +1530,6 @@ def test_timepoint_dump_format(self):
15231530
# QUESTION: What was this test meant to do exactly?
15241531

15251532

1526-
if __name__ == '__main__':
1527-
unittest.main()
1533+
def test_strptime_bad(tp_parser: parsers.TimePointParser):
1534+
with pytest.raises(StrptimeConversionError):
1535+
tp_parser.strptime("2020-01-01", "]")

0 commit comments

Comments
 (0)