Commit 4d6b6d1
committed
Ignore plugin-generated members when inferring PEP 695 variance
infer_variance walked every member of a class, including methods and
attributes synthesized by plugins. Those synthesized members reuse the
class's own type in positions that don't reflect how the user uses the
type variable, which corrupted the inferred variance:
- attrs generates ordering methods (__lt__/__le__/__gt__/__ge__) whose
``other`` parameter is typed as the class's own Self[T], plus an
__attrs_attrs__ tuple of the invariant Attribute[T]. This made
@attrs.define/@attrs.frozen generic classes invariant (and empty ones
contravariant) even when T was used only covariantly.
- On Python 3.13+ the dataclass plugin generates __replace__, whose
keyword parameters reuse the field types and made otherwise covariant
frozen dataclasses invariant.
User-written declarations are never flagged plugin_generated, so
skipping plugin-generated members during variance inference leaves real
fields and methods in control while ignoring synthesized ones. This
generalizes the existing __mypy-replace special case.
Tests:
- testPEP695InferVarianceWithAttrsFrozen (check-python312.test)
- testPEP695InferVarianceInFrozenDataclass (check-python313.test)
Relates to #17623.
Assistant-Model: Claude Code1 parent 94838b0 commit 4d6b6d1
3 files changed
Lines changed: 64 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2274 | 2274 | | |
2275 | 2275 | | |
2276 | 2276 | | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
2277 | 2289 | | |
2278 | 2290 | | |
2279 | 2291 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
251 | 287 | | |
252 | 288 | | |
253 | 289 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
480 | 480 | | |
481 | 481 | | |
482 | 482 | | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
0 commit comments