Skip to content
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

Merged
merged 2 commits into from
Nov 15, 2024
Merged

Conversation

alexfikl
Copy link
Collaborator

Started using these in some of the direct solver annotations, so thought it's a good time to add them to the docs.

Comment on lines -570 to +573
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]
Copy link
Collaborator Author

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?

Copy link
Owner

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__?

Copy link
Collaborator Author

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]

Copy link
Collaborator Author

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]

Copy link
Owner

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:

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.

Copy link
Collaborator Author

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)

@inducer inducer merged commit b009509 into inducer:main Nov 15, 2024
9 checks passed
@alexfikl alexfikl deleted the matrix-builder-docs branch November 15, 2024 21:12
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