Django: ManyToMany `.add()` / `.set()` reject UUID primary keys with `bad-argument-type`

Aperta
#5,448 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
django, python, rust

Direzione di ricerca

Start with the provided repro.py example and run the two commands to confirm that Django accepts the UUID IDs while Pyrefly reports bad-argument-type for .add() and .set(). Then trace how Pyrefly checks these ManyToMany methods and handles Django model primary key types. Done means both calls accept UUID primary keys without a type error while preserving the reported runtime behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

django language-server
Describe the Bug

Adding a related object by its UUID id works, but the type checker rejects it...

# Prints `['news', 'sport']`, so Django accepts these ids at runtime
uv run --script repro.py

# However, type check fails with `bad-argument-type`.
uv run --no-project --with-requirements repro.py pyrefly check --preset default repro.py
# file: repro.py

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "django==6.1.2",
#     "django-stubs==6.1.2",
#     "pyrefly==1.4.0",
# ]
# ///

import uuid

import django
from django.conf import settings

settings.configure(
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
    INSTALLED_APPS=["__main__"],
)
django.setup()

from django.db import connection, models  # noqa: E402


class Tag(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    name = models.CharField(max_length=50)


class Article(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    tags = models.ManyToManyField(Tag)


with connection.schema_editor() as editor:
    editor.create_model(Tag)
    editor.create_model(Article)

news = Tag.objects.create(name="news")
sport = Tag.objects.create(name="sport")
article = Article.objects.create()

tag_id: uuid.UUID = news.id
article.tags.add(tag_id)
article.tags.set([news.id, sport.id])

print(sorted(article.tags.values_list("name", flat=True)))
Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Lingua principale
Rust
Stelle
7.2k
Fork
578
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di facebook/pyrefly

Tutte le issue di facebook/pyrefly

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.