-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add matrix builders to docs #245
Conversation
return componentwise(make_op, operand) | ||
# FIXME: mypy is right: new should return `cls` instances and we're | ||
# abusing it to vectorize the call like this. | ||
return componentwise(make_op, operand) # type: ignore[return-value] |
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.
Not quite sure what else to do about this?
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.
https://docs.python.org/3/reference/datamodel.html#object.__new__ says:
If
__new__()
does not return an instance of cls, then the new instance’s__init__()
method will not be invoked.
I read this as implicit permission for __new__
to return whatever it wants.
What happens if you're honest in the type annotation for __new__
?
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.
At least mypy is unhappy
pytential/symbolic/primitives.py:558: error: "__new__" must return a class instance (got "NumReferenceDerivative | ndarray[Any, dtype[Any]]") [misc]
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.
Also tried doing an overload like
@overload
def __new__(cls,
ref_axes: int | tuple[tuple[int, int], ...] | None = None,
operand: Expression | None = None,
dofdesc: DOFDescriptor | None = None,
) -> "NumReferenceDerivative": ...
@overload
def __new__(cls,
ref_axes: int | tuple[tuple[int, int], ...] | None = None,
operand: Union["np.ndarray[Any, np.dtype[Any]]", None] = None,
dofdesc: DOFDescriptor | None = None,
) -> "np.ndarray[Any, np.dtype[Any]]": ...
and it still gives
pytential/symbolic/primitives.py:566: error: Incompatible return type for "__new__" (returns "ndarray[Any, dtype[Any]]", but must return a subtype of "NumReferenceDerivative") [misc]
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.
Mypy issue/PR rabbit hole in reverse chronological order:
- Honor return type of
__new__
even if not a subclass python/mypy#15182 - Honor return type of
__new__
python/mypy#7188 - Honor return type of
__new__
python/mypy#1020
Including a BDFL quote:
Honestly I don't think that A() should ever return something that's not an instance of A (or a subclass), even though technically new() can return anything it likes.
I don't think there's any way that mypy will learn what's going on here in the short term. Stylistically, it may be better to move away from overloading __new__
and maybe instead have the user call componentwise
directly. But that's a different discussion, #246.
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.
He does agree that this sort of symbolic simplification usage is quite pleasant though 😁
python/mypy#1020 (comment)
Started using these in some of the direct solver annotations, so thought it's a good time to add them to the docs.