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

Another typing attempt #199

Merged
merged 5 commits into from
Jun 18, 2024
Merged

Conversation

FlavioAmurrioCS
Copy link
Contributor

@FlavioAmurrioCS FlavioAmurrioCS commented Apr 28, 2024

Pull Request: Enhance Typing Support for Library

I've been a fan of this library for quite some time, but I've eagerly awaited proper typing support. After reviewing other pull requests, I noticed that some were abandoned or contained extensive changes. With that in mind, I've kept this pull request concise and focused.

Key Updates:

  1. Typing Information: I've primarily added type hints for the most commonly used methods, particularly within the sequence class.
  2. No Logic Changes: Rest assured, there are no alterations to the existing logic.
  3. Configuration Updates:
    • Added pre-commit-config.yaml with Black formatting, mypy type checking, and ruff. (If possible please enable https://pre-commit.ci/ for this repo)
    • Adjusted settings in pyproject.toml for mypy and ruff, enabling only a select few rules to avoid excessive modifications.
  4. Added remove_none: this will remote None values from stream as well as TypeGuard the stream.
  5. Simple Type Testing: In order to "test" the typing annotation I went ahead and called all the examples from the a functions docs and assigned it to a variable that has been typed with the expected type. Is there is a mismatch, mypy would flag this. This helped me debug typing bugs.

I hope this contribution aligns with the project's goals and makes it easier for everyone to work with! 🚀

@FlavioAmurrioCS FlavioAmurrioCS changed the title YATA: Yet another typing attempt Another typing attempt Apr 28, 2024
Copy link

codecov bot commented Apr 30, 2024

Codecov Report

Attention: Patch coverage is 99.61538% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 98.24%. Comparing base (6ed2e9a) to head (59cf0e4).

Files Patch % Lines
functional/streams.py 96.15% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #199      +/-   ##
==========================================
+ Coverage   98.12%   98.24%   +0.11%     
==========================================
  Files          12       13       +1     
  Lines        2350     2452     +102     
==========================================
+ Hits         2306     2409     +103     
+ Misses         44       43       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EntilZha
Copy link
Owner

Thanks for the persistence! I took a quick lookover PR and initially it looks good. I'll take a second look once the tests are passing. It would be great to get the code cov tests passing, but if there is a good reason they wouldn't pass due to lower coverage, then thats fine, wouldn't be a blocker.

@FlavioAmurrioCS
Copy link
Contributor Author

@EntilZha I went ahead configured pytest-cov to ignore TYPE_CHECKING blocks and ellipses. nedbat/coveragepy#831 (comment) so hope things should be good now.

encoding: Optional[str] = None,
errors: Optional[str] = None,
newline: Optional[str] = None,
encoding: str | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Python supports the pipe symbol now "I"
So, Optional[str] and str | None are pretty much the same as far as I know

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct

Copy link
Contributor

Choose a reason for hiding this comment

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

So there isn't any point in changing these parts, is there? Unless I am missing something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are identical for typing purposes as far as I know people gravitate to the pipe version due to it being shorter and more concise. Reason I change existing ones was because I ran ruff on this code with the following rule. https://docs.astral.sh/ruff/rules/non-pep604-annotation/

If allowed I am planning to introduce ruff to this project in the future since I have found it very helpful in my personal projects.

Copy link
Owner

Choose a reason for hiding this comment

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

In general, I prefer the str | None, but one argument in favor of the other style is that the pipe is only available in python 3.10 and above. I'm personally fine with dropping support for python 3.8/3.9 in favor of better syntax, but would be accepting of the old syntax if its a substantial barrier to current use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to drop support for 3.8/3.9. With from __future__ import annotations it allows us to use the new syntax. https://peps.python.org/pep-0563/

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, TIL something new, great!

@@ -234,7 +270,7 @@ def cache(self, delete_lineage=False):
self._lineage = Lineage(engine=self.engine)
return self

def head(self, no_wrap: Optional[bool] = None):
def head(self, no_wrap: bool | None = None) -> _T_co:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is what I was referring to in the comment I left above

@maestro-1
Copy link
Contributor

Hello @FlavioAmurrioCS
Made any progress on this?

@FlavioAmurrioCS
Copy link
Contributor Author

This PR is ready for review. I am just waiting for feedback or an approval at this point. I do have more changes for the remaining files but didn't want to add too much to this PR to prevent it from being too large. cc: @EntilZha @maestro-1

@EntilZha
Copy link
Owner

Sorry for the delay, was traveling! LGTM to merge and passes all the test now. Thanks so much for pushing this across the finish line!

@EntilZha EntilZha merged commit f001dc9 into EntilZha:master Jun 18, 2024
6 checks passed
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.

3 participants