From 8915a67851193c547d0b4160d26ed609ffa47f71 Mon Sep 17 00:00:00 2001 From: Caglar Pir Date: Fri, 4 Sep 2026 10:03:20 +0200 Subject: [PATCH] Stop save_locally defaulting to the installed package directory save_locally declared: file_path: str = os.path.dirname(os.path.realpath(__file__)) That default is evaluated once at import, so it resolves to the directory containing interface.py -- i.e. inside site-packages. Calling save_locally() without file_path therefore wrote user data into the installed package rather than anywhere the caller would expect. The docstring already claimed it defaults to the current directory, so the documented behaviour and the actual behaviour disagreed. Reproduced before the fix, from an unrelated working directory: written to: .../site-packages/mapillary/probe_write.geojson cwd: [] Beyond being surprising, writing there can fail on a system-wide install, silently pollutes the installed package, and loses the files on reinstall or uninstall. Defaults file_path to None and resolves it to os.getcwd() at call time, which is what the docstring already described. Explicit file_path values are unaffected. Docstring examples that passed the package directory have been changed to a plain relative path so they no longer suggest the old behaviour. --- src/mapillary/interface.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mapillary/interface.py b/src/mapillary/interface.py index b2f5223..9a74fa7 100644 --- a/src/mapillary/interface.py +++ b/src/mapillary/interface.py @@ -968,7 +968,7 @@ def image_from_key(key: str, fields: list = []) -> str: @auth() def save_locally( geojson_data: str, - file_path: str = os.path.dirname(os.path.realpath(__file__)), + file_path: str = None, file_name: str = None, extension: str = "geojson", ) -> None: @@ -979,7 +979,8 @@ def save_locally( :param geojson_data: The GeoJSON data to be stored :type geojson_data: str - :param file_path: The path to save the data to. Defaults to the current directory path + :param file_path: The directory to save the data to. Defaults to the current + working directory :type file_path: str :param file_name: The name of the file to be saved. Defaults to 'geojson' @@ -1006,18 +1007,24 @@ def save_locally( >>> mly.interface.set_access_token('MLY|XXX') >>> mly.interface.save_locally( ... geojson_data=geojson_data, - ... file_path=os.path.dirname(os.path.realpath(__file__)), + ... file_path='./output', ... file_name='test_geojson', ... extension='geojson' ... ) >>> mly.interface.save_locally( ... geojson_data=geojson_data, - ... file_path=os.path.dirname(os.path.realpath(__file__)), + ... file_path='./output', ... file_name='local_geometries', ... extension='csv' ... ) """ + # Resolve the default at call time, not at import time. Binding it to the + # module's own directory made the default write target the installed + # package inside site-packages. + if file_path is None: + file_path = os.getcwd() + # Check if a valid file format was provided if extension.lower() not in ["geojson", "csv"]: # If not, raise an error