Skip to content

Fix enum stubs: move __init__ to __new__ for IntegerChoices/TextChoices#3215

Open
ahmedasar00 wants to merge 2 commits intotypeddjango:masterfrom
ahmedasar00:fix/enum-init-to-new
Open

Fix enum stubs: move __init__ to __new__ for IntegerChoices/TextChoices#3215
ahmedasar00 wants to merge 2 commits intotypeddjango:masterfrom
ahmedasar00:fix/enum-init-to-new

Conversation

@ahmedasar00
Copy link
Contributor

Refs #3148

Summary

Partially addresses stubtest inconsistencies in IntegerChoices
and TextChoices caused by Python 3.14 enum changes.

Changes

  • Moved overloaded signatures from __init__ to __new__ in
    enums.pyi
  • Renamed parameters to match runtime (xvalue,
    objectvalue)
  • Updated the comment above IntegerChoices to reference __new__

Results

  • Before: 14 stubtest errors
  • After: 10 stubtest errors (only label parameter remains)

Remaining issue

The runtime __new__ signature is just (cls, value) — no
label parameter. The label is handled by the metaclass
ChoicesType.__new__ during class creation.

Keeping label in the stubs is useful for type checking but
doesn't match the runtime. Looking for guidance on whether to:

  1. Remove label to match runtime exactly
  2. Add these entries to the stubtest allowlist

@ahmedasar00 ahmedasar00 force-pushed the fix/enum-init-to-new branch from 91ae6a3 to 3761818 Compare March 23, 2026 20:21
# Django's ChoicesType metaclass processes (value, label) tuples at class definition time,
# but the runtime __new__ (from enum.Enum) only accepts (cls, value).
# We keep the label overload so that defining enum members type-checks correctly.
django.db.models.enums.IntegerChoices.__new__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have absolutely no clue if any of that is correct. Do you have any ideas @ngnpope ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'll have time to look into it this week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants