Repository navigation
Fix variance of Law{1,2,3}, Unwrappable, Contextable, and Trampoline - #2564
Merged
Merged
Conversation
jorenham
force-pushed
the
typing/variance-fixes
branch
from
October 27, 2026 16:45
a2e73e5 to
76a57ed
Compare
Contributor
Merging this PR will not alter performance1 note
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2564 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 80 82 +2
Lines 2485 2700 +215
Branches 437 45 -392
==========================================
+ Hits 2485 2700 +215 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
2 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have made things!
Several class-scoped generic type parameters were (implicitly) declared invariant, even though these were only used in either input positions (viz. contravariant) or output positions (viz. covariant). As can be seen in the typesafety tests, this could lead to unexpected type-checker complaints about e.g.
Unwrappable[bool, ValueError]not being assignable toUnwrappable[int, Exception], even though that's perfectly type-safe.This all matches the "variance" (in type theory this is called "polarity", but in Python we like naming things ourselves I guess) inference algorithm from the typing spec (https://typing.python.org/en/latest/spec/generics.html#variance-inference) that's used for PEP 695 (py312+) type parameter syntax.
Checklist
CHANGELOG.mdRelated issues
#256 (from 2020), kinda.
🙏 Please, if you or your company finds
dry-pythonvaluable, help us sustain the project by sponsoring it transparently on https://github.com/sponsors/dry-python. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.