Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Python: a local built through a module attribute (x = pkg.Cls()) is typed by its variable name

未关闭
#2,957 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, python, rust
领域
tooling

调研方向

Start with the Python call-resolution code that types a local from its initializer; reproduce with the pkg/_client.py and use.py fixture in the issue, where client.fetch() wrongly resolves to Client.fetch. Check the instantiates handling for pkg.AsyncClient() versus from pkg import AsyncClient. Done when all four functions link to AsyncClient.fetch at line 7 and the with form is typed too.

由索引模型根据 Issue 内容生成。

描述

A Python local assigned from a constructor called through a module attribute (client = pkg.AsyncClient()) gets no type. Its method calls then fall to the receiver-name guess, which links client.fetch() to the class named like the variable, Client, even though the code just built an AsyncClient.

# pkg/_client.py
class Client:
    def fetch(self, url):          # line 2
        return url


class AsyncClient:
    async def fetch(self, url):    # line 7
        return url

# pkg/__init__.py
from ._client import Client, AsyncClient

# use.py
import pkg
from pkg import AsyncClient


async def attr_named_client():
    client = pkg.AsyncClient()
    await client.fetch("u")


async def attr_named_c():
    c = pkg.AsyncClient()
    await c.fetch("u")


async def from_named_client():
    client = AsyncClient()
    await client.fetch("u")


async def with_named_client():
    async with pkg.AsyncClient() as client:
        await client.fetch("u")
function edge on main expected
attr_named_client (client = pkg.AsyncClient()) Client.fetch (line 2, instance-method) ✗ AsyncClient.fetch (line 7)
with_named_client (async with pkg.AsyncClient() as client) Client.fetch ✗ AsyncClient.fetch
attr_named_c (c = pkg.AsyncClient()) none AsyncClient.fetch
from_named_client (from pkg import AsyncClient) AsyncClient.fetch ✓ same

The instantiates edge for pkg.AsyncClient() resolves correctly in every case. Only the local's type is lost, and the name guess fills the gap with the wrong class.

Reproduced on main at 25776014, with CODEGRAPH_KERNEL=0 and with the native kernel. Linux/WSL2, Node 22.23.2. (3f8e8b0f and 69cddc01 since then touch only Rust and the changelog.)

Where it shows up. httpx (b5addb6) uses exactly this: async with httpx.AsyncClient(...) as client: await client.get(url). Checking every Python calls edge against jedi's goto-definition, about 70 httpx edges disagree this way. Their client.get / post / send / stream / put / options / … calls link to the sync Client methods (httpx/_client.py lines 771–1234) instead of AsyncClient's (lines 1485–1949), for example every request in tests/client/test_async_client.py. Calling a project's own classes through the module (import mymod; x = mymod.Thing()) is common Python, so this isn't specific to httpx.

Expected. Type the local from module.Class(...) the same way as from a directly imported Class(...), and type with …() as x / async with …() as x from the context manager's class. (In another fixture, async with AsyncClient() as client with a directly imported AsyncClient also linked client.get() to Client.get, so the with form may need its own fix.) Failing that, don't let the receiver-name guess answer when the initializer is a call on a project class.

Related: #2946 (the same variable-name guess in TypeScript), #2821, #2175.

主要语言
C
星标
73.2k
派生
4.7k
平均合并
3 小时 13 分钟
30 天内合并 PR
579

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

colbymchenry/codegraph 的其他 Issue

查看 colbymchenry/codegraph 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。