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

Protecting our API with beartype #227

Merged
merged 3 commits into from
Dec 17, 2024
Merged

Protecting our API with beartype #227

merged 3 commits into from
Dec 17, 2024

Conversation

rgaudin
Copy link
Member

@rgaudin rgaudin commented Dec 17, 2024

This targets #221 and build ont it. Here's the recap of the commits:

Using beartype on all of scraperlib

This enables beartype on all of scraperlib, raising exceptions on all calls to our API
that violates the requested types (and function returning incorrect types)

Changes:

  • removed tests purposedly testing incorrect input types
  • fixed some return types or call params to match intent
  • logger console arg expecting io.StringIO
  • turned a couple NamedTuple into dataclass to ease with type declarations
  • Removed some unreachable code that was expecting invalid types
  • Introducing new protocols for IO-based type inputs, based on typeshed's (as protocols, those are ignored in test/coverage)
  • Image-related IO are declared as io.BytesIO instead of IO[bytes]. Somewhat equivalent but typing.IO is strongly discouraged everywhere. Cannot harmonize with rest of our code base as we pass this to Pillow.
  • Same goes for logger which eventually accepts TextIO
  • stream_file behavior changed a bit. Code assumed that if fpath is not there we want byte_stream. Given it's not properly tested, I changed it to accept both fpath and byte_stream simultaneously. I believe we should change the API to have a single input that supports the byte stream and the path and adapt behavior. We should do that through the code base though so that would be separate. I'll open a ticket if we agree on going this way

--

Disable cover on special blocks

Three blocks of code had to be marked no cover. Those blocks are entered and tested
but coverage reports them as missing. It might be due to the decorator…
I've tried to simplify the code leading to it but could fix the missing lines…

Once we merge this, I'll open a ticket to revisit this in the future.

--

Removed generic pyright: ignore statements

We should really ban wide pyright: ignore statements and use specifix expections
wherever necessary (or comply!)
This fixes it in the current codebase

@rgaudin rgaudin self-assigned this Dec 17, 2024
@rgaudin rgaudin changed the title Metadata and beartype Protecting our API with beartype Dec 17, 2024
Copy link

codecov bot commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (a0a225b) to head (6f93ffe).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #227   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           39        39           
  Lines         2447      2439    -8     
  Branches       335       331    -4     
=========================================
- Hits          2447      2439    -8     

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

@benoit74 benoit74 force-pushed the safe_metadata_revamp branch from a2c1456 to 7e2efa1 Compare December 17, 2024 15:23
Base automatically changed from safe_metadata_revamp to main December 17, 2024 15:30
This enables beartype on all of scraperlib, raising exceptions on all calls to our API
that violates the requested types (and function returning incorrect types)

Changes:

- removed tests purposedly testing incorrect input types
- fixed some return types or call params to match intent
- logger console arg expecting io.StringIO
- turned a couple NamedTuple into dataclass to ease with type declarations
- Removed some unreachable code that was expecting invalid types
- Introducing new protocols for IO-based type inputs, based on typeshed's (as protocols, those are ignored in test/coverage)
- Image-related IO are declared as io.BytesIO instead of `IO[bytes]`. Somewhat equivalent but typing.IO is strongly discouraged everywhere. Cannot harmonize with rest of our code base as we pass this to Pillow.
- Same goes for logger which eventually accepts TextIO
- stream_file behavior changed a bit. Code assumed that if fpath is not there we want byte_stream. Given it's not properly tested, I changed it to accept both fpath and byte_stream simultaneously. I believe we should change the API to have a single input that supports the byte stream and the path and adapt behavior. We should do that through the code base though so that would be separate. I'll open a ticket if we agree on going this way
Three blocks of code had to be marked no cover. Those blocks are entered and tested
but coverage reports them as missing. It might be due to the decorator…
I've tried to simplify the code leading to it but could fix the missing lines…

Once we merge this, I'll open a ticket to revisit this in the future.
We should really ban wide `pyright: ignore` statements and use specifix expections
wherever necessary (or comply!)
This fixes it in the current codebase
@benoit74 benoit74 force-pushed the metadata_and_beartype branch from 237e4a4 to 6f93ffe Compare December 17, 2024 15:35
@benoit74 benoit74 self-requested a review December 17, 2024 15:45
Copy link
Collaborator

@benoit74 benoit74 left a comment

Choose a reason for hiding this comment

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

LGTM, looks perfect, thanks a lot

@benoit74 benoit74 merged commit f68d568 into main Dec 17, 2024
9 checks passed
@benoit74 benoit74 deleted the metadata_and_beartype branch December 17, 2024 16:27
@benoit74 benoit74 added this to the 5.0.0 milestone Dec 17, 2024
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