Skip to content

use is_same_type when resolving decorators in suggestions #19319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from mypy.plugin import FunctionContext, MethodContext, Plugin
from mypy.server.update import FineGrainedBuildManager
from mypy.state import state
from mypy.subtypes import is_same_type
from mypy.traverser import TraverserVisitor
from mypy.typeops import bind_self, make_simplified_union
from mypy.types import (
Expand Down Expand Up @@ -671,7 +672,7 @@ def extract_from_decorator(self, node: Decorator) -> FuncDef | None:
if not (
len(ct.arg_types) == 1
and _arg_accepts_function(get_proper_type(ct.arg_types[0]))
and ct.arg_types[0] == ct.ret_type
and is_same_type(ct.arg_types[0], ct.ret_type)
):
return None

Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/fine-grained-suggest.test
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,33 @@ f('hi')
(str) -> str
==


[case testSuggestInferFuncDecorator7]
# suggest: foo.f
[file foo.py]
from __future__ import annotations

from typing import Callable, Protocol, TypeVar
from typing_extensions import ParamSpec, TypeAlias

P = ParamSpec('P')
R = TypeVar('R')
Alias: TypeAlias = Callable[P, R]

def dec(f: Callable[P, R]) -> Alias[P, R]:
return f

@dec
def f(x):
return x

f('hi')

[builtins fixtures/isinstancelist.pyi]
[out]
(str) -> str
==

[case testSuggestFlexAny1]
# suggest: --flex-any=0.4 m.foo
# suggest: --flex-any=0.7 m.foo
Expand Down