Skip to content

Commit ccf80fd

Browse files
kszucscpcloud
authored andcommitted
refactor(dtypes): expose nbytes property for integer and floating point datatypes
1 parent 2d27df8 commit ccf80fd

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

ibis/backends/clickhouse/compiler/values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def _bit_agg(func):
636636
def _translate(op, **kw):
637637
arg = translate_val(op.arg, **kw)
638638
if not isinstance((type := op.arg.output_dtype), dt.UnsignedInteger):
639-
nbits = type._nbytes * 8
639+
nbits = type.nbytes * 8
640640
arg = f"reinterpretAsUInt{nbits}({arg})"
641641

642642
if (where := op.where) is not None:

ibis/expr/datatypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ def can_cast_to_differently_signed_integer_type(
9090
@castable.register(dt.SignedInteger, dt.SignedInteger)
9191
@castable.register(dt.UnsignedInteger, dt.UnsignedInteger)
9292
def can_cast_integers(source: dt.Integer, target: dt.Integer, **kwargs) -> bool:
93-
return target._nbytes >= source._nbytes
93+
return target.nbytes >= source.nbytes
9494

9595

9696
@castable.register(dt.Floating, dt.Floating)
9797
def can_cast_floats(
9898
source: dt.Floating, target: dt.Floating, upcast: bool = False, **kwargs
9999
) -> bool:
100100
if upcast:
101-
return target._nbytes >= source._nbytes
101+
return target.nbytes >= source.nbytes
102102

103103
# double -> float must be allowed because
104104
# float literals are inferred as doubles

ibis/expr/datatypes/core.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import numbers
4+
from abc import abstractmethod
45
from typing import Any, Iterable, Mapping, NamedTuple
56

67
import numpy as np
@@ -237,6 +238,11 @@ class Primitive(DataType, Singleton):
237238
"""Values with known size."""
238239

239240

241+
@public
242+
class Variadic(DataType):
243+
"""Values with unknown size."""
244+
245+
240246
@public
241247
class Null(Primitive):
242248
"""Null values."""
@@ -245,11 +251,6 @@ class Null(Primitive):
245251
column = ir.NullColumn
246252

247253

248-
@public
249-
class Variadic(DataType):
250-
"""Values with unknown size."""
251-
252-
253254
@public
254255
class Boolean(Primitive):
255256
"""[`True`][True] or [`False`][False] values."""
@@ -279,11 +280,9 @@ class Integer(Primitive, Numeric):
279280
column = ir.IntegerColumn
280281

281282
@property
282-
def _nbytes(self) -> int:
283+
@abstractmethod
284+
def nbytes(self) -> int:
283285
"""Return the number of bytes used to store values of this type."""
284-
raise TypeError(
285-
"Cannot determine the size in bytes of an abstract integer type."
286-
)
287286

288287

289288
@public
@@ -366,7 +365,7 @@ def largest(self):
366365

367366
@property
368367
def bounds(self):
369-
exp = self._nbytes * 8 - 1
368+
exp = self.nbytes * 8 - 1
370369
upper = (1 << exp) - 1
371370
return Bounds(lower=~upper, upper=upper)
372371

@@ -382,7 +381,7 @@ def largest(self):
382381

383382
@property
384383
def bounds(self):
385-
exp = self._nbytes * 8 - 1
384+
exp = self.nbytes * 8 - 1
386385
upper = 1 << exp
387386
return Bounds(lower=0, upper=upper)
388387

@@ -400,87 +399,86 @@ def largest(self):
400399
return float64
401400

402401
@property
403-
def _nbytes(self) -> int:
404-
raise TypeError(
405-
"Cannot determine the size in bytes of an abstract floating point type."
406-
)
402+
@abstractmethod
403+
def nbytes(self) -> int: # pragma: no cover
404+
...
407405

408406

409407
@public
410408
class Int8(SignedInteger):
411409
"""Signed 8-bit integers."""
412410

413-
_nbytes = 1
411+
nbytes = 1
414412

415413

416414
@public
417415
class Int16(SignedInteger):
418416
"""Signed 16-bit integers."""
419417

420-
_nbytes = 2
418+
nbytes = 2
421419

422420

423421
@public
424422
class Int32(SignedInteger):
425423
"""Signed 32-bit integers."""
426424

427-
_nbytes = 4
425+
nbytes = 4
428426

429427

430428
@public
431429
class Int64(SignedInteger):
432430
"""Signed 64-bit integers."""
433431

434-
_nbytes = 8
432+
nbytes = 8
435433

436434

437435
@public
438436
class UInt8(UnsignedInteger):
439437
"""Unsigned 8-bit integers."""
440438

441-
_nbytes = 1
439+
nbytes = 1
442440

443441

444442
@public
445443
class UInt16(UnsignedInteger):
446444
"""Unsigned 16-bit integers."""
447445

448-
_nbytes = 2
446+
nbytes = 2
449447

450448

451449
@public
452450
class UInt32(UnsignedInteger):
453451
"""Unsigned 32-bit integers."""
454452

455-
_nbytes = 4
453+
nbytes = 4
456454

457455

458456
@public
459457
class UInt64(UnsignedInteger):
460458
"""Unsigned 64-bit integers."""
461459

462-
_nbytes = 8
460+
nbytes = 8
463461

464462

465463
@public
466464
class Float16(Floating):
467465
"""16-bit floating point numbers."""
468466

469-
_nbytes = 2
467+
nbytes = 2
470468

471469

472470
@public
473471
class Float32(Floating):
474472
"""32-bit floating point numbers."""
475473

476-
_nbytes = 4
474+
nbytes = 4
477475

478476

479477
@public
480478
class Float64(Floating):
481479
"""64-bit floating point numbers."""
482480

483-
_nbytes = 8
481+
nbytes = 8
484482

485483

486484
@public

0 commit comments

Comments
 (0)