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

Use annotated for array #4679

Merged
merged 11 commits into from May 25, 2023
Merged

Use annotated for array #4679

merged 11 commits into from May 25, 2023

Conversation

cielavenir
Copy link
Contributor

@cielavenir cielavenir commented May 22, 2023

Description

Addressed #3916 (comment) Fixes #3912

Suggested changelog entry:

* Fixes docstring generation for ``std::array``-list caster. Previously, signatures included the size of the list in a non-standard, non-spec compliant way. The new format conforms to PEP 593. **Tooling for processing the docstrings may need to be updated accordingly.**

(oh I copy-pasted 3916)


/cc @Skylion007
/cc @felixvd

@rwgk
Copy link
Collaborator

rwgk commented May 22, 2023

Hi @cielavenir did you already see the AppVeyor failure? It looks like a couple more tests need updating. I'm holding off triggering the rest of the GitHub Actions until you get a chance to fix. I believe without a fix there will be so many failures that we're not learning much from the full results.

@cielavenir
Copy link
Contributor Author

@rwgk thank you, fixed

@cielavenir
Copy link
Contributor Author

oh why valarray is array_caster not list_caster? hopefully fixed though

@rwgk
Copy link
Collaborator

rwgk commented May 22, 2023

(I triggered a rerun to get rid of a notorious flake.)

@rwgk
Copy link
Collaborator

rwgk commented May 22, 2023

I ran this by my team mates (includes pytype developers)

they didn't like the plain 2

so I looked here: https://docs.python.org/3/library/typing.html#typing.Annotated

where I see e.g. Annotated[list[tuple[int, int]], MaxLen(10)]

based on that I suggested Annotated[List[int], FixedSize(2)]

which they liked but pointed out that FixedSize needs to be defined somewhere:

$ python3.11
>>> from typing import Annotated
>>> def f() -> Annotated[list[int], FixedSize(2)]: ...
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'FixedSize' is not defined

I'm totally out of my depth ... how does that idea play with pybind11-stubgen?

@cielavenir
Copy link
Contributor Author

cielavenir commented May 22, 2023

It works with future

In [1]: from __future__ import annotations

In [2]: from typing import Annotated

In [3]: def f() -> Annotated[list[int], FixedSize(2)]: pass

(but still, let me check mypy output)

@cielavenir
Copy link
Contributor Author

@rwgk oh, mypy works (after another patch of pybind11-stubgen) but pyright shows this __init__.pyi:182:68 - error: "FixedSize" is not defined (reportUndefinedVariable)

@cielavenir
Copy link
Contributor Author

in Python3 typing documentation, ValueRange and MaxLen are not defined - how they write sample code in the way pyright does not understand?

@cielavenir
Copy link
Contributor Author

@felixvd
Copy link

felixvd commented May 23, 2023

As mentioned in this SO post, the MaxLen(10) example in the Annotated doc is not built in, but an arbitrary function.

It would be great if there were defaults in typing_extensions, but leaving this up to the community seems to have been intentional. Defining the function like this should not be a problem:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    def FixedSize(value):
        pass  # Type hint annotation for containers of fixed length

I note that both Annotated[List[str], 2] and Annotated[List[str], FixedSize(2)] are an improvement over the current incorrect syntax List[str[2]].

@cielavenir
Copy link
Contributor Author

@rwgk
Copy link
Collaborator

rwgk commented May 23, 2023

@felixvd pybind11-stubgen update: https://github.com/cielavenir/pybind11-stubgen/commits/addFixedSize

Looks great to me! (purely from a readability pov; I know very little about typing)

I want to run this through global testing (Google-internal toolchain) after you had a chance to fix up the tests.

@rwgk
Copy link
Collaborator

rwgk commented May 23, 2023

Nice, all green! I'll run this through global testing tonight.

@rwgk
Copy link
Collaborator

rwgk commented May 24, 2023

