Skip to content

Commit

Permalink
Merge pull request #263 from MetRonnie/sre_constants
Browse files Browse the repository at this point in the history
Remove deprecated `sre_constants` usage
  • Loading branch information
oliver-sanders authored Jan 17, 2025
2 parents 8c0bb08 + 276c342 commit 37401de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
8 changes: 4 additions & 4 deletions metomi/isodatetime/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"""This provides ISO 8601 parsing functionality."""

import re
import sre_constants

from . import data
from . import parser_spec
Expand Down Expand Up @@ -338,12 +337,13 @@ def strptime(self, strptime_data_string, strptime_format_string,
regex, strptime_data_string,
dump_format=dump_format, source=strptime_format_string)

def _parse_from_custom_regex(self, regex, data_string, dump_format=None,
source=None):
def _parse_from_custom_regex(
self, regex, data_string, dump_format=None, source=None
):
"""Parse data_string according to the regular expression in regex."""
try:
compiled_regex = re.compile(regex)
except sre_constants.error:
except Exception:
raise StrptimeConversionError(source, regex)
result = compiled_regex.match(data_string)
if not result:
Expand Down
14 changes: 13 additions & 1 deletion metomi/isodatetime/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import pytest
import time
from unittest.mock import Mock

import pytest

from metomi.isodatetime.parsers import TimePointParser


@pytest.fixture
def mock_local_time_zone(monkeypatch: pytest.MonkeyPatch):
Expand All @@ -39,3 +42,12 @@ def _mock_local_time_zone(seconds: int, dst_seconds: int = 0) -> None:
mock_time.localtime.return_value = Mock(tm_isdst=is_dst)
monkeypatch.setattr('metomi.isodatetime.timezone.time', mock_time)
return _mock_local_time_zone


@pytest.fixture(scope='session')
def tp_parser():
"""Returns a TimePointParser instance for the session.
This saves time by only creating the parser once for the entire session.
"""
return TimePointParser()
22 changes: 15 additions & 7 deletions metomi/isodatetime/tests/test_00.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
import datetime
from itertools import chain
import unittest

import pytest

from metomi.isodatetime import data
from metomi.isodatetime import dumpers
from metomi.isodatetime import parsers
from metomi.isodatetime import parser_spec
from metomi.isodatetime import (
data,
dumpers,
parser_spec,
parsers,
)
from metomi.isodatetime.exceptions import (
ISO8601SyntaxError, TimePointDumperBoundsError, BadInputError)
BadInputError,
ISO8601SyntaxError,
StrptimeConversionError,
TimePointDumperBoundsError,
)


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


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

0 comments on commit 37401de

Please sign in to comment.