Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Generated Python models report optional fields as required in pyright/Pylance

Aperta
#394 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, python
Ambito
cli, tooling

Direzione di ricerca

Start with the crossplane dependency update-cache and crossplane project build entry points, then inspect how they invoke datamodel-code-generator for schemas/python/models. Reproduce the issue with the supplied Kubernetes v1.36.4 configuration and pyright snippet. Done means generated optional fields are accepted by pyright/Pylance without errors while remaining valid at runtime.

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

Descrizione

bug
What happened?

The Python models that crossplane dependency update-cache (and crossplane project build) generate have many optional fields that type checkers report as required.

datamodel-code-generator writes the default of a field as a positional argument of Field when the field also has other arguments:

continue_: str | None = Field(None, alias='continue')
metadata: v1.ObjectMeta | None = Field({}, validate_default=True)

Pyright (and so Pylance, the default Python language server of VS Code) reads a default only from the default= keyword. It reports each of these fields as a required argument:

from models.io.k8s.api.apps.v1 import Deployment, DeploymentSpec
from models.io.k8s.api.core.v1 import PodTemplateSpec
from models.io.k8s.api.networking.v1 import IPBlock, NetworkPolicyIngressRule
from models.io.k8s.apimachinery.pkg.apis.core.meta.v1 import LabelSelector, ListMeta

Deployment()
DeploymentSpec(selector=LabelSelector(), template=PodTemplateSpec())
IPBlock(cidr="10.0.0.0/8")
NetworkPolicyIngressRule()
ListMeta()
t.py:6:1 - error: Arguments missing for parameters "metadata", "spec", "status" (reportCallIssue)
t.py:7:1 - error: Argument missing for parameter "strategy" (reportCallIssue)
t.py:7:51 - error: Arguments missing for parameters "metadata", "spec" (reportCallIssue)
t.py:8:1 - error: Argument missing for parameter "except" (reportCallIssue)
t.py:9:1 - error: Argument missing for parameter "from" (reportCallIssue)
t.py:10:1 - error: Argument missing for parameter "continue" (reportCallIssue)
6 errors, 0 warnings, 0 informations

Pydantic itself accepts these calls, because the defaults are correct at runtime. Only static type checks fail. In the Kubernetes v1.36.4 core API models, 146 fields use Field({}, ...), plus the fields renamed from Python keywords (continue_, except_, from_, and int_/bool_ since #63).

Expected: no errors. The fields have a default, so they are optional.

How can we reproduce it?
cat > crossplane-project.yaml <<'EOF'
apiVersion: dev.crossplane.io/v1alpha1
kind: Project
metadata:
  name: repro
spec:
  repository: example.com/repro
  schemas:
    languages: [python]
  dependencies:
    - type: k8s
      k8s:
        version: v1.36.4
EOF
crossplane dependency update-cache
grep -rn "Field(None, alias=\|Field({}" schemas/python/models | head

Then run pyright on the snippet above with schemas/python on the Python path (for example pyrightconfig.json with "extraPaths": ["schemas/python"]).

What environment did it happen in?
  • Crossplane CLI version: v2.5.0
  • Platform (e.g., linux/amd64): linux/amd64
  • Crossplane version (if applicable): n/a
  • datamodel-code-generator: 0.59.0 (pinned by the CLI), pyright 1.1.406, pydantic 2
Lingua principale
Go
Stelle
19
Fork
33
Merge medio
2g 21h
PR unite (30g)
45

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 crossplane/cli

Tutte le issue di crossplane/cli

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.