From b7cdead9605e4e65349261780532f268efa27f5d Mon Sep 17 00:00:00 2001 From: Cody Baker <51133164+CodyCBakerPhD@users.noreply.github.com> Date: Wed, 22 Feb 2023 23:10:57 +0000 Subject: [PATCH] Increase stacklevel of timezone warning (#1641) Co-authored-by: mavaylon1 Co-authored-by: Ryan Ly --- CHANGELOG.md | 1 + src/pynwb/file.py | 2 +- tests/unit/test_file.py | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 054a8e34f..8af0be087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Added ``NWBHDF5IO.nwb_version`` property to get the NWB version from an NWB HDF5 file @oruebel [#1612](https://github.com/NeurodataWithoutBorders/pynwb/pull/1612) - Updated ``NWBHDF5IO.read`` to check NWB version before read and raise more informative error if an unsupported version is found @oruebel [#1612](https://github.com/NeurodataWithoutBorders/pynwb/pull/1612) - Added the `driver` keyword argument to the `pynwb.validate` function as well as the corresponding namespace caching. @CodyCBakerPhD [#1588](https://github.com/NeurodataWithoutBorders/pynwb/pull/1588) +- Increased the stacklevel of the warning from `_add_missing_timezone` in `pynwb.file` to make identification of which datetime field is missing a timezone easier. @CodyCBakerPhD [#1641](https://github.com/NeurodataWithoutBorders/pynwb/pull/1641) ### Documentation and tutorial enhancements: - Adjusted [ecephys tutorial](https://pynwb.readthedocs.io/en/stable/tutorials/domain/ecephys.html) to create fake data with proper dimensions @bendichter [#1581](https://github.com/NeurodataWithoutBorders/pynwb/pull/1581) diff --git a/src/pynwb/file.py b/src/pynwb/file.py index 7bd1f452e..05079ae87 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -1120,7 +1120,7 @@ def _add_missing_timezone(date): if not isinstance(date, datetime): raise ValueError("require datetime object") if date.tzinfo is None: - warn("Date is missing timezone information. Updating to local timezone.") + warn("Date is missing timezone information. Updating to local timezone.", stacklevel=2) return date.replace(tzinfo=tzlocal()) return date diff --git a/tests/unit/test_file.py b/tests/unit/test_file.py index c888e89d9..bc00fe8dd 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -6,7 +6,7 @@ from pynwb import NWBFile, TimeSeries, NWBHDF5IO from pynwb.base import Image, Images -from pynwb.file import Subject, ElectrodeTable +from pynwb.file import Subject, ElectrodeTable, _add_missing_timezone from pynwb.epoch import TimeIntervals from pynwb.ecephys import ElectricalSeries from pynwb.testing import TestCase, remove_test_file @@ -561,3 +561,9 @@ def test_reftime_tzaware(self): 'TEST124', self.start_time, timestamps_reference_time=self.ref_time_notz) + + +class TestTimezone(TestCase): + def test_raise_warning__add_missing_timezone(self): + with self.assertWarnsWith(UserWarning, "Date is missing timezone information. Updating to local timezone."): + _add_missing_timezone(datetime(2017, 5, 1, 12))