-
Notifications
You must be signed in to change notification settings - Fork 6
/
nep-0042-new-dtypes.html
1822 lines (1610 loc) · 151 KB
/
nep-0042-new-dtypes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-content_root="./" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NEP 42 — New and extensible DTypes — NumPy Enhancement Proposals</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=26a4bc78f4c0ddb94549" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=26a4bc78f4c0ddb94549" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<!-- So that users can add custom icons -->
<script src="_static/scripts/fontawesome.js?digest=26a4bc78f4c0ddb94549"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
<script src="_static/documentation_options.js?v=7f41d439"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'nep-0042-new-dtypes';</script>
<link rel="icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="NEP 44 — Restructuring the NumPy documentation" href="nep-0044-restructuring-numpy-docs.html" />
<link rel="prev" title="NEP 41 — First step towards a new datatype system" href="nep-0041-improved-dtype-support.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
<meta name="docbuild:last-update" content="Nov 22, 2024"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="content.html">
<img src="_static/numpylogo.svg" class="logo__image only-light" alt="NumPy Enhancement Proposals - Home"/>
<img src="_static/numpylogo.svg" class="logo__image only-dark pst-js-only" alt="NumPy Enhancement Proposals - Home"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wish list
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wish list
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="scope.html">The Scope of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="roadmap.html">Current roadmap</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">Wish list</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="meta.html">Meta-NEPs (NEPs about NEPs or active Processes)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0000.html">NEP 0 — Purpose and process</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0023-backwards-compatibility.html">NEP 23 — Backwards compatibility and deprecation policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0036-fair-play.html">NEP 36 — Fair play</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0045-c_style_guide.html">NEP 45 — C style guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0046-sponsorship-guidelines.html">NEP 46 — NumPy sponsorship guidelines</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0048-spending-project-funds.html">NEP 48 — Spending NumPy project funds</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-template.html">NEP X — Template and instructions</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="provisional.html">Provisional NEPs (provisionally accepted; interface may change)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="simple">
</ul>
</details></li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="accepted.html">Accepted NEPs (implementation in progress)</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="nep-0041-improved-dtype-support.html">NEP 41 — First step towards a new datatype system</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">NEP 42 — New and extensible DTypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0044-restructuring-numpy-docs.html">NEP 44 — Restructuring the NumPy documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0051-scalar-representation.html">NEP 51 — Changing the representation of NumPy scalars</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="open.html">Open NEPs (under consideration)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0043-extensible-ufuncs.html">NEP 43 — Enhancing the extensibility of UFuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0053-c-abi-evolution.html">NEP 53 — Evolving the NumPy C-API for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0054-simd-cpp-highway.html">NEP 54 — SIMD infrastructure evolution: adopting Google Highway when moving to C++?</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="finished.html">Finished NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0001-npy-format.html">NEP 1 — A simple file format for NumPy arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0005-generalized-ufuncs.html">NEP 5 — Generalized universal functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0007-datetime-proposal.html">NEP 7 — A proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0010-new-iterator-ufunc.html">NEP 10 — Optimizing iterator/UFunc performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0013-ufunc-overrides.html">NEP 13 — A mechanism for overriding Ufuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0014-dropping-python2.7-proposal.html">NEP 14 — Plan for dropping Python 2.7 support</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0015-merge-multiarray-umath.html">NEP 15 — Merging multiarray and umath</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0018-array-function-protocol.html">NEP 18 — A dispatch mechanism for NumPy's high level array functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0019-rng-policy.html">NEP 19 — Random number generator policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0020-gufunc-signature-enhancement.html">NEP 20 — Expansion of generalized universal function signatures</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0022-ndarray-duck-typing-overview.html">NEP 22 — Duck typing for NumPy arrays – high level overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0027-zero-rank-arrarys.html">NEP 27 — Zero rank arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0028-website-redesign.html">NEP 28 — numpy.org website redesign</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0029-deprecation_policy.html">NEP 29 — Recommend Python and NumPy version support as a community policy standard</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0032-remove-financial-functions.html">NEP 32 — Remove the financial functions from NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0034-infer-dtype-is-object.html">NEP 34 — Disallow inferring ``dtype=object`` from sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0035-array-creation-dispatch-with-array-function.html">NEP 35 — Array creation dispatching with __array_function__</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0038-SIMD-optimizations.html">NEP 38 — Using SIMD optimization instructions for performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0040-legacy-datatype-impl.html">NEP 40 — Legacy datatype implementation in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0049.html">NEP 49 — Data allocation strategies</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0050-scalar-promotion.html">NEP 50 — Promotion rules for Python scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0052-python-api-cleanup.html">NEP 52 — Python API cleanup for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0055-string_dtype.html">NEP 55 — Add a UTF-8 variable-width string DType to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0056-array-api-main-namespace.html">NEP 56 — Array API standard support in NumPy's main namespace</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="deferred.html">Deferred and Superseded NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0002-warnfix.html">NEP 2 — A proposal to build numpy without warning with a big set of warning flags</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0003-math_config_clean.html">NEP 3 — Cleaning the math configuration of numpy.core</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0004-datetime-proposal3.html">NEP 4 — A (third) proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0006-newbugtracker.html">NEP 6 — Replacing Trac with a different bug tracker</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0008-groupby_additions.html">NEP 8 — A proposal for adding groupby functionality to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0009-structured_array_extensions.html">NEP 9 — Structured array extensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0011-deferred-ufunc-evaluation.html">NEP 11 — Deferred UFunc evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0012-missing-data.html">NEP 12 — Missing data functionality in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0021-advanced-indexing.html">NEP 21 — Simplified and explicit advanced indexing</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0024-missing-data-2.html">NEP 24 — Missing data functionality - alternative 1 to NEP 12</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0025-missing-data-3.html">NEP 25 — NA support via special dtypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0026-missing-data-summary.html">NEP 26 — Summary of missing data NEPs and discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0030-duck-array-protocol.html">NEP 30 — Duck typing for NumPy arrays - implementation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0031-uarray.html">NEP 31 — Context-local and global overrides of the NumPy API</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0037-array-module.html">NEP 37 — A dispatch protocol for NumPy-like modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0047-array-api-standard.html">NEP 47 — Adopting the array API standard</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="rejected.html">Rejected and Withdrawn NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0016-abstract-array.html">NEP 16 — An abstract base class for identifying "duck arrays"</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0017-split-out-maskedarray.html">NEP 17 — Split out masked arrays</a></li>
</ul>
</details></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="content.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">Roadmap & NumPy enhancement proposals</a></li>
<li class="breadcrumb-item"><a href="accepted.html" class="nav-link">Accepted NEPs (implementation in progress)</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">NEP 42 — New and extensible DTypes</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="nep-42-new-and-extensible-dtypes">
<span id="nep42"></span><h1>NEP 42 — New and extensible DTypes<a class="headerlink" href="#nep-42-new-and-extensible-dtypes" title="Link to this heading">#</a></h1>
<dl class="field-list simple">
<dt class="field-odd">title<span class="colon">:</span></dt>
<dd class="field-odd"><p>New and extensible DTypes</p>
</dd>
<dt class="field-even">Author<span class="colon">:</span></dt>
<dd class="field-even"><p>Sebastian Berg</p>
</dd>
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Ben Nathanson</p>
</dd>
<dt class="field-even">Author<span class="colon">:</span></dt>
<dd class="field-even"><p>Marten van Kerkwijk</p>
</dd>
<dt class="field-odd">Status<span class="colon">:</span></dt>
<dd class="field-odd"><p>Accepted</p>
</dd>
<dt class="field-even">Type<span class="colon">:</span></dt>
<dd class="field-even"><p>Standard</p>
</dd>
<dt class="field-odd">Created<span class="colon">:</span></dt>
<dd class="field-odd"><p>2019-07-17</p>
</dd>
<dt class="field-even">Resolution<span class="colon">:</span></dt>
<dd class="field-even"><p><a class="reference external" href="https://mail.python.org/pipermail/numpy-discussion/2020-October/081038.html">https://mail.python.org/pipermail/numpy-discussion/2020-October/081038.html</a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This NEP is third in a series:</p>
<ul class="simple">
<li><p><a class="reference internal" href="nep-0040-legacy-datatype-impl.html#nep40"><span class="std std-ref">NEP 40</span></a> explains the shortcomings of NumPy’s dtype implementation.</p></li>
<li><p><a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a> gives an overview of our proposed replacement.</p></li>
<li><p>NEP 42 (this document) describes the new design’s datatype-related APIs.</p></li>
<li><p><a class="reference internal" href="nep-0043-extensible-ufuncs.html#nep43"><span class="std std-ref">NEP 43</span></a> describes the new design’s API for universal functions.</p></li>
</ul>
</div>
<section id="abstract">
<h2>Abstract<a class="headerlink" href="#abstract" title="Link to this heading">#</a></h2>
<p>NumPy’s dtype architecture is monolithic – each dtype is an instance of a
single class. There’s no principled way to expand it for new dtypes, and the
code is difficult to read and maintain.</p>
<p>As <a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a> explains, we are proposing a new architecture that is
modular and open to user additions. dtypes will derive from a new <code class="docutils literal notranslate"><span class="pre">DType</span></code>
class serving as the extension point for new types. <code class="docutils literal notranslate"><span class="pre">np.dtype("float64")</span></code>
will return an instance of a <code class="docutils literal notranslate"><span class="pre">Float64</span></code> class, a subclass of root class
<code class="docutils literal notranslate"><span class="pre">np.dtype</span></code>.</p>
<p>This NEP is one of two that lay out the design and API of this new
architecture. This NEP addresses dtype implementation; <a class="reference internal" href="nep-0043-extensible-ufuncs.html#nep43"><span class="std std-ref">NEP 43</span></a> addresses
universal functions.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Details of the private and external APIs may change to reflect user
comments and implementation constraints. The underlying principles and
choices should not change significantly.</p>
</div>
</section>
<section id="motivation-and-scope">
<h2>Motivation and scope<a class="headerlink" href="#motivation-and-scope" title="Link to this heading">#</a></h2>
<p>Our goal is to allow user code to create fully featured dtypes for a broad
variety of uses, from physical units (such as meters) to domain-specific
representations of geometric objects. <a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a> describes a number
of these new dtypes and their benefits.</p>
<p>Any design supporting dtypes must consider:</p>
<ul class="simple">
<li><p>How shape and dtype are determined when an array is created</p></li>
<li><p>How array elements are stored and accessed</p></li>
<li><p>The rules for casting dtypes to other dtypes</p></li>
</ul>
<p>In addition:</p>
<ul class="simple">
<li><p>We want dtypes to comprise a class hierarchy open to new types and to
subhierarchies, as motivated in <a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a>.</p></li>
</ul>
<p>And to provide this,</p>
<ul class="simple">
<li><p>We need to define a user API.</p></li>
</ul>
<p>All these are the subjects of this NEP.</p>
<ul class="simple">
<li><p>The class hierarchy, its relation to the Python scalar types, and its
important attributes are described in <a class="reference internal" href="#nep42-dtype-class">nep42_DType class</a>.</p></li>
<li><p>The functionality that will support dtype casting is described in <a class="reference internal" href="#casting">Casting</a>.</p></li>
<li><p>The implementation of item access and storage, and the way shape and dtype
are determined when creating an array, are described in <a class="reference internal" href="#nep42-array-coercion"><span class="std std-ref">Coercion to and from Python objects</span></a>.</p></li>
<li><p>The functionality for users to define their own DTypes is described in
<a class="reference internal" href="#public-c-api">Public C-API</a>.</p></li>
</ul>
<p>The API here and in <a class="reference internal" href="nep-0043-extensible-ufuncs.html#nep43"><span class="std std-ref">NEP 43</span></a> is entirely on the C side. A Python-side version
will be proposed in a future NEP. A future Python API is expected to be
similar, but provide a more convenient API to reuse the functionality of
existing DTypes. It could also provide shorthands to create structured DTypes
similar to Python’s
<a class="reference external" href="https://docs.python.org/3.8/library/dataclasses.html">dataclasses</a>.</p>
</section>
<section id="backward-compatibility">
<h2>Backward compatibility<a class="headerlink" href="#backward-compatibility" title="Link to this heading">#</a></h2>
<p>The disruption is expected to be no greater than that of a typical NumPy
release.</p>
<ul class="simple">
<li><p>The main issues are noted in <a class="reference internal" href="nep-0041-improved-dtype-support.html#nep41"><span class="std std-ref">NEP 41</span></a> and will mostly affect
heavy users of the NumPy C-API.</p></li>
<li><p>Eventually we will want to deprecate the API currently used for creating
user-defined dtypes.</p></li>
<li><p>Small, rarely noticed inconsistencies are likely to change. Examples:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">np.array(np.nan,</span> <span class="pre">dtype=np.int64)</span></code> behaves differently from
<code class="docutils literal notranslate"><span class="pre">np.array([np.nan],</span> <span class="pre">dtype=np.int64)</span></code> with the latter raising an error.
This may require identical results (either both error or both succeed).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">np.array([array_like])</span></code> sometimes behaves differently from
<code class="docutils literal notranslate"><span class="pre">np.array([np.array(array_like)])</span></code></p></li>
<li><p>array operations may or may not preserve dtype metadata</p></li>
</ul>
</li>
<li><p>Documentation that describes the internal structure of dtypes will need
to be updated.</p></li>
</ul>
<p>The new code must pass NumPy’s regular test suite, giving some assurance that
the changes are compatible with existing code.</p>
</section>
<section id="usage-and-impact">
<h2>Usage and impact<a class="headerlink" href="#usage-and-impact" title="Link to this heading">#</a></h2>
<p>We believe the few structures in this section are sufficient to consolidate
NumPy’s present functionality and also to support complex user-defined DTypes.</p>
<p>The rest of the NEP fills in details and provides support for the claim.</p>
<p>Again, though Python is used for illustration, the implementation is a C API only; a
future NEP will tackle the Python API.</p>
<p>After implementing this NEP, creating a DType will be possible by implementing
the following outlined DType base class,
that is further described in <a class="reference internal" href="#nep42-dtype-class">nep42_DType class</a>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DType</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">):</span>
<span class="nb">type</span> <span class="p">:</span> <span class="nb">type</span> <span class="c1"># Python scalar type</span>
<span class="n">parametric</span> <span class="p">:</span> <span class="nb">bool</span> <span class="c1"># (may be indicated by superclass)</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">canonical</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="k">def</span> <span class="nf">ensure_canonical</span><span class="p">(</span><span class="bp">self</span> <span class="p">:</span> <span class="n">DType</span><span class="p">)</span> <span class="o">-></span> <span class="n">DType</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
</pre></div>
</div>
<p>For casting, a large part of the functionality is provided by the “methods” stored
in <code class="docutils literal notranslate"><span class="pre">_castingimpl</span></code></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span> <span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">common_dtype</span><span class="p">(</span><span class="bp">cls</span> <span class="p">:</span> <span class="n">DTypeMeta</span><span class="p">,</span> <span class="n">other</span> <span class="p">:</span> <span class="n">DTypeMeta</span><span class="p">)</span> <span class="o">-></span> <span class="n">DTypeMeta</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="k">def</span> <span class="nf">common_instance</span><span class="p">(</span><span class="bp">self</span> <span class="p">:</span> <span class="n">DType</span><span class="p">,</span> <span class="n">other</span> <span class="p">:</span> <span class="n">DType</span><span class="p">)</span> <span class="o">-></span> <span class="n">DType</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="c1"># A mapping of "methods" each detailing how to cast to another DType</span>
<span class="c1"># (further specified at the end of the section)</span>
<span class="n">_castingimpl</span> <span class="o">=</span> <span class="p">{}</span>
</pre></div>
</div>
<p>For array-coercion, also part of casting:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span> <span class="k">def</span> <span class="nf">__dtype_setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item_pointer</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="k">def</span> <span class="nf">__dtype_getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item_pointer</span><span class="p">,</span> <span class="n">base_obj</span><span class="p">)</span> <span class="o">-></span> <span class="nb">object</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">__discover_descr_from_pyobject__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">obj</span> <span class="p">:</span> <span class="nb">object</span><span class="p">)</span> <span class="o">-></span> <span class="n">DType</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="c1"># initially private:</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">_known_scalar_type</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">obj</span> <span class="p">:</span> <span class="nb">object</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
</pre></div>
</div>
<p>Other elements of the casting implementation is the <code class="docutils literal notranslate"><span class="pre">CastingImpl</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">casting</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="s2">"safe"</span><span class="p">,</span> <span class="s2">"same_kind"</span><span class="p">,</span> <span class="s2">"unsafe"</span><span class="p">]</span>
<span class="k">class</span> <span class="nc">CastingImpl</span><span class="p">:</span>
<span class="c1"># Object describing and performing the cast</span>
<span class="n">casting</span> <span class="p">:</span> <span class="n">casting</span>
<span class="k">def</span> <span class="nf">resolve_descriptors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">DTypeMeta</span><span class="p">],</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">DType</span><span class="o">|</span><span class="kc">None</span><span class="p">]</span> <span class="p">:</span> <span class="nb">input</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="n">casting</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">DType</span><span class="p">]):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="c1"># initially private:</span>
<span class="k">def</span> <span class="nf">_get_loop</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="o">-></span> <span class="n">lowlevel_C_loop</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
</pre></div>
</div>
<p>which describes the casting from one DType to another. In
<a class="reference internal" href="nep-0043-extensible-ufuncs.html#nep43"><span class="std std-ref">NEP 43</span></a> this <code class="docutils literal notranslate"><span class="pre">CastingImpl</span></code> object is used unchanged to
support universal functions.
Note that the name <code class="docutils literal notranslate"><span class="pre">CastingImpl</span></code> here will be generically called
<code class="docutils literal notranslate"><span class="pre">ArrayMethod</span></code> to accommodate both casting and universal functions.</p>
</section>
<section id="definitions">
<h2>Definitions<a class="headerlink" href="#definitions" title="Link to this heading">#</a></h2>
<dl class="simple glossary">
<dt id="term-dtype">dtype<a class="headerlink" href="#term-dtype" title="Link to this term">#</a></dt><dd><p>The dtype <em>instance</em>; this is the object attached to a numpy array.</p>
</dd>
<dt id="term-DType">DType<a class="headerlink" href="#term-DType" title="Link to this term">#</a></dt><dd><p>Any subclass of the base type <code class="docutils literal notranslate"><span class="pre">np.dtype</span></code>.</p>
</dd>
<dt id="term-coercion">coercion<a class="headerlink" href="#term-coercion" title="Link to this term">#</a></dt><dd><p>Conversion of Python types to NumPy arrays and values stored in a NumPy
array.</p>
</dd>
<dt id="term-cast">cast<a class="headerlink" href="#term-cast" title="Link to this term">#</a></dt><dd><p>Conversion of an array to a different dtype.</p>
</dd>
<dt id="term-parametric-type">parametric type<a class="headerlink" href="#term-parametric-type" title="Link to this term">#</a></dt><dd><p>A dtype whose representation can change based on a parameter value,
like a string dtype with a length parameter. All members of the current
<code class="docutils literal notranslate"><span class="pre">flexible</span></code> dtype class are parametric. See
<a class="reference internal" href="nep-0040-legacy-datatype-impl.html#parametric-datatype-discussion"><span class="std std-ref">NEP 40</span></a>.</p>
</dd>
<dt id="term-promotion">promotion<a class="headerlink" href="#term-promotion" title="Link to this term">#</a></dt><dd><p>Finding a dtype that can perform an operation on a mix of dtypes without
loss of information.</p>
</dd>
<dt id="term-safe-cast">safe cast<a class="headerlink" href="#term-safe-cast" title="Link to this term">#</a></dt><dd><p>A cast is safe if no information is lost when changing type.</p>
</dd>
</dl>
<p>On the C level we use <code class="docutils literal notranslate"><span class="pre">descriptor</span></code> or <code class="docutils literal notranslate"><span class="pre">descr</span></code> to mean
<em>dtype instance</em>. In the proposed C-API, these terms will distinguish
dtype instances from DType classes.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>NumPy has an existing class hierarchy for scalar types, as
seen <a class="reference internal" href="nep-0040-legacy-datatype-impl.html#nep-0040-dtype-hierarchy"><span class="std std-ref">in the figure</span></a> of
<a class="reference internal" href="nep-0040-legacy-datatype-impl.html#nep40"><span class="std std-ref">NEP 40</span></a>, and the new DType hierarchy will resemble it. The
types are used as an attribute of the single dtype class in the current
NumPy; they’re not dtype classes. They neither harm nor help this work.</p>
</div>
</section>
<section id="the-dtype-class">
<span id="nep42-dtype-class"></span><h2>The DType class<a class="headerlink" href="#the-dtype-class" title="Link to this heading">#</a></h2>
<p>This section reviews the structure underlying the proposed DType class,
including the type hierarchy and the use of abstract DTypes.</p>
<section id="class-getter">
<h3>Class getter<a class="headerlink" href="#class-getter" title="Link to this heading">#</a></h3>
<p>To create a DType instance from a scalar type users now call
<code class="docutils literal notranslate"><span class="pre">np.dtype</span></code> (for instance, <code class="docutils literal notranslate"><span class="pre">np.dtype(np.int64)</span></code>). Sometimes it is
also necessary to access the underlying DType class; this comes up in
particular with type hinting because the “type” of a DType instance is
the DType class. Taking inspiration from type hinting, we propose the
following getter syntax:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">int64</span><span class="p">]</span>
</pre></div>
</div>
<p>to get the DType class corresponding to a scalar type. The notation
works equally well with built-in and user-defined DTypes.</p>
<p>This getter eliminates the need to create an explicit name for every
DType, crowding the <code class="docutils literal notranslate"><span class="pre">np</span></code> namespace; the getter itself signifies the
type. It also opens the possibility of making <code class="docutils literal notranslate"><span class="pre">np.ndarray</span></code> generic
over DType class using annotations like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">]]</span>
</pre></div>
</div>
<p>The above is fairly verbose, so it is possible that we will include
aliases like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Float64</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">]</span>
</pre></div>
</div>
<p>in <code class="docutils literal notranslate"><span class="pre">numpy.typing</span></code>, thus keeping annotations concise but still
avoiding crowding the <code class="docutils literal notranslate"><span class="pre">np</span></code> namespace as discussed above. For a
user-defined DType:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">UserDtype</span><span class="p">(</span><span class="n">dtype</span><span class="p">):</span> <span class="o">...</span>
</pre></div>
</div>
<p>one can do <code class="docutils literal notranslate"><span class="pre">np.ndarray[UserDtype]</span></code>, keeping annotations concise in
that case without introducing boilerplate in NumPy itself. For a
user-defined scalar type:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">UserScalar</span><span class="p">(</span><span class="n">generic</span><span class="p">):</span> <span class="o">...</span>
</pre></div>
</div>
<p>we would need to add a typing overload to <code class="docutils literal notranslate"><span class="pre">dtype</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@overload</span>
<span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">dtype</span><span class="p">:</span> <span class="n">Type</span><span class="p">[</span><span class="n">UserScalar</span><span class="p">],</span> <span class="o">...</span><span class="p">)</span> <span class="o">-></span> <span class="n">UserDtype</span>
</pre></div>
</div>
<p>to allow <code class="docutils literal notranslate"><span class="pre">np.dtype[UserScalar]</span></code>.</p>
<p>The initial implementation probably will return only concrete (not abstract)
DTypes.</p>
<p><em>This item is still under review.</em></p>
</section>
<section id="hierarchy-and-abstract-classes">
<h3>Hierarchy and abstract classes<a class="headerlink" href="#hierarchy-and-abstract-classes" title="Link to this heading">#</a></h3>
<p>We will use abstract classes as building blocks of our extensible DType class
hierarchy.</p>
<ol class="arabic simple">
<li><p>Abstract classes are inherited cleanly, in principle allowing checks like
<code class="docutils literal notranslate"><span class="pre">isinstance(np.dtype("float64"),</span> <span class="pre">np.inexact)</span></code>.</p></li>
<li><p>Abstract classes allow a single piece of code to handle a multiplicity of
input types. Code written to accept Complex objects can work with numbers
of any precision; the precision of the results is determined by the
precision of the arguments.</p></li>
<li><p>There’s room for user-created families of DTypes. We can envision an
abstract <code class="docutils literal notranslate"><span class="pre">Unit</span></code> class for physical units, with a concrete subclass like
<code class="docutils literal notranslate"><span class="pre">Float64Unit</span></code>. Calling <code class="docutils literal notranslate"><span class="pre">Unit(np.float64,</span> <span class="pre">"m")</span></code> (<code class="docutils literal notranslate"><span class="pre">m</span></code> for meters) would
be equivalent to <code class="docutils literal notranslate"><span class="pre">Float64Unit("m")</span></code>.</p></li>
<li><p>The implementation of universal functions in <a class="reference internal" href="nep-0043-extensible-ufuncs.html#nep43"><span class="std std-ref">NEP 43</span></a> may require
a class hierarchy.</p></li>
</ol>
<p><strong>Example:</strong> A NumPy <code class="docutils literal notranslate"><span class="pre">Categorical</span></code> class would be a match for pandas
<code class="docutils literal notranslate"><span class="pre">Categorical</span></code> objects, which can contain integers or general Python objects.
NumPy needs a DType that it can assign a Categorical to, but it also needs
DTypes like <code class="docutils literal notranslate"><span class="pre">CategoricalInt64</span></code> and <code class="docutils literal notranslate"><span class="pre">CategoricalObject</span></code> such that
<code class="docutils literal notranslate"><span class="pre">common_dtype(CategoricalInt64,</span> <span class="pre">String)</span></code> raises an error, but
<code class="docutils literal notranslate"><span class="pre">common_dtype(CategoricalObject,</span> <span class="pre">String)</span></code> returns an <code class="docutils literal notranslate"><span class="pre">object</span></code> DType. In
our scheme, <code class="docutils literal notranslate"><span class="pre">Categorical</span></code> is an abstract type with <code class="docutils literal notranslate"><span class="pre">CategoricalInt64</span></code> and
<code class="docutils literal notranslate"><span class="pre">CategoricalObject</span></code> subclasses.</p>
<p>Rules for the class structure, illustrated <a class="reference internal" href="#nep42-hierarchy-figure"><span class="std std-ref">below</span></a>:</p>
<ol class="arabic simple">
<li><p>Abstract DTypes cannot be instantiated. Instantiating an abstract DType
raises an error, or perhaps returns an instance of a concrete subclass.
Raising an error will be the default behavior and may be required initially.</p></li>
<li><p>While abstract DTypes may be superclasses, they may also act like Python’s
abstract base classes (ABC) allowing registration instead of subclassing.
It may be possible to simply use or inherit from Python ABCs.</p></li>
<li><p>Concrete DTypes may not be subclassed. In the future this might be relaxed
to allow specialized implementations such as a GPU float64 subclassing a
NumPy float64.</p></li>
</ol>
<p>The
<a class="reference external" href="https://docs.julialang.org/en/v1/manual/types/#man-abstract-types-1">Julia language</a>
has a similar prohibition against subclassing concrete types.
For example methods such as the later <code class="docutils literal notranslate"><span class="pre">__common_instance__</span></code> or
<code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code> cannot work for a subclass unless they were designed
very carefully.
It helps avoid unintended vulnerabilities to implementation changes that
result from subclassing types that were not written to be subclassed.
We believe that the DType API should rather be extended to simplify wrapping
of existing functionality.</p>
<p>The DType class requires C-side storage of methods and additional information,
to be implemented by a <code class="docutils literal notranslate"><span class="pre">DTypeMeta</span></code> class. Each <code class="docutils literal notranslate"><span class="pre">DType</span></code> class is an
instance of <code class="docutils literal notranslate"><span class="pre">DTypeMeta</span></code> with a well-defined and extensible interface;
end users ignore it.</p>
<figure class="align-center align-default" id="nep42-hierarchy-figure">
<img alt="_images/dtype_hierarchy.svg" src="_images/dtype_hierarchy.svg" /></figure>
</section>
<section id="miscellaneous-methods-and-attributes">
<h3>Miscellaneous methods and attributes<a class="headerlink" href="#miscellaneous-methods-and-attributes" title="Link to this heading">#</a></h3>
<p>This section collects definitions in the DType class that are not used in
casting and array coercion, which are described in detail below.</p>
<ul class="simple">
<li><p>Existing dtype methods (<a class="reference external" href="https://numpy.org/devdocs/reference/generated/numpy.dtype.html#numpy.dtype" title="(in NumPy v2.2.dev0)"><code class="xref py py-class docutils literal notranslate"><span class="pre">numpy.dtype</span></code></a>) and C-side fields are preserved.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">DType.type</span></code> replaces <code class="docutils literal notranslate"><span class="pre">dtype.type</span></code>. Unless a use case arises,
<code class="docutils literal notranslate"><span class="pre">dtype.type</span></code> will be deprecated.
This indicates a Python scalar type which represents the same values as
the DType. This is the same type as used in the proposed <a class="reference internal" href="#class-getter">Class getter</a>
and for <a class="reference internal" href="#dtype-discovery-during-array-coercion">DType discovery during array coercion</a>.
(This can may also be set for abstract DTypes, this is necessary
for array coercion.)</p></li>
<li><p>A new <code class="docutils literal notranslate"><span class="pre">self.canonical</span></code> property generalizes the notion of byte order to
indicate whether data has been stored in a default/canonical way. For
existing code, “canonical” will just signify native byte order, but it can
take on new meanings in new DTypes – for instance, to distinguish a
complex-conjugated instance of Complex which stores <code class="docutils literal notranslate"><span class="pre">real</span> <span class="pre">-</span> <span class="pre">imag</span></code> instead
of <code class="docutils literal notranslate"><span class="pre">real</span> <span class="pre">+</span> <span class="pre">imag</span></code>. The ISNBO (“is
native byte order”) flag might be repurposed as the canonical flag.</p></li>
<li><p>Support is included for parametric DTypes. A DType will be deemed parametric
if it inherits from ParametricDType.</p></li>
<li><p>DType methods may resemble or even reuse existing Python slots. Thus Python
special slots are off-limits for user-defined DTypes (for instance, defining
<code class="docutils literal notranslate"><span class="pre">Unit("m")</span> <span class="pre">></span> <span class="pre">Unit("cm")</span></code>), since we may want to develop a meaning for these
operators that is common to all DTypes.</p></li>
<li><p>Sorting functions are moved to the DType class. They may be implemented by
defining a method <code class="docutils literal notranslate"><span class="pre">dtype_get_sort_function(self,</span> <span class="pre">sortkind="stable")</span> <span class="pre">-></span>
<span class="pre">sortfunction</span></code> that must return <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> if the given <code class="docutils literal notranslate"><span class="pre">sortkind</span></code>
is not known.</p></li>
<li><p>Functions that cannot be removed are implemented as special methods.
Many of these were previously defined part of the <a class="reference external" href="https://numpy.org/devdocs/reference/c-api/types-and-structures.html#c.PyArray_ArrFuncs" title="(in NumPy v2.2.dev0)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyArray_ArrFuncs</span></code></a>
slot of the dtype instance (<code class="docutils literal notranslate"><span class="pre">PyArray_Descr</span> <span class="pre">*</span></code>) and include functions
such as <code class="docutils literal notranslate"><span class="pre">nonzero</span></code>, <code class="docutils literal notranslate"><span class="pre">fill</span></code> (used for <code class="docutils literal notranslate"><span class="pre">np.arange</span></code>), and
<code class="docutils literal notranslate"><span class="pre">fromstr</span></code> (used to parse text files).
These old methods will be deprecated and replacements
following the new design principles added.
The API is not defined here. Since these methods can be deprecated and renamed
replacements added, it is acceptable if these new methods have to be modified.</p></li>
<li><p>Use of <code class="docutils literal notranslate"><span class="pre">kind</span></code> for non-built-in types is discouraged in favor of
<code class="docutils literal notranslate"><span class="pre">isinstance</span></code> checks. <code class="docutils literal notranslate"><span class="pre">kind</span></code> will return the <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code> of the
object to ensure uniqueness for all DTypes. On the C side, <code class="docutils literal notranslate"><span class="pre">kind</span></code> and
<code class="docutils literal notranslate"><span class="pre">char</span></code> are set to <code class="docutils literal notranslate"><span class="pre">\0</span></code> (NULL character).
While <code class="docutils literal notranslate"><span class="pre">kind</span></code> will be discouraged, the current <code class="docutils literal notranslate"><span class="pre">np.issubdtype</span></code>
may remain the preferred method for this type of check.</p></li>
<li><p>A method <code class="docutils literal notranslate"><span class="pre">ensure_canonical(self)</span> <span class="pre">-></span> <span class="pre">dtype</span></code> returns a new dtype (or
<code class="docutils literal notranslate"><span class="pre">self</span></code>) with the <code class="docutils literal notranslate"><span class="pre">canonical</span></code> flag set.</p></li>
<li><p>Since NumPy’s approach is to provide functionality through unfuncs,
functions like sorting that will be implemented in DTypes might eventually be
reimplemented as generalized ufuncs.</p></li>
</ul>
</section>
</section>
<section id="casting">
<span id="nep-42-casting"></span><h2>Casting<a class="headerlink" href="#casting" title="Link to this heading">#</a></h2>
<p>We review here the operations related to casting arrays:</p>
<ul class="simple">
<li><p>Finding the “common dtype,” returned by <a class="reference external" href="https://numpy.org/devdocs/reference/generated/numpy.promote_types.html#numpy.promote_types" title="(in NumPy v2.2.dev0)"><code class="xref py py-func docutils literal notranslate"><span class="pre">numpy.promote_types()</span></code></a> and
<a class="reference external" href="https://numpy.org/devdocs/reference/generated/numpy.result_type.html#numpy.result_type" title="(in NumPy v2.2.dev0)"><code class="xref py py-func docutils literal notranslate"><span class="pre">numpy.result_type()</span></code></a></p></li>
<li><p>The result of calling <a class="reference external" href="https://numpy.org/devdocs/reference/generated/numpy.can_cast.html#numpy.can_cast" title="(in NumPy v2.2.dev0)"><code class="xref py py-func docutils literal notranslate"><span class="pre">numpy.can_cast()</span></code></a></p></li>
</ul>
<p>We show how casting arrays with <code class="docutils literal notranslate"><span class="pre">astype(new_dtype)</span></code> will be implemented.</p>
<section id="common-dtype-operations">
<h3><cite>Common DType</cite> operations<a class="headerlink" href="#common-dtype-operations" title="Link to this heading">#</a></h3>
<p>When input types are mixed, a first step is to find a DType that can hold
the result without loss of information – a “common DType.”</p>
<p>Array coercion and concatenation both return a common dtype instance. Most
universal functions use the common DType for dispatching, though they might
not use it for a result (for instance, the result of a comparison is always
bool).</p>
<p>We propose the following implementation:</p>
<ul>
<li><p>For two DType classes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">__common_dtype__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">other</span> <span class="p">:</span> <span class="n">DTypeMeta</span><span class="p">)</span> <span class="o">-></span> <span class="n">DTypeMeta</span>
</pre></div>
</div>
<p>Returns a new DType, often one of the inputs, which can represent values
of both input DTypes. This should usually be minimal:
the common DType of <code class="docutils literal notranslate"><span class="pre">Int16</span></code> and <code class="docutils literal notranslate"><span class="pre">Uint16</span></code> is <code class="docutils literal notranslate"><span class="pre">Int32</span></code> and not <code class="docutils literal notranslate"><span class="pre">Int64</span></code>.
<code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code> may return NotImplemented to defer to other and,
like Python operators, subclasses take precedence (their
<code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code> method is tried first).</p>
</li>
<li><p>For two instances of the same DType:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">__common_instance__</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">SelfT</span><span class="p">,</span> <span class="n">other</span> <span class="p">:</span> <span class="n">SelfT</span><span class="p">)</span> <span class="o">-></span> <span class="n">SelfT</span>
</pre></div>
</div>
<p>For nonparametric built-in dtypes, this returns a canonicalized copy of
<code class="docutils literal notranslate"><span class="pre">self</span></code>, preserving metadata. For nonparametric user types, this provides
a default implementation.</p>
</li>
<li><p>For instances of different DTypes, for example <code class="docutils literal notranslate"><span class="pre">>float64</span></code> and <code class="docutils literal notranslate"><span class="pre">S8</span></code>,
the operation is done in three steps:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">Float64.__common_dtype__(type(>float64),</span> <span class="pre">type(S8))</span></code>
returns <code class="docutils literal notranslate"><span class="pre">String</span></code> (or defers to <code class="docutils literal notranslate"><span class="pre">String.__common_dtype__</span></code>).</p></li>
<li><p>The casting machinery (explained in detail below) provides the
information that <code class="docutils literal notranslate"><span class="pre">">float64"</span></code> casts to <code class="docutils literal notranslate"><span class="pre">"S32"</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">String.__common_instance__("S8",</span> <span class="pre">"S32")</span></code> returns the final <code class="docutils literal notranslate"><span class="pre">"S32"</span></code>.</p></li>
</ol>
</li>
</ul>
<p>The benefit of this handoff is to reduce duplicated code and keep concerns
separate. DType implementations don’t need to know how to cast, and the
results of casting can be extended to new types, such as a new string encoding.</p>
<p>This means the implementation will work like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">common_dtype</span><span class="p">(</span><span class="n">DType1</span><span class="p">,</span> <span class="n">DType2</span><span class="p">):</span>
<span class="n">common_dtype</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">dtype1</span><span class="p">)</span><span class="o">.</span><span class="n">__common_dtype__</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">dtype2</span><span class="p">))</span>
<span class="k">if</span> <span class="n">common_dtype</span> <span class="ow">is</span> <span class="bp">NotImplemented</span><span class="p">:</span>
<span class="n">common_dtype</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">dtype2</span><span class="p">)</span><span class="o">.</span><span class="n">__common_dtype__</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">dtype1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">common_dtype</span> <span class="ow">is</span> <span class="bp">NotImplemented</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">"no common dtype"</span><span class="p">)</span>
<span class="k">return</span> <span class="n">common_dtype</span>
<span class="k">def</span> <span class="nf">promote_types</span><span class="p">(</span><span class="n">dtype1</span><span class="p">,</span> <span class="n">dtype2</span><span class="p">):</span>
<span class="n">common</span> <span class="o">=</span> <span class="n">common_dtype</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">dtype1</span><span class="p">),</span> <span class="nb">type</span><span class="p">(</span><span class="n">dtype2</span><span class="p">))</span>
<span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">dtype1</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">common</span><span class="p">:</span>
<span class="c1"># Find what dtype1 is cast to when cast to the common DType</span>
<span class="c1"># by using the CastingImpl as described below:</span>
<span class="n">castingimpl</span> <span class="o">=</span> <span class="n">get_castingimpl</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">dtype1</span><span class="p">),</span> <span class="n">common</span><span class="p">)</span>
<span class="n">safety</span><span class="p">,</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">dtype1</span><span class="p">)</span> <span class="o">=</span> <span class="n">castingimpl</span><span class="o">.</span><span class="n">resolve_descriptors</span><span class="p">(</span>
<span class="p">(</span><span class="n">common</span><span class="p">,</span> <span class="n">common</span><span class="p">),</span> <span class="p">(</span><span class="n">dtype1</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
<span class="k">assert</span> <span class="n">safety</span> <span class="o">==</span> <span class="s2">"safe"</span> <span class="c1"># promotion should normally be a safe cast</span>
<span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">dtype2</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">common</span><span class="p">:</span>
<span class="c1"># Same as above branch for dtype1.</span>
<span class="k">if</span> <span class="n">dtype1</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">dtype2</span><span class="p">:</span>
<span class="k">return</span> <span class="n">common</span><span class="o">.</span><span class="n">__common_instance__</span><span class="p">(</span><span class="n">dtype1</span><span class="p">,</span> <span class="n">dtype2</span><span class="p">)</span>
</pre></div>
</div>
<p>Some of these steps may be optimized for nonparametric DTypes.</p>
<p>Since the type returned by <code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code> is not necessarily one of the
two arguments, it’s not equivalent to NumPy’s “safe” casting.
Safe casting works for <code class="docutils literal notranslate"><span class="pre">np.promote_types(int16,</span> <span class="pre">int64)</span></code>, which returns
<code class="docutils literal notranslate"><span class="pre">int64</span></code>, but fails for:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">promote_types</span><span class="p">(</span><span class="s2">"int64"</span><span class="p">,</span> <span class="s2">"float32"</span><span class="p">)</span> <span class="o">-></span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s2">"float64"</span><span class="p">)</span>
</pre></div>
</div>
<p>It is the responsibility of the DType author to ensure that the inputs
can be safely cast to the <code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code>.</p>
<p>Exceptions may apply. For example, casting <code class="docutils literal notranslate"><span class="pre">int32</span></code> to
a (long enough) string is at least at this time considered “safe”.
However <code class="docutils literal notranslate"><span class="pre">np.promote_types(int32,</span> <span class="pre">String)</span></code> will <em>not</em> be defined.</p>
<p><strong>Example:</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">object</span></code> always chooses <code class="docutils literal notranslate"><span class="pre">object</span></code> as the common DType. For
<code class="docutils literal notranslate"><span class="pre">datetime64</span></code> type promotion is defined with no other datatype, but if
someone were to implement a new higher precision datetime, then:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">HighPrecisionDatetime</span><span class="o">.</span><span class="n">__common_dtype__</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">])</span>
</pre></div>
</div>
<p>would return <code class="docutils literal notranslate"><span class="pre">HighPrecisionDatetime</span></code>, and the casting implementation,
as described below, may need to decide how to handle the datetime unit.</p>
<p><strong>Alternatives:</strong></p>
<ul>
<li><p>We’re pushing the decision on common DTypes to the DType classes. Suppose
instead we could turn to a universal algorithm based on safe casting,
imposing a total order on DTypes and returning the first type that both
arguments could cast to safely.</p>
<p>It would be difficult to devise a reasonable total order, and it would have
to accept new entries. Beyond that, the approach is flawed because
importing a type can change the behavior of a program. For example, a
program requiring the common DType of <code class="docutils literal notranslate"><span class="pre">int16</span></code> and <code class="docutils literal notranslate"><span class="pre">uint16</span></code> would
ordinarily get the built-in type <code class="docutils literal notranslate"><span class="pre">int32</span></code> as the first match; if the
program adds <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">int24</span></code>, the first match becomes <code class="docutils literal notranslate"><span class="pre">int24</span></code> and the
smaller type might make the program overflow for the first time. <a class="footnote-reference brackets" href="#id3" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
</li>
<li><p>A more flexible common DType could be implemented in the future where
<code class="docutils literal notranslate"><span class="pre">__common_dtype__</span></code> relies on information from the casting logic.
Since <code class="docutils literal notranslate"><span class="pre">__commond_dtype__</span></code> is a method a such a default implementation
could be added at a later time.</p></li>
<li><p>The three-step handling of differing dtypes could, of course, be coalesced.
It would lose the value of splitting in return for a possibly faster
execution. But few cases would benefit. Most cases, such as array coercion,
involve a single Python type (and thus dtype).</p></li>
</ul>
</section>
<section id="the-cast-operation">
<h3>The cast operation<a class="headerlink" href="#the-cast-operation" title="Link to this heading">#</a></h3>
<p>Casting is perhaps the most complex and interesting DType operation. It
is much like a typical universal function on arrays, converting one input to a
new output, with two distinctions:</p>
<ul class="simple">
<li><p>Casting always requires an explicit output datatype.</p></li>
<li><p>The NumPy iterator API requires access to functions that are lower-level
than what universal functions currently need.</p></li>
</ul>
<p>Casting can be complex and may not implement all details of each input