Skip to content

Conversation

@jrieke
Copy link
Collaborator

@jrieke jrieke commented Oct 24, 2024

Describe your changes

lib/streamlit/elements/image.py contained a bunch of utils functions that were imported in other files, which caused cyclic imports in #9711. This PR moves all of these functions into lib/streamlit/elements/lib/image_utils.py and makes all other files import directly from there.

Unfortunately, this PR didn't remove all cyclic imports. But it's still worth merging this to clean up our codebase a bit more.

GitHub Issue Link (if applicable)

Just a refactor, no new functionality or tests added.

Testing Plan


Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@jrieke jrieke changed the title Move image utils function into lib/image_utils.py [WIP] Move image utils function into lib/image_utils.py Oct 24, 2024
@jrieke jrieke marked this pull request as draft October 24, 2024 05:25
from streamlit.errors import StreamlitAPIException
from streamlit.proto.Image_pb2 import ImageList as ImageListProto
from streamlit.runtime import caching
from streamlit.type_util import NumpyShape

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.type_util](1) begins an import cycle.

import streamlit.elements.image as image_utils
from streamlit.deprecation_util import show_deprecation_warning
from streamlit.elements.lib.image_utils import WidthBehavior, marshall_images

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.image_utils](1) begins an import cycle.
from streamlit.delta_generator_singletons import get_dg_singleton_instance
from streamlit.elements.image import AtomicImage, WidthBehaviour, image_to_url
from streamlit.elements.lib.form_utils import is_in_form
from streamlit.elements.lib.image_utils import AtomicImage, WidthBehavior, image_to_url

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.image_utils](1) begins an import cycle.
Comment on lines +29 to +35
from streamlit.elements.lib.image_utils import (
Channels,
ImageFormatOrAuto,
ImageOrImageList,
WidthBehavior,
marshall_images,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.image_utils](1) begins an import cycle.
@jrieke jrieke added security-assessment-completed Security assessment has been completed for PR impact:internal PR changes only affect internal code change:refactor PR contains code refactoring without behavior change labels Oct 24, 2024
from typing import Any

import numpy.typing as npt
from PIL import GifImagePlugin, Image, ImageFile

Check notice

Code scanning / CodeQL

Unused import

Import of 'GifImagePlugin' is not used. Import of 'Image' is not used. Import of 'ImageFile' is not used.
@jrieke jrieke marked this pull request as ready for review October 25, 2024 05:56
@jrieke jrieke requested a review from a team as a code owner October 25, 2024 05:56
@jrieke jrieke changed the title [WIP] Move image utils function into lib/image_utils.py Move image utils function into lib/image_utils.py Oct 25, 2024
# `npt.NDArray[Any]` returns a `npt.NDArray[Any]`, so we need to
# ignore redundant casts below.
image_data = _np_array_to_bytes(
array=cast("npt.NDArray[Any]", image), # type: ignore[redundant-cast]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

MyPy complained that the ignore statement here is unused, so I removed it (as well as the comment above) in image_utils.py. Not entirely sure though if this is still needed for some very old numpy versions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah it looks like this was added 2 years ago in #4761, so I hope its safe to remove now.
Based on the comment it sounds like we might be able to remove the cast as well as it is described as redundant. Could you try this out and see whether anything complains?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the cast and everything seems to work fine, so I think it's probably fine to leave it like this. I can't imagine a realistic scenario where someone would use the latest Streamlit version but a NumPy version that's older than 2 years.

Copy link
Collaborator

@raethlein raethlein left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the refactoring!

@jrieke jrieke merged commit 1fe7188 into develop Nov 2, 2024
@jrieke jrieke deleted the refactor/image-utils-cleanup branch November 2, 2024 01:34
edegp pushed a commit to edegp/streamlit that referenced this pull request Jan 19, 2025
## Describe your changes

`lib/streamlit/elements/image.py` contained a bunch of utils functions
that were imported in other files, which caused cyclic imports in streamlit#9711.
This PR moves all of these functions into
`lib/streamlit/elements/lib/image_utils.py` and makes all other files
import directly from there.

Unfortunately, this PR didn't remove all cyclic imports. But it's still
worth merging this to clean up our codebase a bit more.

## GitHub Issue Link (if applicable)

Just a refactor, no new functionality or tests added. 

## Testing Plan

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.

---------

Co-authored-by: Johannes Rieke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:refactor PR contains code refactoring without behavior change impact:internal PR changes only affect internal code security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants