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

fix typing on search/match/findall/sub/... #5

Merged
merged 6 commits into from
Oct 17, 2024
Merged

Conversation

trim21
Copy link
Contributor

@trim21 trim21 commented Oct 4, 2024

you can run mypy tests and python tests/api/__init__.py to verify typing are both valid at runtime and typing level.

this should suppress #4 , patch are included in this PR.

Looks like I misunderstood how re2 handle inputs and you misunderstood how AnyStr works here.

I find that methods on re2.compile(str(...)).match/findall/sub/... can handle bytes input, so it dones't need to be AnyStr. For python stdlib re, pattern and string must have same type but it's not true for re2, re2 can run bytes pattren on string or string pattern on bytes, which means re.findall("1", b"0 1 2") doesn't work but re2.findall("1", b"0 1 2") works.

And AnyStr is a TypeVar and in class _Regexp(Generic[AnyStr]) it will be narrowed base on the type of re2.compile(), but not narrowed at function/method level by the input string.

This make most methods in _Regexp have wrong typing, I'll send a seprated PR to fix them later

@trim21
Copy link
Contributor Author

trim21 commented Oct 4, 2024

A good thing on Python typing is that even original package doesn't have typing, you can still use typing stub.

If you do not want to maintain this package I can keep maintaining it. re2 looks pretty stable so I guess we won't need to change this package very much.

I know nothing about gerrit or bazel, it will take me lots of time to
learn them first to submit a PR to re2 main package, it would be much easier for me to just maintain a type stubs.

@ddn0
Copy link
Owner

ddn0 commented Oct 9, 2024

Sorry for the delay, I'll review this PR soon.

@ddn0 ddn0 force-pushed the fix-typing branch 3 times, most recently from c456cd1 to f3460d7 Compare October 14, 2024 21:46
@ddn0
Copy link
Owner

ddn0 commented Oct 14, 2024

Thanks!

I learned a lot more about constrained types.

I just had some style issues. And to speed up convergence, I just applied my suggestions to your PR.

Let me know if you have any objections by Tuesday end (anywhere on earth "timezone"); otherwise, I'll merge the PR as it is and prepare a new PyPI release with these changes.

@trim21
Copy link
Contributor Author

trim21 commented Oct 14, 2024

LGTM.

sadly we can't export Match and Regex

@ddn0 ddn0 merged commit 8047d12 into ddn0:main Oct 17, 2024
5 checks passed
@trim21 trim21 deleted the fix-typing branch October 17, 2024 17:20
@trim21
Copy link
Contributor Author

trim21 commented Oct 18, 2024

new pypi release?

@trim21 trim21 restored the fix-typing branch October 18, 2024 01:21
@ddn0
Copy link
Owner

ddn0 commented Oct 21, 2024

Done!

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