Skip to content

BUG: astropy.time.update_leap_seconds doesn't warn for corrupt files passed as pathlib.Path objects #16929

@neutrinoceros

Description

@neutrinoceros

In #16060 and #16923, we find that astropy/time.update_leap_seconds doesn't behave correctly if passed a corrupt file as Path instead of str.
This is visible in astropy/test/test_update_leap_second.py::test_auto_update_corrupt_file, as applying the following patch

diff --git a/astropy/time/tests/test_update_leap_seconds.py b/astropy/time/tests/test_update_leap_seconds.py
index a1027a0755..45250147c3 100644
--- a/astropy/time/tests/test_update_leap_seconds.py
+++ b/astropy/time/tests/test_update_leap_seconds.py
@@ -58,7 +58,7 @@ class TestUpdateLeapSeconds:
             update_leap_seconds(["nonsense"])
 
     def test_auto_update_corrupt_file(self, tmp_path):
-        bad_file = str(tmp_path / "no_expiration")
+        bad_file = tmp_path / "no_expiration"
         with open(iers.IERS_LEAP_SECOND_FILE) as fh:
             lines = fh.readlines()
         with open(bad_file, "w") as fh:

leads to failing the test:

E   Failed: DID NOT WARN. No warnings of type (<class 'astropy.utils.exceptions.AstropyWarning'>,) matching the regex were emitted.
     Regex: ValueError.*did not find expiration
     Emitted warnings: [ AstropyWarning('leap-second auto-update failed due to the following exception: AttributeError("\'PosixPath\' object has no attribute \'decode\'")')].

(or in other words: a different warning is emitted first)

The issue doesn't appear to be trivial enough to be fixed directly in #16023 (which is meant as a simple refactor), but it should be done as a follow up.

There should ideally be more than one test exercising passing Path arguments to this function.

Originally posted by @pllim in #16923 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions