Skip to content

Commit

Permalink
Fix class method and static method calls with --export-ref-info (pyth…
Browse files Browse the repository at this point in the history
…on#15282)

Previously they were sometimes reported as `builtins.type.method_name`
(i.e. a method of `builtins.type` instead of e.g. `module.ClsName`). Now
report them as `module.ClsName.method_name`.
  • Loading branch information
JukkaL authored May 22, 2023
1 parent 6c7e480 commit caf4787
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions mypy/refinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

from __future__ import annotations

from mypy.nodes import LDEF, Expression, MemberExpr, MypyFile, NameExpr, RefExpr
from mypy.nodes import (
LDEF,
Expression,
MemberExpr,
MypyFile,
NameExpr,
RefExpr,
SymbolNode,
TypeInfo,
)
from mypy.traverser import TraverserVisitor
from mypy.typeops import tuple_fallback
from mypy.types import (
Expand Down Expand Up @@ -36,8 +45,11 @@ def record_ref_expr(self, expr: RefExpr) -> None:
fullname = expr.fullname
elif isinstance(expr, MemberExpr):
typ = self.type_map.get(expr.expr)
sym = None
if isinstance(expr.expr, RefExpr):
sym = expr.expr.node
if typ:
tfn = type_fullname(typ)
tfn = type_fullname(typ, sym)
if tfn:
fullname = f"{tfn}.{expr.name}"
if not fullname:
Expand All @@ -46,13 +58,15 @@ def record_ref_expr(self, expr: RefExpr) -> None:
self.data.append({"line": expr.line, "column": expr.column, "target": fullname})


def type_fullname(typ: Type) -> str | None:
def type_fullname(typ: Type, node: SymbolNode | None = None) -> str | None:
typ = get_proper_type(typ)
if isinstance(typ, Instance):
return typ.type.fullname
elif isinstance(typ, TypeType):
return type_fullname(typ.item)
elif isinstance(typ, FunctionLike) and typ.is_type_obj():
if isinstance(node, TypeInfo):
return node.fullname
return type_fullname(typ.fallback)
elif isinstance(typ, TupleType):
return type_fullname(tuple_fallback(typ))
Expand Down

0 comments on commit caf4787

Please sign in to comment.