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

🎬 : Improved file uploader and camera input to call its on_change handler only when necessary #4270

Merged
merged 12 commits into from Jan 25, 2022
5 changes: 5 additions & 0 deletions lib/streamlit/uploaded_file_manager.py
Expand Up @@ -50,6 +50,11 @@ def __init__(self, record: UploadedFileRec):
self.type = record.type
self.size = len(record.data)

def __eq__(self, other):
if other is not None:
return self.id == other.id
return False

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be really correct, __eq__ should typecheck its argument (and we might as well also type-annotate the function)

def __eq__(self, other: object) -> bool:
  if not isinstance(other, UploadedFile):
    return NotImplemented
  return self.id == other.id

(This StackOverflow has more details about the weird "return NotImplemented" line: https://stackoverflow.com/questions/878943/why-return-notimplemented-instead-of-raising-notimplementederror)

def __repr__(self) -> str:
return util.repr_(self)

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/streamlit/file_uploader_test.py
Expand Up @@ -125,7 +125,7 @@ def test_unique_uploaded_file_instance(self, get_file_recs_patch):
file1: UploadedFile = st.file_uploader("a", accept_multiple_files=False)
file2: UploadedFile = st.file_uploader("b", accept_multiple_files=False)

self.assertNotEqual(file1, file2)
self.assertNotEqual(id(file1), id(file2))

# Seeking in one instance should not impact the position in the other.
file1.seek(2)
Expand Down