Skip to content

Commit 3153ae6

Browse files
committed
Support suggesting types for class and static methods
1 parent 8194d79 commit 3153ae6

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

mypy/suggestions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ def get_suggestion(self, function: str) -> str:
318318
if self.no_errors and orig_errors:
319319
raise SuggestionFailure("Function does not typecheck.")
320320

321-
# FIXME: what about static and class methods?
322-
is_method = bool(node.info)
321+
is_method = bool(node.info) and not node.is_static
323322

324323
if len(node.arg_names) >= 10:
325324
raise SuggestionFailure("Too many arguments")
@@ -482,6 +481,9 @@ def builtin_type(self, s: str) -> Instance:
482481
def json_suggestion(self, function: str, suggestion: str) -> str:
483482
"""Produce a json blob for a suggestion suitable for application by pyannotate."""
484483
mod, func_name, node = self.find_node(function)
484+
# pyannotate irritatingly drops class names for class and static methods
485+
if node.is_class or node.is_static:
486+
func_name = func_name.split('.', 1)[-1]
485487

486488
# pyannotate works with either paths relative to where the
487489
# module is rooted or with absolute paths. We produce absolute

test-data/unit/fine-grained-suggest.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,31 @@ def bar() -> None:
456456
[out]
457457
(str) -> str
458458
==
459+
460+
[case testSuggestInferClassMethod]
461+
# flags: --strict-optional
462+
# suggest: foo.F.bar
463+
# suggest: foo.F.baz
464+
[file foo.py]
465+
class F:
466+
@classmethod
467+
def bar(cls, x, y):
468+
return x
469+
470+
@staticmethod
471+
def baz(x, y):
472+
return x
473+
474+
[file bar.py]
475+
from typing import Any
476+
any: Any
477+
478+
from foo import F
479+
def bar() -> None:
480+
F.bar(0, any)
481+
F.baz(0, any)
482+
[builtins fixtures/classmethod.pyi]
483+
[out]
484+
(int, Any) -> int
485+
(int, Any) -> int
486+
==

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy