@@ -26,6 +26,9 @@ def _scaling_preprocessor(input_string: str) -> str:
26
26
tight_division = False
27
27
scales = []
28
28
29
+ if next (token for token in tokens ).type == NUMBER :
30
+ return input_string # The unit can't have a leading number; scaling factors are internal
31
+
29
32
for token in tokens :
30
33
# Note that while this prevents adding a bunch of numbers to the registry,
31
34
# no test would break if the `exponent` logic were removed
@@ -36,8 +39,13 @@ def _scaling_preprocessor(input_string: str) -> str:
36
39
if not exponent and token .type == NUMBER :
37
40
scales .append ([token .string , False ])
38
41
tight_division = division
39
- exponent = token .type == OP and token .string in {"^" , "**" }
40
- division = token .type == OP and token .string in {"/" , "//" }
42
+ if token .type == OP :
43
+ if token .string not in {"+" , "-" , "*" , "/" , "//" , "^" , "**" , "(" , ")" }:
44
+ raise UndefinedUnitError (f"Unrecognized operator: { token .string } " )
45
+ exponent = token .string in {"^" , "**" }
46
+ division = token .string in {"/" , "//" }
47
+ else :
48
+ exponent , division = False , False
41
49
42
50
for scale , division in scales :
43
51
# There's probably something to be said for stashing these, but this sin
@@ -103,7 +111,10 @@ def parse_units(units: Union[str, Unit, None]) -> Union[str, Unit, None]:
103
111
elif units == '' :
104
112
return 'dimensionless'
105
113
elif isinstance (units , str ):
106
- return f"{ _REGISTRY (units ).u :clean} "
114
+ parsed = _REGISTRY (units )
115
+ if isinstance (parsed , int ) or parsed .magnitude != 1 :
116
+ raise ValueError ("Unit expression cannot have a scaling factor." )
117
+ return f"{ parsed .u :clean} "
107
118
elif isinstance (units , Unit ):
108
119
return units
109
120
else :
0 commit comments