@@ -344,7 +344,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> np.str_:
344344 return self .to_native_dtype ().type (data )
345345 raise TypeError (f"Invalid type: { data } . Expected a string." ) # pragma: no cover
346346
347- def _check_scalar (self , data : object ) -> TypeGuard [str | np . str_ | bytes | int ]:
347+ def _check_scalar (self , data : object ) -> TypeGuard [SupportsStr ]:
348348 """
349349 Check that the input is a valid scalar value for this data type.
350350
@@ -355,11 +355,11 @@ def _check_scalar(self, data: object) -> TypeGuard[str | np.str_ | bytes | int]:
355355
356356 Returns
357357 -------
358- TypeGuard[str | np.str_ | bytes | int ]
358+ TypeGuard[SupportsStr ]
359359 Whether the input is a valid scalar value for this data type.
360360 """
361361 # this is generous for backwards compatibility
362- return isinstance (data , str | np . str_ | bytes | int )
362+ return isinstance (data , SupportsStr )
363363
364364 def cast_scalar (self , data : object ) -> np .str_ :
365365 """
@@ -383,13 +383,13 @@ def cast_scalar(self, data: object) -> np.str_:
383383 # >>> x.dtype
384384 # dtype('U11')
385385
386- if isinstance (data , int ):
387- return self .to_native_dtype ().type (str (data )[: self .length ])
388- else :
389- return self .to_native_dtype ().type (data [: self .length ])
390- raise TypeError (
391- f"Cannot convert object with type { type (data )} to a NumPy unicode string scalar."
386+ return self .to_native_dtype ().type (str (data )[: self .length ])
387+
388+ msg = (
389+ f"Cannot convert object { data !r} with type { type (data )} to a scalar compatible with the "
390+ f"data type { self } ."
392391 )
392+ raise TypeError (msg ) # pragma: no-cover
393393
394394 @property
395395 def item_size (self ) -> int :
@@ -711,7 +711,11 @@ def cast_scalar(self, data: object) -> str:
711711 """
712712 if self ._check_scalar (data ):
713713 return self ._cast_scalar_unchecked (data )
714- raise TypeError (f"Cannot convert object with type { type (data )} to a Python string." )
714+ msg = (
715+ f"Cannot convert object { data !r} with type { type (data )} to a scalar compatible with the "
716+ f"data type { self } ."
717+ )
718+ raise TypeError (msg )
715719
716720
717721if _NUMPY_SUPPORTS_VLEN_STRING :
0 commit comments