Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address numpy int argument conversion issue #1782

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions holidays/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ def _normalize_arguments(cls, value):
A set created from `value` argument.

"""
if value is None:
return set()

if isinstance(value, cls):
return {value}

return set(value) if value is not None else set()
try:
return {v if isinstance(v, cls) else cls(v) for v in value}
except TypeError: # non-iterable
return {value if isinstance(value, cls) else cls(value)}


def _normalize_tuple(data):
def _normalize_tuple(value):
"""Normalize tuple.

:param data:
Expand All @@ -40,4 +46,4 @@ def _normalize_tuple(data):
An unchanged object for tuple of tuples, e.g., ((JAN, 10), (DEC, 31)).
An object put into a tuple otherwise, e.g., ((JAN, 10),).
"""
return data if not data or isinstance(data[0], tuple) else (data,)
return value if not value or isinstance(value[0], tuple) else (value,)
3 changes: 2 additions & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Test requirements.
coverage
importlib-metadata
numpy
polib
pytest
pytest-cov
pytest-xdist
polib
42 changes: 42 additions & 0 deletions tests/third_party/test_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# holidays
# --------
# A fast, efficient Python library for generating country, province and state
# specific sets of holidays on the fly. It aims to make determining whether a
# specific date is a holiday as fast and flexible as possible.
#
# Authors: Vacanza Team and individual contributors (see AUTHORS file)
# dr-prodigy <dr.prodigy.github@gmail.com> (c) 2017-2023
# ryanss <ryanssdev@icloud.com> (c) 2014-2017
# Website: https://github.com/vacanza/python-holidays
# License: MIT (see LICENSE file)

from platform import platform, python_implementation, python_version
from unittest import TestCase

import pytest

from holidays.countries.cambodia import Cambodia
from holidays.countries.thailand import Thailand
from holidays.countries.ukraine import Ukraine


class TestNumpy(TestCase):
@pytest.mark.skipif(
platform().startswith("Windows")
and python_implementation().startswith("PyPy")
and python_version().startswith("3.10"),
reason='Avoid "make: *** [Makefile:63: test] Error 5" for pypy3.10 on Windows',
)
Comment on lines +24 to +29
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems I was looking in the wrong place. :(

def test_years_int_conversion(self):
import numpy as np # It seems the import causes the error mentioned above.

years = (1995, 2000)
years_range = set(range(*years))

for cls in (Cambodia, Thailand, Ukraine):
# Test single value.
for int_x in (np.int16, np.int32, np.int64):
self.assertEqual(cls(years=int_x(2024)).years, {2024})

# Test iterable.
self.assertEqual(cls(years=np.arange(*years)).years, years_range)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ env_list =
docs
pre-commit
python3.{12, 11, 10, 9, 8}
pypy3.{10, 9}
skip_missing_interpreters = true

[testenv]
Expand Down
Loading