There is one failing test in the global testing, exactly what I was afraid would happen. It's in the non-public part of our codebase, which is good news in the sense that I can change that code together with the import of this PR. Bad news for me because I'll probably have to spend at least a couple hours to negotiate with the owning team and see it through.

AFAIU there is a tool that harvests the pybind11 docstrings to create typestubs of some sorts. They have a command to update the typestubs automatically, the result is akin to a commit to be merged. Currently it looks like this:

-    def world_origin_world_to_camera(self) -> List[float[3]]: ...
+    def world_origin_world_to_camera(self) -> Any: ...

It's not a surprise to see a change, but apparently the tool doesn't know at all how to handle Annotated[List[float], FixedSize(3)] and simply falls back to Any.

As I hinted, I can work with the team owning the typestub generator, but in the bigger picture, the rest of the world might have similar problems.

Just pointing out, I think this is a good change and getting the world to adjust is an OK cost.

@cielavenir & @felixvd, could you please formally approve this PR if you agree? I'll merge this when I see both approvals.


Internal reference: OCL:534690556:BASE:534836523:1684939647732:8df15d67 (to help me find it again later)

@cielavenir
Copy link
Contributor Author

@rwgk pull request author cannot approve his own changeset, but I'm fine if I don't need to perform further changes on pybind11.

a tool that harvests the pybind11 docstrings to create typestubs of some sorts

interesting if other than pybind11-stubgen, though I will have to ask pybind11-stubgen staff to merge https://github.com/cielavenir/pybind11-stubgen/commits/addFixedSize anyway

Copy link

@felixvd felixvd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you said, this is a change that tools may need to adjust to. But since it follows PEP 593 and I believe is an improvement over the previous custom syntax which predates the PEP, I agree that the cost is worth it.

Thank you for the quick review and testing. Much appreciated.

It might be a good idea to loop in the team owning the typestub generation tool so they can comment before merging.

@rwgk
Copy link
Collaborator

rwgk commented May 25, 2023

It might be a good idea to loop in the team owning the typestub generation tool so they can comment before merging.

I reached out already; waiting to hear back. We're not deploying directly from master, I'll go ahead and merge here. Thanks all!

@rwgk rwgk merged commit d0232b1 into pybind:master May 25, 2023
79 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label May 25, 2023
@rwgk
Copy link
Collaborator

rwgk commented May 25, 2023

@cielavenir & @felixvd Do you have experience working on mypy stubgen, or know someone who could make the required changes there?

It might be a good idea to loop in the team owning the typestub generation tool so they can comment before merging.

I reached out already; waiting to hear back.

We converged on this idea (full context but the only point that matters here is 4.):

  1. disable the failing typestub test
  2. manually update the checked-in .pyi files
  3. import the latest pybind11 (into the Google codebase)
  4. update mypy stubgen on github & import (into Google codebase)
  5. re-enable the typestub test

@cielavenir
Copy link
Contributor Author

if other than pybind11-stubgen

@rwgk So you mean https://github.com/python/mypy/blob/master/mypy/stubgenc.py one right? I was not aware of that module (I was aware of mypy itself though).

@rwgk
Copy link
Collaborator

rwgk commented May 26, 2023

if other than pybind11-stubgen

@rwgk So you mean https://github.com/python/mypy/blob/master/mypy/stubgenc.py one right? I was not aware of that module (I was aware of mypy itself though).

I think the answer is yes. What I know for sure is that we're using

https://github.com/python/mypy/blob/master/mypy/stubgen.py

This version exactly, which calls stubgenc:

https://github.com/python/mypy/blob/b5914b51304800e82d435310e548547d2b55298c/mypy/stubgen.py#L110

@cielavenir
Copy link
Contributor Author

@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Jul 15, 2023
@loriab loriab mentioned this pull request Aug 3, 2023
9 tasks
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.

[BUG]: Invalid typing for std::array?
3 participants