Skip to content

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

Closed

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)

Activity

  1. pllim commented on Sep 4, 2024

    @pllim
    Member

    'PosixPath' object has no attribute 'decode'

    Hmm. Do you still have the full traceback?

  2. pllim commented on Sep 4, 2024

    @pllim
    Member

    Ah, okay, that error is actually from urlparse here

    if urlparse(file).netloc:

    that you can reproduce like this

    from urllib.parse import urlparse
    from pathlib import Path
    p = Path("pyproject.toml")  # Or pick any file you have locally
    urlparse(p)

    so that probably means open itself needs to support Path object (new feature) for this to work (#16931)

    def open(cls, file=None, cache=False, **kwargs):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to 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