Parser: a field declared under `if`/`else` in the class body is lost
评估
调研方向
Start in cli/orm_lens/parser.py: _build_body_regexes() near line 150 only matches class-body indent, and the body loop in _collect_defs() (around lines 606-700) is where block openers like if/else at class level would need handling. Reproduce with the docs/models.py example via orm-lens describe docs.Doc and add the test in cli/tests/test_conditional_models.py. Done means title_upper appears once, def bodies add nothing, and test_golden_snapshots.py is unchanged; a linked open PR (#259) already covers this, so check it before starting.
由索引模型根据 Issue 内容生成。
描述
Source: label-studio — label_studio/projects/models.py:1361 (Project.search_vector)
What happens
Some models pick a field depending on the database:
class Project(models.Model):
...
if connection.vendor == "postgresql":
search_vector = GeneratedField(...)
else:
search_vector = models.TextField(null=True, blank=True)
Body regexes are built for exactly the class-body indent (_build_body_regexes(indent)), so lines one level deeper never match. Both branches are skipped and the field is gone. On label-studio that's projects.Project.search_vector, reported by drift as "migrated but no longer declared".
Note: even with models.TextField in both branches the field is lost, so it isn't only about GeneratedField (that one is #6 above).
Repro
docs/models.py
from django.db import connection, models
class Doc(models.Model):
title = models.CharField(max_length=200)
if connection.vendor == "postgresql":
title_upper = models.TextField(db_comment="pg")
else:
title_upper = models.TextField(null=True)
body = models.TextField()
class Meta:
ordering = ["title"]
$ orm-lens describe docs.Doc
| `title` | CharField | `max_length=200` |
| `body` | TextField | `` |
Expected: title, title_upper, body.
Where
cli/orm_lens/parser.py
- line 150,
_build_body_regexes(); - around lines 606–700, the body loop in
_collect_defs().
Suggested fix
When a body line at class indent is a block opener (if ...:, elif ...:, else:, try:, except ...:, finally:), match the lines in that block with _build_body_regexes(indent + <block indent>) too. Keep the first definition of a name and ignore the rest, so the two branches don't give two title_upper fields. Methods (def) must stay out of this; only plain block statements count.
This touches the core loop, hence help wanted. src/parser.ts has the same loop, and test/fixtures/parity_input.py is the shared contract. A Python-only fix is fine, but leave a note in the PR so the TS side can follow.
How to test
- A new test in
cli/tests/test_conditional_models.pywith the repro: three fields,title_upperonce. - A method with an
ifinside (def save(self): if x: y = models.Q()) adds no field. - The golden snapshots don't change (
test_golden_snapshots.py), or the PR explains why. cd cli && python -m pytest -q,ruffandmypypass.
Difficulty: help wanted, a few hours. Ask in the issue before you start if the approach is unclear.
- 主要语言
- Python
- 星标
- 84
- 派生
- 13
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
FROWNINGdev/django-orm-lens 的其他 Issue
-
suggest-indexes proposes composite indexes that include the primary key or a unique field可能已有人在做 @bfs2021 今天认领。 未关闭bug good first issue python
难度 2/5 1-3 小时 新手友好度 82/100
FROWNINGdev/django-orm-lens#269 ·
-
`migration-risk` text output prints `app.None` for graph-level findings可能已有人在做 @phoenixbycaselka 今天认领。 未关闭bug good first issue python
难度 1/5 1-3 小时 新手友好度 88/100
FROWNINGdev/django-orm-lens#268 ·
-
Finish the Chinese rule reference — DOL001–DOL007可能重新可做 @harbinresearcher 于 64 天前认领,目前没有进行中的 PR。 未关闭documentation good first issue help wanted
难度 2/5 1-3 小时 新手友好度 68/100
FROWNINGdev/django-orm-lens#81 · 2 条评论 ·
-
Fields inherited from Django's `AbstractBaseUser` / `PermissionsMixin` / `AbstractUser` are missing未关闭bug good first issue python
难度 3/5 1-2 天 新手友好度 62/100
FROWNINGdev/django-orm-lens#267 ·
-
bug good first issue python
难度 3/5 半天 新手友好度 78/100
FROWNINGdev/django-orm-lens#266 ·
查看 FROWNINGdev/django-orm-lens 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
gorakhargosh/watchdog#1462 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
openwisp/openwisp-monitoring#889 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
OpenFreeEnergy/Lomap#180 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
roman-corgi/corgisim#327 ·
维护者通常 2 天内回复
-
agent:WSL bug code-review linux pri:medium sev:low tooling
难度 2/5 半天 新手友好度 62/100
维护者通常 1 天内回复