Repository navigation
Commit 25323bf
authored
fix: Accept a path object as an override file path (#572)
<!-- TERMAG_PR_BODY_BEGIN -->
**Requirements**
- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] I have validated my changes against all supported platform
versions
- [x] Sync/async parity: matching changes made to `async_*` siblings (or
N/A). N/A: `LDClient` and `AsyncLDClient` use the same
`FileOverrideSourceBuilder`.
**Related issues**
- Bugbot finding on #529:
#529 (comment)
**Describe the solution you've provided**
`FileOverrideSourceBuilder` accepted one path as a `str`, or a list of
paths. A single `pathlib.Path`, or any other `os.PathLike`, was passed
to `list()`. A path object is not iterable, so the constructor raised
`TypeError: 'PosixPath' object is not iterable`. The Bugbot finding
describes a different failure, a path split into its components that
fails every reload, but that cannot happen. The constructor raises
before a client exists.
The constructor now treats a `str` or an `os.PathLike` as a single path,
and converts every path with `os.fspath`. A path that does not name a
string path raises `TypeError` from the constructor, with a message that
names the value. That includes `bytes` (given alone, in a list, or named
by a path object), an `int`, and `None`. `bytes` and `bytearray` count
as single values, so a bare `bytes` path is named whole instead of being
read as a sequence of integers. The type hint is `Sequence` rather than
`List`, so that type checkers accept a list of path objects but not a
set, whose order is not fixed. The docstring says that paths may be path
objects.
Tests cover:
- a single path object;
- a list of path objects, which already worked;
- a path object whose string form differs from its path, as with
`os.DirEntry`;
- a source that loads a file given as a path object;
- the rejection of each of the values above.
Each of six broken variants of the constructor fails at least one of
these tests, including the original code and a variant that converts
with `str()` instead of `os.fspath`.
**Describe alternatives you've considered**
Leaving the builder to accept only strings, as its type hint said.
`pathlib` is the usual way to build paths, a list of path objects
already worked, and the error did not say what was wrong.
**Additional context**
This PR is stacked on #530, the top of the overrides stack, so that the
approved PRs below it do not change. Retarget it as the PRs below it
merge.
The review of this change found three existing gaps that this PR leaves
alone:
- An empty path resolves to the working directory, and its reads keep
failing.
- In polling mode, a path that contains a NUL character makes the source
fail to start, so the client runs without overrides. In watching mode,
the reload fails and keeps retrying.
- The file data sources treat a tuple of paths differently from a list.
Testing: `mypy ldclient`, `isort --check` and `pycodestyle` pass
locally. The test suite passes (2,111 tests). It was run without the
Redis, DynamoDB and Consul store integration tests, which need servers
that were not running. CI runs only for PRs based on `main` or
`feat/**`, so it will not run on this PR until it is retargeted.
<!-- TERMAG_PR_BODY_END -->
<!-- termag: box=i-0575d1c54853058d7 project=Overrides -->
<sub>Opened from termag</sub>
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Overview**
> Fixes a constructor bug where a **single** `pathlib.Path` (or other
`os.PathLike`) was passed to `list()`, causing `TypeError: 'PosixPath'
object is not iterable`.
>
> `FileOverrideSourceBuilder` now wraps a lone `str`,
`bytes`/`bytearray`, or `PathLike` as one path, normalizes every entry
with `os.fspath()` (so objects like `os.DirEntry` use their filesystem
path, not `str()`), and stores string paths. Invalid inputs (`bytes`
paths, ints, `None`, path objects that do not resolve to a `str`) fail
fast with an explicit `TypeError`. Type hints use `Sequence` plus
`PathLike` so callers can pass path objects while unordered collections
like `set` are discouraged.
>
> New tests cover single/list path objects, `__fspath__` behavior,
invalid types, and loading overrides from a path object.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
154b5b8. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 4b5a4b5 commit 25323bf
2 files changed
Lines changed: 88 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
110 | | - | |
111 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
112 | 115 | | |
113 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
114 | 129 | | |
115 | 130 | | |
116 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
125 | 185 | | |
126 | 186 | | |
127 | 187 | | |
| |||
256 | 316 | | |
257 | 317 | | |
258 | 318 | | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
259 | 326 | | |
260 | 327 | | |
261 | 328 | | |
| |||
0 commit comments