-
Notifications
You must be signed in to change notification settings - Fork 11
Fixes for dataclass_array_container #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Not loving either change, unfortunately. Let's discuss.
17dbce4
to
eae95b5
Compare
arraycontext/container/__init__.py
Outdated
@@ -173,6 +173,10 @@ def is_array_container_type(cls: type) -> bool: | |||
function will say that :class:`numpy.ndarray` is an array container | |||
type, only object arrays *actually are* array containers. | |||
""" | |||
assert isinstance(cls, type), \ | |||
f"must pass a type, not an instance: '{cls!r}'" | |||
assert hasattr(cls, "__mro__"), "'cls' has no attribute '__mro__': " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, is this __mro__
check needed? Everything that is an instance of type
should have an __mro__
defined, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's needed.
Just to check, isinstance(..., type)
does seem to successfully rule out the typing
stuff:
>>> from typing import Sequence
>>> isinstance(Sequence, type)
False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking! Removed in 1a28536.
eae95b5
to
e1d0dc6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM with the MRO check removed.
arraycontext/container/__init__.py
Outdated
@@ -173,6 +173,10 @@ def is_array_container_type(cls: type) -> bool: | |||
function will say that :class:`numpy.ndarray` is an array container | |||
type, only object arrays *actually are* array containers. | |||
""" | |||
assert isinstance(cls, type), \ | |||
f"must pass a type, not an instance: '{cls!r}'" | |||
assert hasattr(cls, "__mro__"), "'cls' has no attribute '__mro__': " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's needed.
Just to check, isinstance(..., type)
does seem to successfully rule out the typing
stuff:
>>> from typing import Sequence
>>> isinstance(Sequence, type)
False
Thanks! |
There's a couple of random fixes to the dataclass helper in here:
Add an assert in
is_array_container_type
thatisinstance(cls, type)
. If a dataclass has anOptional
orUnion
field (which aren't classes),is_array_container_type
would raise someAttributeError
anyway, but with a more cryptic message.Added some checks in
dataclass_array_container
to give better error messages when it encounters unsuported field types: withinit=False
, string annotations or classes wrapped inOptional
(or equivalent).