Fix stubtest false positive for a typevar default across overloads - #21862
Open
arose26 wants to merge 1 commit into
Open
Fix stubtest false positive for a typevar default across overloads#21862arose26 wants to merge 1 commit into
arose26 wants to merge 1 commit into
Conversation
The default value check unwrapped a bare TypeVar to its upper bound, but merging overload items contributes one type variable per item, so the same TypeVar used in several items arrives as a union that nothing can satisfy.
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.
Fixes #21597
Adding a third overload that reuses
_Tmakes stubtest reject a runtime default it accepted with two:Cause
_T | _Tis not a failure to deduplicate.Signature.from_overloadedfuncdefalready merges argument types withmake_simplified_union, but each overload item has its own type variable ids, so the_Tfrom one item and the_Tfrom another are distinct variables and the union is kept.The problem is in
_verify_arg_default_value, which unwrapped only a bare type variable:A union of type variables never took that branch, and nothing is a subtype of it, so any runtime default failed as soon as a second overload item mentioned the same
TypeVar.Change
The unwrapping now applies inside a union too, so
_T | _Tis checked asobjectrather than as a union no value inhabits.Upper bounds are preserved rather than erased:
erase_typevars()would have replaced the variables withAnyand quietly stopped catching real mismatches, so the helper maps each variable to its own upper bound. A bounded typevar still rejects an incompatible default — there is a test for exactly that, and the message it produces readsstub parameter type strinstead ofstr | str.Tests
Two cases added to
test_overload:_Twith a runtime default: no error (fails before this change)TypeVar(bound=str)and a runtime default of1: still an errormypy/test/teststubtest.pypasses (67 tests), self-check onmypy/stubtest.pyis clean, andruff/blackare clean on both files.