Skip to content

Comments

storage.UserProtocol: allow for read-only attributes#1582

Merged
nijel merged 3 commits intopython-social-auth:masterfrom
jorenham:storage.UserProtocol-fix
Feb 24, 2026
Merged

storage.UserProtocol: allow for read-only attributes#1582
nijel merged 3 commits intopython-social-auth:masterfrom
jorenham:storage.UserProtocol-fix

Conversation

@jorenham
Copy link
Contributor

This loosens the restrictions on the social_core.storage.UserProtocol attributes to allow for read-only attributes by changing them from mutable attributes to properties. This is a backwards compatible change. The properties might look a bit odd here, but because there is no way to better way express read-only attributes in Python, this is the conventional way to express this, and is understood by all major type-checkers.

I ran into this issue in my django project when trying to pass an instance of AbstractUser to create_social_auth.

error: Argument of type "AbstractUser" cannot be assigned to parameter "user" of type "UserProtocol" in function "create_social_auth"
    "AbstractUser" is incompatible with protocol "UserProtocol"
      "id" is not present
        "username" is invariant because it is mutable
        "username" is an incompatible type
          "CharField[Unknown, Unknown]" is not assignable to "str"
        "is_active" is invariant because it is mutable
        "is_active" is an incompatible type
          Type "BooleanField[bool | Combinable, bool]" is not assignable to type "bool | (() -> bool)"
    ... (reportArgumentType)

This (basedpyright) error appeared after upgrading social-auth-core from 4.8.3 to 4.8.5. I did not try other type-checkers, but I'm pretty certain they'd also report a similar error.

This loosens the restrictions on the `social_core.storage.UserProtocol` attributes to allow for read-only attributes by changing them from mutable attributes to properties. This is a backwards compatible change.
The properties might look a bit odd here, but because there is no way to better way express read-only attributes in Python, this is the conventional way to express this, and is understood by all major type-checkers.
@nijel
Copy link
Member

nijel commented Feb 23, 2026

The test model will need some adjustment for this because it now assigns to a read-only property:

self.id = User.next_id()

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.15%. Comparing base (17b5f12) to head (c8fd4b1).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1582      +/-   ##
==========================================
- Coverage   79.16%   79.15%   -0.01%     
==========================================
  Files         354      354              
  Lines       11661    11658       -3     
  Branches      536      536              
==========================================
- Hits         9231     9228       -3     
  Misses       2245     2245              
  Partials      185      185              
Flag Coverage Δ
unittests 79.15% <100.00%> (-0.01%) ⬇️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nijel nijel merged commit 4f4e78c into python-social-auth:master Feb 24, 2026
13 checks passed
@nijel
Copy link
Member

nijel commented Feb 24, 2026

Merged, thanks for your contribution!

@nijel nijel self-assigned this Feb 24, 2026
@jorenham jorenham deleted the storage.UserProtocol-fix branch February 24, 2026 10:25
@jorenham
Copy link
Contributor Author

Happy to help. And thank you for upping the static typing game here :)

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.

2 participants