Skip to content

Commit 834e978

Browse files
committed
note what may raise
1 parent c4ab5b4 commit 834e978

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

spec/design_topics/python_builtin_types.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,37 @@ builtin types to CPU. In the above example, the `.mean()` call returns a
3030
`float`. It is likely beneficial though to implement this as a library-specific
3131
scalar object which duck types with `float`. This means that it should (a) have
3232
the same semantics as a builtin `float` when used within a library, and (b)
33-
support usage as a `float` outside of the library (i.e., implement
34-
`__float__`). Duck typing is usually not perfect, for example `isinstance`
33+
support usage as a `float` outside of the library (see below).
34+
Duck typing is usually not perfect, for example `isinstance`
3535
usage on the float-like duck type will behave differently. Such explicit "type
3636
of object" checks don't have to be supported.
3737

3838
The following design rule applies everywhere builtin Python types are used
3939
within this API standard: _where a Python builtin type is specified, an
4040
implementation may always replace it by an equivalent library-specific type
4141
that duck types with the Python builtin type._
42+
43+
## Required methods
44+
45+
A ducktyped float scalar is required to implement all the methods which `float` implements.
46+
47+
For example, if a library implements `FancyFloat` and `FancyBool` scalars,
48+
then the following should all be supported:
49+
```python
50+
df: DataFrame
51+
column_1: Column = df.col('a')
52+
column_2: Column = df.col('b')
53+
54+
scalar: FancyFloat = column_1.std()
55+
result_1: Column = column_2 - column_1.std()
56+
result_2: FancyBool = column_2.std() > column_1.std()
57+
```
58+
The following, however, may raise, dependening on the
59+
implementation:
60+
```python
61+
df: DataFrame
62+
column = df.col('a')
63+
64+
if column.std() > 0: # this line may raise!
65+
print('std is positive')
66+
```

0 commit comments

Comments
 (0)