diff --git a/mypy/plugins/singledispatch.py b/mypy/plugins/singledispatch.py index 9a5576c17e82c..7fb01985f8e4a 100644 --- a/mypy/plugins/singledispatch.py +++ b/mypy/plugins/singledispatch.py @@ -106,7 +106,8 @@ def create_singledispatch_function_callback(ctx: FunctionContext) -> Type: # singledispatch returns an instance of functools._SingleDispatchCallable according to # typeshed singledispatch_obj = get_proper_type(ctx.default_return_type) - assert isinstance(singledispatch_obj, Instance) + if not isinstance(singledispatch_obj, Instance): + return ctx.default_return_type singledispatch_obj.args += (func_type,) return ctx.default_return_type diff --git a/test-data/unit/check-singledispatch.test b/test-data/unit/check-singledispatch.test index 36c99b288bbe8..eb172acba3673 100644 --- a/test-data/unit/check-singledispatch.test +++ b/test-data/unit/check-singledispatch.test @@ -307,3 +307,15 @@ def default(val) -> int: return 3 [builtins fixtures/args.pyi] + +[case testDontCrashWhenSingledispatchIsShadowedByUserFunction] +# cmd: mypy -m functools +[file functools.py] +def singledispatch(func): + pass + +@singledispatch +def f(fn): + pass +[out] +[builtins fixtures/args.pyi]