|
9 | 9 |
|
10 | 10 | import math |
11 | 11 | from datetime import timedelta |
12 | | -from typing import Any, NoReturn, Self, TypeVar, overload |
| 12 | +from typing import Self, TypeVar, overload |
13 | 13 |
|
14 | 14 | QuantityT = TypeVar( |
15 | 15 | "QuantityT", |
@@ -41,14 +41,27 @@ class Quantity: |
41 | 41 | class. Sub-classes must define this. |
42 | 42 | """ |
43 | 43 |
|
44 | | - def __init__(self, value: float, exponent: int = 0) -> None: |
45 | | - """Initialize a new quantity. |
| 44 | + def __init__(self) -> None: |
| 45 | + """Initialize this instance. |
46 | 46 |
|
47 | | - Args: |
48 | | - value: The value of this quantity in a given exponent of the base unit. |
49 | | - exponent: The exponent of the base unit the given value is in. |
| 47 | + This constructor is not meant to be used directly. Use one of the `from_*()` |
| 48 | + methods instead. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + TypeError: Every time this constructor is called. |
50 | 52 | """ |
51 | | - self._base_value = value * 10.0**exponent |
| 53 | + # This is for documentation purposes and to hint to mypy that this |
| 54 | + # attribute exists. |
| 55 | + self._base_value: float = 0.0 |
| 56 | + """The value of this quantity in the base unit.""" |
| 57 | + |
| 58 | + cls = type(self) |
| 59 | + raise TypeError( |
| 60 | + "Use of default constructor is not allowed for " |
| 61 | + f"{cls.__module__}.{cls.__qualname__}, " |
| 62 | + f"use {cls.__name__}.zero() or one of the " |
| 63 | + f"`{cls.__name__}.from_*()` constructors instead." |
| 64 | + ) |
52 | 65 |
|
53 | 66 | @classmethod |
54 | 67 | def _new(cls, value: float, *, exponent: int = 0) -> Self: |
@@ -451,29 +464,8 @@ def __abs__(self) -> Self: |
451 | 464 | return absolute |
452 | 465 |
|
453 | 466 |
|
454 | | -class _NoDefaultConstructible(type): |
455 | | - """A metaclass that disables the default constructor.""" |
456 | | - |
457 | | - def __call__(cls, *_args: Any, **_kwargs: Any) -> NoReturn: |
458 | | - """Raise a TypeError when the default constructor is called. |
459 | | -
|
460 | | - Args: |
461 | | - *_args: ignored positional arguments. |
462 | | - **_kwargs: ignored keyword arguments. |
463 | | -
|
464 | | - Raises: |
465 | | - TypeError: Always. |
466 | | - """ |
467 | | - raise TypeError( |
468 | | - "Use of default constructor NOT allowed for " |
469 | | - f"{cls.__module__}.{cls.__qualname__}, " |
470 | | - f"use one of the `{cls.__name__}.from_*()` methods instead." |
471 | | - ) |
472 | | - |
473 | | - |
474 | 467 | class Temperature( |
475 | 468 | Quantity, |
476 | | - metaclass=_NoDefaultConstructible, |
477 | 469 | exponent_unit_map={ |
478 | 470 | 0: "°C", |
479 | 471 | }, |
@@ -503,7 +495,6 @@ def as_celsius(self) -> float: |
503 | 495 |
|
504 | 496 | class Power( |
505 | 497 | Quantity, |
506 | | - metaclass=_NoDefaultConstructible, |
507 | 498 | exponent_unit_map={ |
508 | 499 | -3: "mW", |
509 | 500 | 0: "W", |
@@ -679,7 +670,6 @@ def __truediv__(self, other: Current | Voltage) -> Voltage | Current: |
679 | 670 |
|
680 | 671 | class Current( |
681 | 672 | Quantity, |
682 | | - metaclass=_NoDefaultConstructible, |
683 | 673 | exponent_unit_map={ |
684 | 674 | -3: "mA", |
685 | 675 | 0: "A", |
@@ -777,7 +767,6 @@ def __mul__(self, other: Percentage | Voltage) -> Self | Power: |
777 | 767 |
|
778 | 768 | class Voltage( |
779 | 769 | Quantity, |
780 | | - metaclass=_NoDefaultConstructible, |
781 | 770 | exponent_unit_map={0: "V", -3: "mV", 3: "kV"}, |
782 | 771 | ): |
783 | 772 | """A voltage quantity. |
@@ -892,7 +881,6 @@ def __mul__(self, other: Percentage | Current) -> Self | Power: |
892 | 881 |
|
893 | 882 | class Energy( |
894 | 883 | Quantity, |
895 | | - metaclass=_NoDefaultConstructible, |
896 | 884 | exponent_unit_map={ |
897 | 885 | 0: "Wh", |
898 | 886 | 3: "kWh", |
@@ -1032,7 +1020,6 @@ def __truediv__(self, other: timedelta | Power) -> Power | timedelta: |
1032 | 1020 |
|
1033 | 1021 | class Frequency( |
1034 | 1022 | Quantity, |
1035 | | - metaclass=_NoDefaultConstructible, |
1036 | 1023 | exponent_unit_map={0: "Hz", 3: "kHz", 6: "MHz", 9: "GHz"}, |
1037 | 1024 | ): |
1038 | 1025 | """A frequency quantity. |
@@ -1137,7 +1124,6 @@ def period(self) -> timedelta: |
1137 | 1124 |
|
1138 | 1125 | class Percentage( |
1139 | 1126 | Quantity, |
1140 | | - metaclass=_NoDefaultConstructible, |
1141 | 1127 | exponent_unit_map={0: "%"}, |
1142 | 1128 | ): |
1143 | 1129 | """A percentage quantity. |
|
0 commit comments