Skip to content

Fix variance of Law{1,2,3}, Unwrappable, Contextable, and Trampoline - #2564

Merged
sobolevn merged 1 commit into
dry-python:masterfrom
jorenham:typing/variance-fixes
Oct 27, 2026
Merged

sobolevn merged 1 commit into
dry-python:masterfrom
jorenham:typing/variance-fixes

Conversation

@jorenham

Copy link
Copy Markdown
Contributor

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 to Unwrappable[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

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

#256 (from 2020), kinda.


🙏 Please, if you or your company finds dry-python valuable, 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.

@jorenham
jorenham force-pushed the typing/variance-fixes branch from a2e73e5 to 76a57ed Compare October 27, 2026 16:45
@codspeed

codspeed Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 note

ℹ️ 2 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

✅ 22 untouched benchmarks


Comparing jorenham:typing/variance-fixes (76a57ed) with master (f3a6f54)

Open in CodSpeed

@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (82ef3ef) to head (76a57ed).
⚠️ Report is 656 commits behind head on master.

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.
📢 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.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Very nice!

@sobolevn
sobolevn merged commit 8b9bf72 into dry-python:master Oct 27, 2026
27 checks passed
@jorenham
jorenham deleted the typing/variance-fixes branch October 27, 2026 18:09
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