-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Python typestubs #496
Comments
Thanks for asking! RE2 doesn't have a stub file, but it really should. Unfortunately, I have no experience with Python type checkers apart from https://github.com/google/pytype, so I will need some time to read about (and tinker with) https://github.com/python/mypy before it would be safe to say that I know what I'm doing. ( |
I have a stub file written, which you can use as a starting point. My plan this week was to package it up to be pip installable. This might be a useful basis for you too? I can share the repo when it is ready (and I'm happy to sign any CLA required for you to take the code). |
Many thanks for offering! I did spend some time looking into this on Saturday and, as I had feared, the situation with Bazel with regard to Python type checkers is... truly dire. Distributing the stub file as part of |
For others, here's the stub package I will be maintaining: https://github.com/ddn0/google-re2-stubs |
Thanks, @ddn0! @cosmicexplorer, I'm thinking to give up trying to work entirely within Bazel and, instead, to factor out |
This is setting up to run mypy as per #496. Change-Id: I48e67387313ac01d493086be8481b34f26bafe82
This is setting up to run mypy as per #496. Change-Id: I48e67387313ac01d493086be8481b34f26bafe82 Reviewed-on: https://code-review.googlesource.com/c/re2/+/63230 Reviewed-by: Alex Chernyakhovsky <[email protected]> Reviewed-by: Paul Wankadia <[email protected]>
Have been working a pip-only solution for now, and realized after much trial and error that:
; cd python
; python setup.py bdist_wheel
; ls build/lib.linux-x86_64-cpython-310/re2/_re2.cpython-310-x86_64-linux-gnu.so
-rwxr-xr-x 1 cosmicexplorer wheel 5.5M Jun 8 16:37 build/lib.linux-x86_64-cpython-310/re2/_re2.cpython-310-x86_64-linux-gnu.so*
(bbb) # cosmicexplorer@terrestrial-gamma-ray-flash: ~/tools/re2/python 16:43:51
; python
Python 3.10.13 (main, Jan 10 2024, 22:22:23) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.machinery import ExtensionFileLoader as EFL
>>> EFL('_re2', 'build/lib.linux-x86_64-cpython-310/re2/_re2.cpython-310-x86_64-linux-gnu.so')
<_frozen_importlib_external.ExtensionFileLoader object at 0x70ae72e98250>
>>> zzz = EFL('_re2', 'build/lib.linux-x86_64-cpython-310/re2/_re2.cpython-310-x86_64-linux-gnu.so')
>>> zzz
<_frozen_importlib_external.ExtensionFileLoader object at 0x70ae72e98be0>
>>> dir(zzz)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'create_module', 'exec_module', 'get_code', 'get_data', 'get_filename', 'get_resource_reader', 'get_source', 'is_package', 'load_module', 'name', 'path']
>>> zzz.load_module()
<module '_re2' from 'build/lib.linux-x86_64-cpython-310/re2/_re2.cpython-310-x86_64-linux-gnu.so'>
>>> _re2 = zzz.load_module()
>>> dir(_re2)
['BytesToCharLen', 'CharLenToBytes', 'Error', 'Filter', 'RE2', 'Set', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] However, trying to use (bbb) # cosmicexplorer@terrestrial-gamma-ray-flash: ~/tools/re2/python 16:58:54
; ls dist
total 1.7M
-rw-r--r-- 1 cosmicexplorer wheel 1.7M Jun 8 16:37 google_re2-1.1.20240601-cp310-cp310-linux_x86_64.whl
(bbb) # cosmicexplorer@terrestrial-gamma-ray-flash: ~/tools/re2/python 16:58:55
; python
Python 3.10.13 (main, Jan 10 2024, 22:22:23) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dir(_re2)
KeyboardInterrupt
>>> from importlib.machinery import ExtensionFileLoader as EFL
>>> zzz = EFL('_re2', 'dist/google_re2-1.1.20240601-cp310-cp310-linux_x86_64.whl/re2/_re2.cpython-310-x86_64-linux-gnu.so')
>>> zzz.load_module()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap_external>", line 548, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 1063, in load_module
File "<frozen importlib._bootstrap_external>", line 888, in load_module
File "<frozen importlib._bootstrap>", line 290, in _load_module_shim
File "<frozen importlib._bootstrap>", line 719, in _load
File "<frozen importlib._bootstrap>", line 674, in _load_unlocked
File "<frozen importlib._bootstrap>", line 571, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 1176, in create_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ImportError: dist/google_re2-1.1.20240601-cp310-cp310-linux_x86_64.whl/re2/_re2.cpython-310-x86_64-linux-gnu.so: cannot open shared object file: Not a directory
>>> I also tried to get this working by executing the module straight from its bytes, but it appears loading a native extension requires the special handling of the However, the branch I linked (https://github.com/google/re2/compare/main...cosmicexplorer:internal-type-stubs?expand=1) does at least demonstrate that we can keep using
There were a whole host of difficulties I think we really shouldn't have had to deal with here, but if we can figure out a way to unzip the native |
We need `re2` to be a package, not a module, because it appears that modules can't have `.pyi` files, so munge the module into a package. Change-Id: I1a268875743390c32c0fb9cd58f6d83a670ce928
I spent a lot of tonight glaring at the |
We need `re2` to be a package, not a module, because it appears that modules can't have `.pyi` files, so munge the module into a package. Change-Id: I1a268875743390c32c0fb9cd58f6d83a670ce928
Change has been successfully cherry-picked as 10f876d Patch-set: 3 Subject: More setting up to run mypy as per #496. Status: merged Commit: 10f876d Tag: autogenerated:gerrit:merged Groups: aadebe5 Label: Code-Review=+2, ad8c2965909d1475610e379f93c9659b7d559a7c Label: Code-Review=+1, 92dba7285c5342283bec690b6e1168d01e7be543 Gerrit User 44612 <44612@c958e1eb-c711-3e17-a1d0-c94d35b2e5aa> Label: SUBM=+1, 32d94f2fe9b9d15bd3b8ce1e35892fa4da113e25 Submission-id: 63270 Submitted-with: OK Submitted-with: Rule-Name: gerrit~DefaultSubmitRule Submitted-with: MAY: Code-Review: Gerrit User 44612 <44612@c958e1eb-c711-3e17-a1d0-c94d35b2e5aa>
We need `re2` to be a package, not a module, because it appears that modules can't have `.pyi` files, so munge the module into a package. Change-Id: I1a268875743390c32c0fb9cd58f6d83a670ce928 Reviewed-on: https://code-review.googlesource.com/c/re2/+/63270 Reviewed-by: Paul Wankadia <[email protected]> Reviewed-by: Alex Chernyakhovsky <[email protected]>
I have some type stubs available at https://github.com/google/re2/compare/main...cosmicexplorer:type-stubs?expand=1 along with a change to EDIT: on gerrit as https://code-review.googlesource.com/c/re2/+/63310! |
All of the CI workflows are "post-submit" only: they trigger when Copybara mirrors https://code.googlesource.com/re2 to GitHub. No need to concern yourself with anything more than local testing. :)
If the Bazel community ever decides to make Python type checkers Just Work™, we can revisit this then. In the meantime, I have no desire to (co-)author and subsequently maintain whatever Starlark would be needed, so please don't spend any (more!) time worrying about it. ;) |
Change-Id: Ib1250ec954c0fecdaed606c41bbbbe00ff7bc1de Reviewed-on: https://code-review.googlesource.com/c/re2/+/63371 Reviewed-by: Paul Wankadia <[email protected]> Reviewed-by: Alex Chernyakhovsky <[email protected]>
Hello! Wondering if this issue is still being worked on. I'm getting PRs on my stub package ddn0/google-re2-stubs#4 and I'm wondering if I should signpost that my repo should be sunsetted or if I should continue my maintenance. |
also we should consider export for example: pattern: re2.Regexp[str] = re2.compile(...)
def __repl(m: re2.Match[str]) -> str: ...
pattern.sub(__repl, ...) |
I'm wondering if this project already maintains Python typestubs (pyi) for the google-re2 package.
If not, I've written typestubs for my own personal use, would this project be interested in accepting them? Otherwise, I can maintain a separate typestub library.
The text was updated successfully, but these errors were encountered: