Commit 1406f44
Fix: Convert scopes set to list in Credentials.__init__ (#1898)
This PR addresses an issue where passing a `set` of scopes to
`Credentials` causes `to_json` to fail and introduces inconsistent
mutability behavior if handled via a property accessor.
Changes:
- Modified `google.oauth2.credentials.Credentials.__init__` to convert
`scopes` to a `list` if it is a `set`.
- Updated `google.oauth2.credentials.Credentials.scopes` property
docstring to `Optional[Sequence[str]]`.
- Added a regression test `test_init_with_set_scopes` in
`tests/oauth2/test_credentials.py` to verify the fix, ensuring type
conversion, object stability, and serialization support.
- Updated `tests/test__oauth2client.py` to relax strict equality checks
on `scopes` (comparing as `sets`) to accommodate the new behavior where
internal storage is enforced as a `list`.
This PR supercedes PR #1145 which potentially introduced a mutability
inconsistency and failed to address a type correctness issue. This PR
avoids both of those concerns.
1. Mutability Inconsistency: If `._scopes` is a `set`, the `scopes`
property returns a new `list` every time it is accessed.
* If the user modifies this list (e.g.
`creds.scopes.append("new_scope")`), the modification is lost because
the next access returns a fresh list **from the underlying set**.
* If `_scopes` was originally a list (passed in `__init__`),
`creds.scopes` returns the same list reference, so modifications are
preserved. This inconsistent behavior between "set-initialized" and
"list-initialized" credentials is a potential bug trap.
2. Type correctness: The scopes argument in `__init__` is documented as
`Sequence[str]`. A set is not a `Sequence` (it's not indexable/ordered).
While Python is lenient, it is better to normalize the input at the
boundary (`__init__`) rather than in the accessor.
Moving the conversion logic to `__init__` ensures:
* _scopes is always a list (or `None`).
* Access to .scopes always returns the stored list (consistent
mutability).
* `to_json` works correctly because it accesses `self.scopes` (or
self._scopes).
---
*PR created automatically by Jules for task
[15605314498929918698](https://jules.google.com/task/15605314498929918698)
started by @chalmerlowe*
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Chalmer Lowe <[email protected]>1 parent cccad82 commit 1406f44
File tree
5 files changed
+34
-6
lines changed- google
- auth/transport
- oauth2
- tests_async/transport
- tests
- oauth2
5 files changed
+34
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
| |||
207 | 210 | | |
208 | 211 | | |
209 | 212 | | |
210 | | - | |
| 213 | + | |
211 | 214 | | |
212 | 215 | | |
213 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
58 | 83 | | |
59 | 84 | | |
60 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
0 commit comments