Parser: a field declared under `if`/`else` in the class body is lost

オープン
#253 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

@X6M-Work がすでに取り組んでいます。

2026年11月14日 から。

  • #259 @X6M-Work による — オープン

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
django, python

調査の方向性

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 の本文から書いたものです。

説明

bug help wanted python

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.py with the repro: three fields, title_upper once.
  • A method with an if inside (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, ruff and mypy pass.

Difficulty: help wanted, a few hours. Ask in the issue before you start if the approach is unclear.

主要言語
Python
スター
84
フォーク
13
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

FROWNINGdev/django-orm-lens のほかの issue

FROWNINGdev/django-orm-lens の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。