Skip to content

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

Merged
merged 3 commits into from
Oct 29, 2021

Conversation

alexfikl
Copy link
Collaborator

@alexfikl alexfikl commented Oct 26, 2021

There's a couple of random fixes to the dataclass helper in here:

  1. Add an assert in is_array_container_type that isinstance(cls, type). If a dataclass has an Optional or Union field (which aren't classes), is_array_container_type would raise some AttributeError anyway, but with a more cryptic message.

  2. Added some checks in dataclass_array_container to give better error messages when it encounters unsuported field types: with init=False, string annotations or classes wrapped in Optional (or equivalent).

@alexfikl
Copy link
Collaborator Author

alexfikl commented Oct 26, 2021

Snuck in another dataclass-related fix in here. If any fields are Optional[MyType] or Union[SomeType, SomeOtherType], the singledispatch stuff raises. Added a check in is_array_container_type that the cls actually has an __mro__.

@alexfikl alexfikl changed the title Assert dataclass_array_container fields are not stringly-typed Fixed for dataclass_array_container Oct 26, 2021
@alexfikl alexfikl changed the title Fixed for dataclass_array_container Fixes for dataclass_array_container Oct 26, 2021
@alexfikl alexfikl requested a review from inducer October 26, 2021 03:15
Copy link
Owner

@inducer inducer left a 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.

@alexfikl alexfikl force-pushed the dataclass-container-strings branch from 17dbce4 to eae95b5 Compare October 28, 2021 00:33
@@ -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__': "
Copy link
Collaborator Author

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?

Copy link
Owner

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

Copy link
Collaborator Author

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.

@alexfikl alexfikl force-pushed the dataclass-container-strings branch from eae95b5 to e1d0dc6 Compare October 29, 2021 19:33
Copy link
Owner

@inducer inducer left a 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.

@@ -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__': "
Copy link
Owner

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

@inducer inducer enabled auto-merge (rebase) October 29, 2021 22:54
@inducer
Copy link
Owner

inducer commented Oct 29, 2021

Thanks!

@inducer inducer merged commit f6400a4 into inducer:main Oct 29, 2021
@alexfikl alexfikl deleted the dataclass-container-strings branch October 29, 2021 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants