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

Type Annotate the *heck* out of social-core #986

Merged
merged 10 commits into from
Jan 10, 2025

Conversation

offbyone
Copy link
Contributor

@offbyone offbyone commented Jan 10, 2025

Proposed changes

This is implementing the type checking I suggested in #949.

Types of changes

Please check the type of change your PR introduces:

  • Release (new release request)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (PEP8, lint, formatting, renaming, etc)
  • Refactoring (no functional changes, no api changes)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Build related changes (build process, tests runner, etc)
  • Other (please describe):

Checklist

Put an x in the boxes that apply. You can also fill these out after creating
the PR. If you're unsure about any of them, don't hesitate to ask. We're here to
help! This is simply a reminder of what we are going to look for before merging
your code.

Other information

This touches most of social-core, and entails some compromises due to overrides that pyright really does not like. These can be broken down into a few categories:

  • cases where a value can be None but is being treated as not, because it's set outside of the scope of the method. In these cases, I have added assert <var> statements to tell the type checker that the value is non-null; this code would have raised an exception at this point anyway
  • method overrides that are incompatible with the base class. get_user_id() and get_user_details() and the like are particularly egregious in this regard; I have added type:ignore comments in these cases.
  • Incorrect types in library values. These probably bugs. Look for TODO comments on these; I didn't try fix them here out of an abundance of caution, since I don't use those authentication providers.
  • the Model mixins all used get() methods, which actually led to type confusion, since sometimes more than one was mixed in. I renamed get() on Nonce to get_nonce() and on Association to get_association() in those cases.
  • The signature types are definitely wrong somehow; public_key = RSAAlgorithm.from_jwk(...) is not returning a public key, it's returning an allowed keys type, at least with modern pyca-cryptography; it is probably wrong, but it's working. type:ignore'd
  • Annotated several return types that were mis-inferred. Often pyright would infer a literal return type, because pyright is overly picky.

This enables typing in social_core and gives us the tools for testing
- ignore several errors that are endemic to mixin-type test strategies
- in several cases assert that tests are set up correctly to give the
  type checker some help
- for tidiness, mark some test-like class names as not-a-test
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 77.87611% with 25 lines in your changes missing coverage. Please review.

Project coverage is 77.93%. Comparing base (326d4b3) to head (6792dad).
Report is 38 commits behind head on master.

Files with missing lines Patch % Lines
social_core/backends/apple.py 50.00% 2 Missing and 1 partial ⚠️
social_core/backends/mediawiki.py 0.00% 3 Missing ⚠️
social_core/backends/uffd.py 0.00% 3 Missing ⚠️
social_core/backends/oauth.py 86.66% 1 Missing and 1 partial ⚠️
social_core/storage.py 75.00% 0 Missing and 2 partials ⚠️
social_core/tests/backends/test_saml.py 66.66% 2 Missing ⚠️
social_core/tests/test_exceptions.py 0.00% 1 Missing and 1 partial ⚠️
social_core/backends/auth0.py 0.00% 1 Missing ⚠️
social_core/backends/bitbucket.py 66.66% 0 Missing and 1 partial ⚠️
social_core/backends/facebook.py 0.00% 1 Missing ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #986      +/-   ##
==========================================
+ Coverage   77.88%   77.93%   +0.05%     
==========================================
  Files         347      347              
  Lines       10669    10698      +29     
  Branches      504      455      -49     
==========================================
+ Hits         8310     8338      +28     
  Misses       2200     2200              
- Partials      159      160       +1     
Flag Coverage Δ
unittests 77.93% <77.87%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@offbyone offbyone force-pushed the type-annotations branch 3 times, most recently from 5fc38b6 to dd09461 Compare January 10, 2025 07:04
social_core/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@nijel nijel left a comment

Choose a reason for hiding this comment

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

Can you please constantly use future typing with Type | None syntax instead of mixing it with Optional[Type] in some files?

@offbyone
Copy link
Contributor Author

No... I just accidentally left in a test assertion. Fixing...

@offbyone
Copy link
Contributor Author

Can you please constantly use future typing with Type | None syntax instead of mixing it with Optional[Type] in some files?

Happy to; that was accidental in the early steps, where I forgot that we had Python new enough to use it. I'll revise ASAP.

@nijel nijel merged commit 3cce6f3 into python-social-auth:master Jan 10, 2025
11 of 12 checks passed
@nijel
Copy link
Member

nijel commented Jan 10, 2025

Merged, thanks for your contribution!

@nijel nijel self-assigned this Jan 10, 2025
nijel added a commit to nijel/social-core that referenced this pull request Feb 13, 2025
This was a breaking change in API that social-app-django
and other storages rely on.
@nijel
Copy link
Member

nijel commented Feb 13, 2025

This introduced API changes that break storage modules, #1020 reverts that part.

nijel added a commit to nijel/social-core that referenced this pull request Feb 13, 2025
This was a breaking change in API that social-app-django
and other storages rely on.
nijel added a commit to nijel/social-core that referenced this pull request Feb 13, 2025
This was a breaking change in API that social-app-django
and other storages rely on.
nijel added a commit to nijel/social-core that referenced this pull request Feb 13, 2025
This was a breaking change in API that social-app-django
and other storages rely on.
nijel added a commit that referenced this pull request Feb 13, 2025
This was a breaking change in API that social-app-django
and other storages rely on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

lxml & xmlsec libxml2 library version mismatch Add type annotations to clarify some return types
2 participants