Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

1.15.0 regression: list.append false positive with Signals enum #18628

Open
sobolevn opened this issue Feb 7, 2025 · 4 comments
Open

1.15.0 regression: list.append false positive with Signals enum #18628

sobolevn opened this issue Feb 7, 2025 · 4 comments
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations

Comments

@sobolevn
Copy link
Member

sobolevn commented Feb 7, 2025

This code:

import signal
signals = []
if hasattr(signal, 'SIGALRM'):
    signals.append(signal.SIGALRM)
if hasattr(signal, 'SIGUSR1'):
    signals.append(signal.SIGUSR1)

Produces:

ex.py:7: error: Argument 1 to "append" of "list" has incompatible type "Literal[Signals.SIGUSR1]"; expected "Literal[Signals.SIGALRM]"  [arg-type]

Source: https://github.com/python/typeshed/blob/main/stdlib/signal.pyi

SIGALRM: Signals
SIGUSR1: Signals

It is a regression of 1.15.0, before this code was correct: https://mypy-play.net/?mypy=1.14.1&python=3.12&gist=24f0696ba047856ad7d932f4a7f25ec4
I don't see a reason, why two enum membres cannot be appended to a single list.

@sobolevn sobolevn added bug mypy got something wrong topic-inference When to infer types or require explicit annotations labels Feb 7, 2025
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2025

I think this can be fixed by using SIGALRM: Final = Signals.SIGALRM etc. in the stubs instead of explicit literal type annotations. Using a literal type annotations instead of Final is likely to cause issues like this. The mypy behavior hasn't changed in a long time, so I doubt it's a regression in mypy.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2025

This is the cause of the regression: python/typeshed#13336

@sobolevn
Copy link
Member Author

I confirmed this with the repro:

from typing import Final, Literal
from enum import Enum

class S(Enum):
    A = 1
    B = 2

A1: Literal[S.A]
B1: Literal[S.B]

signals1 = []
signals1.append(A1)
signals1.append(B1)  # E: Argument 1 to "append" of "list" has incompatible type "Literal[S.B]"; expected "Literal[S.A]"

A2: Final = S.A
B2: Final = S.B

signals2 = []
signals2.append(A2)
signals2.append(B2)

@sobolevn
Copy link
Member Author

I opened python/typeshed#13500

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations
Projects
None yet
Development

No branches or pull requests

2 participants