Skip to content

Commit 9bdbaf0

Browse files
authored
Only suppress imports of optional external dependencies (#550)
* Only suppress imports of optional external dependencies * Gather optional dependencies * Fix import order * Add changelog entry
1 parent 3c15300 commit 9bdbaf0

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

docs/release.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Fixes
2424

2525
Improvements
2626
~~~~~~~~~~~~
27+
* If an import error is raised when trying to define a codec that is *not*
28+
an optional dependency, it is no longer silently caught. Instead it will
29+
be propagated to the user, as this indicates an issue with the installed
30+
package.
31+
32+
Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
33+
are still silently caught.
34+
By :user:`David Stansby <dstansby>`, :issue:`550`.
2735

2836

2937
0.14.1

numcodecs/__init__.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,32 @@
3636

3737
register_codec(BZ2)
3838

39-
with suppress(ImportError):
40-
from numcodecs.lzma import LZMA
39+
from numcodecs.lzma import LZMA
4140

42-
register_codec(LZMA)
41+
register_codec(LZMA)
4342

44-
with suppress(ImportError):
45-
from numcodecs import blosc
46-
from numcodecs.blosc import Blosc
47-
48-
register_codec(Blosc)
49-
# initialize blosc
50-
try:
51-
ncores = multiprocessing.cpu_count()
52-
except OSError: # pragma: no cover
53-
ncores = 1
54-
blosc.init()
55-
blosc.set_nthreads(min(8, ncores))
56-
atexit.register(blosc.destroy)
43+
from numcodecs import blosc
44+
from numcodecs.blosc import Blosc
5745

58-
with suppress(ImportError):
59-
from numcodecs import zstd as zstd
60-
from numcodecs.zstd import Zstd
46+
register_codec(Blosc)
47+
# initialize blosc
48+
try:
49+
ncores = multiprocessing.cpu_count()
50+
except OSError: # pragma: no cover
51+
ncores = 1
52+
blosc.init()
53+
blosc.set_nthreads(min(8, ncores))
54+
atexit.register(blosc.destroy)
6155

62-
register_codec(Zstd)
56+
from numcodecs import zstd as zstd
57+
from numcodecs.zstd import Zstd
6358

64-
with suppress(ImportError):
65-
from numcodecs import lz4 as lz4
66-
from numcodecs.lz4 import LZ4
59+
register_codec(Zstd)
6760

68-
register_codec(LZ4)
61+
from numcodecs import lz4 as lz4
62+
from numcodecs.lz4 import LZ4
6963

70-
with suppress(ImportError):
71-
from numcodecs.zfpy import ZFPY
72-
73-
register_codec(ZFPY)
64+
register_codec(LZ4)
7465

7566
from numcodecs.astype import AsType
7667

@@ -112,38 +103,44 @@
112103

113104
register_codec(BitRound)
114105

115-
with suppress(ImportError):
116-
from numcodecs.msgpacks import MsgPack
117-
118-
register_codec(MsgPack)
119-
120106
from numcodecs.checksum32 import CRC32, Adler32, JenkinsLookup3
121107

122108
register_codec(CRC32)
123109
register_codec(Adler32)
124110
register_codec(JenkinsLookup3)
125111

126-
with suppress(ImportError):
127-
from numcodecs.checksum32 import CRC32C
128-
129-
register_codec(CRC32C)
130-
131112
from numcodecs.json import JSON
132113

133114
register_codec(JSON)
134115

135-
with suppress(ImportError):
136-
from numcodecs import vlen as vlen
137-
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
116+
from numcodecs import vlen as vlen
117+
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
138118

139-
register_codec(VLenUTF8)
140-
register_codec(VLenBytes)
141-
register_codec(VLenArray)
119+
register_codec(VLenUTF8)
120+
register_codec(VLenBytes)
121+
register_codec(VLenArray)
142122

143123
from numcodecs.fletcher32 import Fletcher32
144124

145125
register_codec(Fletcher32)
146126

147-
from numcodecs.pcodec import PCodec
127+
# Optional depenedencies
128+
with suppress(ImportError):
129+
from numcodecs.zfpy import ZFPY
130+
131+
register_codec(ZFPY)
132+
133+
with suppress(ImportError):
134+
from numcodecs.msgpacks import MsgPack
135+
136+
register_codec(MsgPack)
137+
138+
with suppress(ImportError):
139+
from numcodecs.checksum32 import CRC32C
140+
141+
register_codec(CRC32C)
142+
143+
with suppress(ImportError):
144+
from numcodecs.pcodec import PCodec
148145

149-
register_codec(PCodec)
146+
register_codec(PCodec)

0 commit comments

Comments
 (0)