Skip to content

Commit

Permalink
Only suppress imports of optional external dependencies (#550)
Browse files Browse the repository at this point in the history
* Only suppress imports of optional external dependencies

* Gather optional dependencies

* Fix import order

* Add changelog entry
  • Loading branch information
dstansby authored Dec 3, 2024
1 parent 3c15300 commit 9bdbaf0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
8 changes: 8 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Fixes

Improvements
~~~~~~~~~~~~
* If an import error is raised when trying to define a codec that is *not*
an optional dependency, it is no longer silently caught. Instead it will
be propagated to the user, as this indicates an issue with the installed
package.

Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
are still silently caught.
By :user:`David Stansby <dstansby>`, :issue:`550`.


0.14.1
Expand Down
89 changes: 43 additions & 46 deletions numcodecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,32 @@

register_codec(BZ2)

with suppress(ImportError):
from numcodecs.lzma import LZMA
from numcodecs.lzma import LZMA

register_codec(LZMA)
register_codec(LZMA)

with suppress(ImportError):
from numcodecs import blosc
from numcodecs.blosc import Blosc

register_codec(Blosc)
# initialize blosc
try:
ncores = multiprocessing.cpu_count()
except OSError: # pragma: no cover
ncores = 1
blosc.init()
blosc.set_nthreads(min(8, ncores))
atexit.register(blosc.destroy)
from numcodecs import blosc
from numcodecs.blosc import Blosc

with suppress(ImportError):
from numcodecs import zstd as zstd
from numcodecs.zstd import Zstd
register_codec(Blosc)
# initialize blosc
try:
ncores = multiprocessing.cpu_count()
except OSError: # pragma: no cover
ncores = 1
blosc.init()
blosc.set_nthreads(min(8, ncores))
atexit.register(blosc.destroy)

register_codec(Zstd)
from numcodecs import zstd as zstd
from numcodecs.zstd import Zstd

with suppress(ImportError):
from numcodecs import lz4 as lz4
from numcodecs.lz4 import LZ4
register_codec(Zstd)

register_codec(LZ4)
from numcodecs import lz4 as lz4
from numcodecs.lz4 import LZ4

with suppress(ImportError):
from numcodecs.zfpy import ZFPY

register_codec(ZFPY)
register_codec(LZ4)

from numcodecs.astype import AsType

Expand Down Expand Up @@ -112,38 +103,44 @@

register_codec(BitRound)

with suppress(ImportError):
from numcodecs.msgpacks import MsgPack

register_codec(MsgPack)

from numcodecs.checksum32 import CRC32, Adler32, JenkinsLookup3

register_codec(CRC32)
register_codec(Adler32)
register_codec(JenkinsLookup3)

with suppress(ImportError):
from numcodecs.checksum32 import CRC32C

register_codec(CRC32C)

from numcodecs.json import JSON

register_codec(JSON)

with suppress(ImportError):
from numcodecs import vlen as vlen
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
from numcodecs import vlen as vlen
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8

register_codec(VLenUTF8)
register_codec(VLenBytes)
register_codec(VLenArray)
register_codec(VLenUTF8)
register_codec(VLenBytes)
register_codec(VLenArray)

from numcodecs.fletcher32 import Fletcher32

register_codec(Fletcher32)

from numcodecs.pcodec import PCodec
# Optional depenedencies
with suppress(ImportError):
from numcodecs.zfpy import ZFPY

register_codec(ZFPY)

with suppress(ImportError):
from numcodecs.msgpacks import MsgPack

register_codec(MsgPack)

with suppress(ImportError):
from numcodecs.checksum32 import CRC32C

register_codec(CRC32C)

with suppress(ImportError):
from numcodecs.pcodec import PCodec

register_codec(PCodec)
register_codec(PCodec)

0 comments on commit 9bdbaf0

Please sign in to comment.