flake8-pyi icon indicating copy to clipboard operation
flake8-pyi copied to clipboard

Y011: Allow enum value defaults

Open srittau opened this issue 9 months ago • 2 comments

For example:

def foo(x: MyEnum = MyEnum.BAR): ...  # Triggers Y011 (only simple defaults)

I think enum value defaults are both simple and quite useful.

Implementation idea: If a default follows the pattern ([\w.]+)\.\w, check whether the type includes the first match group.

srittau avatar Mar 19 '25 12:03 srittau

Implementation idea: If a default follows the pattern ([\w.]+)\.\w, check whether the type includes the first match group.

We might need something a little cleverer -- I think this regex would mean we'd still emit errors on things like

class X(enum.IntEnum):
    MEMBER1 = 1
    MEMBER2 = 2

def foo(x: X | int = X.MEMBER1): ...

AlexWaygood avatar Mar 19 '25 12:03 AlexWaygood

That's what I meant by "includes". I.e. the type itself or - if it's a union - one of the union members must be equivalent to the regex group.

srittau avatar Mar 19 '25 12:03 srittau