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

[FR] Better support for pathlib.Path in other fields #5102

Open
2 of 6 tasks
Laurent2916 opened this issue Nov 13, 2024 · 0 comments
Open
2 of 6 tasks

[FR] Better support for pathlib.Path in other fields #5102

Laurent2916 opened this issue Nov 13, 2024 · 0 comments
Labels
feature Work on a feature request

Comments

@Laurent2916
Copy link
Contributor

Proposal Summary

Currently pathlib.Path is well supported for the filepath field, though not so much for other fields:

$ python
Python 3.12.7 (main, Oct 16 2024, 04:37:19) [Clang 18.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fiftyone as fo
>>> from pathlib import Path
>>> d = fo.Dataset("my_dataset_test")

>>> s1 = fo.Sample(filepath="/foo/bar")
>>> d.add_sample(s1)
'673473ebe4b27ccbe9f5016d'

>>> s2 = fo.Sample(filepath=Path("/foo/bar"))
>>> d.add_sample(s2)
'673473f9e4b27ccbe9f5016e'

>>> s3 = fo.Sample(filepath=Path("/foo/bar"), other_filepath="/bar/foo")
>>> d.add_sample(s3)
'67347412e4b27ccbe9f5016f'

>>> s4 = fo.Sample(filepath=Path("/foo/bar"), other_filepath=Path("/bar/foo"))
>>> d.add_sample(s4)
Traceback (most recent call last):
  File "/blabla/.venv/lib/python3.12/site-packages/fiftyone/core/dataset.py", line 7950, in _validate_samples
    field.validate(value)
  File "/blabla/.venv/lib/python3.12/site-packages/mongoengine/fields.py", line 129, in validate
    self.error("StringField only accepts string values")
  File "/blabla/.venv/lib/python3.12/site-packages/mongoengine/base/fields.py", line 171, in error
    raise ValidationError(message, errors=errors, field_name=field_name)
mongoengine.errors.ValidationError: StringField only accepts string values

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/blabla/.venv/lib/python3.12/site-packages/fiftyone/core/dataset.py", line 3201, in add_sample
    ids = self._add_samples_batch(
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/blabla/.venv/lib/python3.12/site-packages/fiftyone/core/dataset.py", line 3323, in _add_samples_batch
    self._validate_samples(samples)
  File "/blabla/.venv/lib/python3.12/site-packages/fiftyone/core/dataset.py", line 7952, in _validate_samples
    raise ValueError(
ValueError: Invalid value for field 'other_filepath'. Reason: StringField only accepts string values

>>> s4
<Sample: {
    'id': None,
    'media_type': 'image',
    'filepath': '/foo/bar',  # <-- the Path was converted to str
    'tags': [],
    'metadata': None,
    'created_at': None,
    'last_modified_at': None,
    'other_filepath': PosixPath('/bar/foo'),  # <-- PosixPath not converted to str
}>

>>> s5 = fo.Sample(filepath=Path("/foo/bar"), other_filepath=str(Path("/bar/foo")))
>>> d.add_sample(s5)
'6734743ee4b27ccbe9f50170'

The workaround is to wrap every Path with str(..), though if this could be handled directly this would be better.

What areas of FiftyOne does this feature affect?

  • App: FiftyOne application
  • Core: Core fiftyone Python library
  • Server: FiftyOne server

Willingness to contribute

The FiftyOne Community welcomes contributions! Would you or another member of your organization be willing to contribute an implementation of this feature?

  • Yes. I can contribute this feature independently
  • Yes. I would be willing to contribute this feature with guidance from the FiftyOne community
  • No. I cannot contribute this feature at this time
@Laurent2916 Laurent2916 added the feature Work on a feature request label Nov 13, 2024
@Laurent2916 Laurent2916 changed the title [FR] Better support for pathlib.Path in other media_fields [FR] Better support for pathlib.Path in other fields Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Work on a feature request
Projects
None yet
Development

No branches or pull requests

1 participant