Parser: a field declared under `if`/`else` in the class body is lost
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- Nửa ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 84
- Fork
- 13
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của FROWNINGdev/django-orm-lens
-
suggest-indexes proposes composite indexes that include the primary key or a unique fieldCó thể đã có người làm @bfs2021 đã nhận hôm nay. Đang mởbug good first issue python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
FROWNINGdev/django-orm-lens#269 ·
-
`migration-risk` text output prints `app.None` for graph-level findingsCó thể đã có người làm @phoenixbycaselka đã nhận hôm nay. Đang mởbug good first issue python
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
FROWNINGdev/django-orm-lens#268 ·
-
Finish the Chinese rule reference — DOL001–DOL007Có thể làm lại được @harbinresearcher đã nhận 64 ngày trước và không có pull request nào đang mở. Đang mởdocumentation good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
FROWNINGdev/django-orm-lens#81 · 2 bình luận ·
-
Fields inherited from Django's `AbstractBaseUser` / `PermissionsMixin` / `AbstractUser` are missingĐang mởbug good first issue python
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
FROWNINGdev/django-orm-lens#267 ·
-
bug good first issue python
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 78/100
FROWNINGdev/django-orm-lens#266 ·
Tất cả issue của FROWNINGdev/django-orm-lens
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
gorakhargosh/watchdog#1462 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
openwisp/openwisp-monitoring#889 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
OpenFreeEnergy/Lomap#180 ·
-
generate_observation_scenario_from_cpgs exposure time conversion when auto-gain set to 'yes'Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
roman-corgi/corgisim#327 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
agent:WSL bug code-review linux pri:medium sev:low tooling
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày