Fix crash when singledispatch is shadowed by a user function - #21848
Open
TheMuffinMan1320 wants to merge 1 commit into
Open
Fix crash when singledispatch is shadowed by a user function#21848TheMuffinMan1320 wants to merge 1 commit into
TheMuffinMan1320 wants to merge 1 commit into
Conversation
…20339) The functools.singledispatch plugin hook is matched purely by fully qualified name. If a user's own module happens to produce a function whose fullname is exactly "functools.singledispatch" (e.g. a top-level module literally named functools.py defining its own singledispatch function), the hook fires even though the return type is not an Instance of functools._SingleDispatchCallable, tripping an assert and crashing mypy. Bail out gracefully instead.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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 #20339
Root cause
The plugin hook for
functools.singledispatch(create_singledispatch_function_callbackinmypy/plugins/singledispatch.py) is registered inmypy/plugins/default.pypurely by matching the callee's fully qualified name against the string"functools.singledispatch".If a user's own code produces a function whose fullname happens to be exactly
"functools.singledispatch"— e.g. a top-level module literally namedfunctools.pythat defines its owndef singledispatch(func): ...— the plugin hook still fires for calls to that unrelated function, since the match is name-based rather than tied to the real typeshed definition.In that case
ctx.default_return_typeis whatever mypy inferred for the user's own function (not anInstanceoffunctools._SingleDispatchCallable, which is what the real typeshed stub forsingledispatchreturns), so:fails its assertion and crashes mypy with an
AssertionError/INTERNAL ERROR.Fix
Following the reproduction and suggestion from @hauntsaninja in the issue thread, when
ctx.default_return_typedoesn't have the shape the plugin expects, back off and returnctx.default_return_typeunchanged instead of asserting — the same pattern already used by the two earlier guard clauses in this function. This lets normal type checking continue rather than crashing.Testing
Added a regression test in
test-data/unit/check-singledispatch.test(testDontCrashWhenSingledispatchIsShadowedByUserFunction) that defines a module namedfunctools.pywith its ownsingledispatchfunction, matching the issue's exact repro. Verified this test:AssertionError)Ran the targeted suite:
pytest mypy/test/testcheck.py -k singledispatch→ 16 passed, 2 xfailed (pre-existing, unrelated).Also manually verified the original repro script from the issue no longer crashes and reports
Success: no issues found in 1 source file.