Skip to content
New issue

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

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

Already on GitHub? # to your account

SymbolTableNode.kind is incorrect for from <module> import <name> imports in a function body #18616

Open
bzoracler opened this issue Feb 6, 2025 · 0 comments
Labels
bug mypy got something wrong

Comments

@bzoracler
Copy link
Contributor

Bug Report, Expected Behaviour, & Actual Behaviour

I'm writing a mypy plugin which needs to distinguish between the origin of variables (whether they're from the global scope or local scope). When analysing names from ImportFrom nodes, the kind value appears to be incorrect:

# module.py
import typing as t

TypeT = t.TypeVar("TypeT", bound=type[object])

def reveal_name_expr_kinds(Class: TypeT, /) -> TypeT:
    return Class

@reveal_name_expr_kinds
class A:
    def method(self, /) -> None:
        import os
        from os import chdir, path

        x = 1

        # Correct behaviour
        os  # N: Revealed kind is "Ldef"
        x  # N: Revealed kind is "Ldef"

        # Correct behaviour
        t  # N: Revealed kind is "Gdef"
        TypeT  # N: Revealed kind is "Gdef"

        # Incorrect behaviour (expected both of these to be "Ldef")
        chdir  # N: Revealed kind is "Gdef"
        path  # N: Revealed kind is "Gdef"

Potential fix

I believe the issue is here:

node = module.names.get(id)

Changing it to the following seems to fix the problem:

                 node = module.names.get(id)
+                if node is not None:
+                    node = SymbolTableNode(self.current_symbol_kind(), node.node)

However, It's not obvious how to write a test for this. I'm looking at https://github.com/python/mypy/blob/master/test-data/unit/semanal-symtable.test but the tests here rely on str()ingifying a mypy.nodes.SymbolTable object, which is an attribute of mypy.nodes.MypyFile and mypy.nodes.TypeInfo; there is no equivalent attribute in mypy.nodes.FuncDef (function body symbol tables only temporarily exist at

mypy/mypy/semanal.py

Lines 444 to 446 in e9a813c

self.saved_locals: dict[
FuncItem | GeneratorExpr | DictionaryComprehension, SymbolTable
] = {}
).

Is there is an obvious way to test the local definition kinds directly (that is, without employing a mypy plugin into the tests)?

To Reproduce

Along with module.py above, put the following files in any directory, navigate inside the directory, and just run mypy in the command line:

# mypy.ini
[mypy]
plugins = plugin.py
files = module.py
# plugin.py
from __future__ import annotations

import typing as t

import mypy.nodes
import mypy.plugin
import mypy.server.subexpr

if t.TYPE_CHECKING:
    import collections.abc as cx

STR_REVEAL_KINDS_DECO_NAME: t.Final = "reveal_name_expr_kinds"

def plugin(version: str) -> type[mypy.plugin.Plugin]:
    return SymbolKindRevealer

class SymbolKindRevealer(mypy.plugin.Plugin):
    def get_class_decorator_hook_2(
        self, fullname: str
    ) -> cx.Callable[[mypy.plugin.ClassDefContext], bool] | None:
        if fullname.endswith(f".{STR_REVEAL_KINDS_DECO_NAME}"):
            return _reveal_symbol_kinds

def _reveal_symbol_kinds(ctx: mypy.plugin.ClassDefContext, /) -> bool:
    expr_finder: t.Final = mypy.server.subexpr.SubexpressionFinder()
    for cls_stmt in ctx.cls.defs.body:
        if isinstance(cls_stmt, mypy.nodes.FuncDef):
            for func_stmt in cls_stmt.body.body:
                func_stmt.accept(expr_finder)
    for expr in expr_finder.expressions:
        if isinstance(expr, mypy.nodes.NameExpr) and (expr.kind is not None):
            ctx.api.msg.note(
                f'Revealed kind is "{mypy.nodes.node_kinds[expr.kind]}"', expr
            )
    return True

Your Environment

  • Mypy version used: 1.15
  • Python version used: 3.9
@bzoracler bzoracler added the bug mypy got something wrong label Feb 6, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant