Skip to content

Do not treat enum.nonmember values as enum members - #3468

Open
PDGGK wants to merge 2 commits into
pylint-dev:mainfrom
PDGGK:fix-enum-nonmember-not-a-member
Open

PDGGK wants to merge 2 commits into
pylint-dev:mainfrom
PDGGK:fix-enum-nonmember-not-a-member

Conversation

@PDGGK

@PDGGK PDGGK commented Nov 9, 2026

Copy link
Copy Markdown

Type of Changes

Type
✓ 🐛 Bug fix

Description

A value wrapped in enum.nonmember() is not a member of the enum, but infer_enum_class handled it like any other assignment and listed it in __members__. pylint reads that dict in _check_enum_base, so extending such a class reports invalid-enum-extension on valid code. Django 6.1.3 declares do_not_call_in_templates = enum.nonmember(True) on models.Choices, so an ordinary choices class in a Django 6 project fails:

from django.db import models


class MyChoices(models.Choices):
    FOO = "FOO", "A foo"
$ pylint --disable=all --enable=invalid-enum-extension --score=n django_choices.py
************* Module django_choices
django_choices.py:5:0: E0244: Extending inherited Enum class "Choices" (invalid-enum-extension)

That is Django 6.1.3, pylint 4.1.3 and astroid main (d5319a4) on Python 3.13.16, exit code 2. The problem was reported in a comment on pylint-dev/pylint#6887, which has a reproducer using only the standard library. I ran that one too: pylint reports E0244, and at runtime dict(MyEnum.__members__) holds only the real member.

infer_enum_class now skips an assignment whose value is a call to enum.nonmember, so the name stays a plain class attribute and stays out of __members__. The call is matched by name and confirmed with inference, so a local function that happens to be called nonmember still produces a member. With this branch and pylint main (79c8d0c) the command above prints nothing and exits 0, and so do subclasses of models.IntegerChoices and models.TextChoices.

Two things are unchanged. Reading the attribute back does not infer the wrapped value: Base.LABEL.upper() with LABEL = enum.nonmember("label") gets no-member before and after, and only the name in the message changes. An aliased import (from enum import nonmember as nm) is not recognised.

Tests: three new tests in tests/brain/test_enum.py. Without the new check in infer_enum_class, test_enum_nonmember_is_not_a_member fails with assert ['FLAG', 'ANNOTATED', 'RED'] == ['RED'] and test_enum_with_only_nonmembers_can_be_extended fails on the non-empty __members__. The test for the local function passes either way, as it should. tests/brain, tests/test_inference.py, tests/test_scoped_nodes.py, tests/test_regrtest.py and tests/test_object_model.py give 1208 passed, 82 skipped, 15 xfailed. On pylint main against this branch, the functional tests selected with -k enum give 12 passed. ruff check, black --check, mypy and pylint with the repo's pylintrc are clean.

All of that ran on Python 3.13 in a container. I did not run the rest of the astroid suite, the rest of pylint's tests, or pyright, and left those to CI.

Refs pylint-dev/pylint#6887

Closes #3467

@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.4.0 milestone Nov 15, 2026
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.24%. Comparing base (d5319a4) to head (40f8326).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3468   +/-   ##
=======================================
  Coverage   91.23%   91.24%           
=======================================
  Files          93       93           
  Lines       11881    11889    +8     
  Branches     1960     1962    +2     
=======================================
+ Hits        10840    10848    +8     
  Misses        732      732           
  Partials      309      309           
Flag Coverage Δ
linux 90.92% <100.00%> (+<0.01%) ⬆️
pypy 91.24% <100.00%> (+<0.01%) ⬆️
windows 91.08% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/brain/brain_namedtuple_enum.py 92.50% <100.00%> (+0.20%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing PDGGK:fix-enum-nonmember-not-a-member (40f8326) with main (d5319a4)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

PDGGK and others added 2 commits November 20, 2026 23:13
``infer_enum_class`` turned every assignment in an enum body into a
member, including values wrapped in ``enum.nonmember()``. The name ended
up in ``__members__``, so extending such a class made pylint report
``invalid-enum-extension``. Django 6 declares
``do_not_call_in_templates = enum.nonmember(True)`` on ``models.Choices``,
so every ``models.Choices`` subclass in a Django 6 project hit it.

Skip those assignments and leave the name as a plain class attribute.
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the fix-enum-nonmember-not-a-member branch from eee012c to 0907d1e Compare November 20, 2026 22:18

This branch has not been deployed

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

Labels

Brain 🧠 Needs a brain tip Bug 🪳

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enum.nonmember() values are listed in __members__, so pylint reports invalid-enum-extension

2 participants