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

fix: from_sample override logic #3202

Merged
merged 3 commits into from Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 1 addition & 21 deletions src/bentoml/_internal/io_descriptors/base.py
Expand Up @@ -81,7 +81,6 @@ class IODescriptor(ABC, _OpenAPIMeta, t.Generic[IOType]):
_rpc_content_type: str = "application/grpc"
_proto_fields: tuple[ProtoField]
_sample: IOType | None = None
_initialized: bool = False
_args: t.Sequence[t.Any]
_kwargs: dict[str, t.Any]

Expand All @@ -99,27 +98,9 @@ def __init_subclass__(cls, *, descriptor_id: str | None = None):
def __init__(self, **kwargs: t.Any) -> None:
...

def __getattr__(self, name: str) -> t.Any:
if not self._initialized:
self._lazy_init()
assert self._initialized
return object.__getattribute__(self, name)

def __dir__(self) -> t.Iterable[str]:
if not self._initialized:
self._lazy_init()
assert self._initialized
return object.__dir__(self)

def __repr__(self) -> str:
return self.__class__.__qualname__

def _lazy_init(self) -> None:
self._initialized = True
self.__init__(*self._args, **self._kwargs)
del self._args
del self._kwargs

@property
def sample(self) -> IOType | None:
return self._sample
Expand All @@ -131,8 +112,7 @@ def sample(self, value: IOType) -> None:
@classmethod
def from_sample(cls, sample: IOType | t.Any, **kwargs: t.Any) -> Self:
klass = cls(**kwargs)
sample = klass._from_sample(sample)
klass.sample = sample
klass.sample = klass._from_sample(sample)
return klass

@abstractmethod
Expand Down
1 change: 0 additions & 1 deletion src/bentoml/_internal/io_descriptors/file.py
Expand Up @@ -135,7 +135,6 @@ def _from_sample(self, sample: FileType | str) -> FileType:
)
if isinstance(sample, t.IO):
sample = FileLike[bytes](sample, "<sample>")
self._mime_type = filetype.guess_mime(sample)
elif isinstance(sample, (str, os.PathLike)):
p = resolve_user_filepath(sample, ctx=None)
self._mime_type = filetype.guess_mime(p)
Expand Down
1 change: 1 addition & 0 deletions src/bentoml/_internal/io_descriptors/image.py
Expand Up @@ -231,6 +231,7 @@ def _from_sample(self, sample: ImageType | str) -> ImageType:
sample = PIL.Image.open(f)
except PIL.UnidentifiedImageError as err:
raise BadInput(f"Failed to parse sample image file: {err}") from None
self._mime_type = img_type.mime
return sample

def to_spec(self) -> dict[str, t.Any]:
Expand Down
2 changes: 0 additions & 2 deletions src/bentoml/_internal/io_descriptors/numpy.py
Expand Up @@ -440,8 +440,6 @@ async def predict(input: NDArray[np.int16]) -> NDArray[Any]:
) from None
self._dtype = sample.dtype
self._shape = sample.shape
self._enforce_dtype = True
self._enforce_shape = True
return sample

async def from_proto(self, field: pb.NDArray | bytes) -> ext.NpNDArray:
Expand Down
8 changes: 2 additions & 6 deletions src/bentoml/_internal/io_descriptors/pandas.py
Expand Up @@ -431,10 +431,8 @@ def predict(inputs: pd.DataFrame) -> pd.DataFrame: ...
) from None
self._shape = sample.shape
self._columns = [str(i) for i in list(sample.columns)]
self._dtype = True
self._enforce_dtype = True
self._enforce_shape = True
self._apply_column_names = True
if self._dtype is None:
self._dtype = True # infer dtype automatically
return sample

def _convert_dtype(
Expand Down Expand Up @@ -885,8 +883,6 @@ def predict(inputs: pd.Series) -> pd.Series: ...
sample = pd.Series(sample)
self._dtype = sample.dtype
self._shape = sample.shape
self._enforce_dtype = True
self._enforce_shape = True
return sample

def input_type(self) -> LazyType[ext.PdSeries]:
Expand Down