Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Correctly infer x: SomeEnum; x.name as union of literal names #18797

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sobolevn
Copy link
Member

Closes #18786

Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

This makes sense to me

This comment has been minimized.

sobolevn added a commit to sobolevn/psycopg that referenced this pull request Mar 13, 2025
Hi! I am working on mypy in python/mypy#18797

And I've noticed a small thing in your code. Ideally, `SomeEnum.Item.name` should be inferenced as `Literal[NAME_ONE, NAME_TWO, ...]`

But, since you later modify `status` (which is the name of a enum item), it should be explicitly annotated as `str`, not `Literal`.
@sobolevn
Copy link
Member Author

I opened psycopg/psycopg#1023 to fix a "problem" is psycopg.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/pq/misc.py:158: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['IDLE', 'ACTIVE', 'INTRANS', 'INERROR', 'UNKNOWN']")  [assignment]
+ psycopg/psycopg/pq/misc.py:170: error: Incompatible types in assignment (expression has type "Literal['OK', 'BAD', 'STARTED', 'MADE', 'AWAITING_RESPONSE', 'AUTH_OK', 'SETENV', 'SSL_STARTUP', 'NEEDED', 'CHECK_WRITABLE', 'CONSUME', 'GSS_STARTUP', 'CHECK_TARGET', 'CHECK_STANDBY', 'ALLOCATED']", variable has type "Literal['IDLE', 'ACTIVE', 'INTRANS', 'INERROR', 'UNKNOWN']")  [assignment]
+ psycopg/psycopg/pq/misc.py:170: note: Items in the first union not in the second: "Literal['OK']", "Literal['BAD']", "Literal['STARTED']", "Literal['MADE']", "Literal['AWAITING_RESPONSE']", "Literal['AUTH_OK']", "Literal['SETENV']", "Literal['SSL_STARTUP']", "Literal['NEEDED']", "Literal['CHECK_WRITABLE']", "Literal['CONSUME']", "Literal['GSS_STARTUP']", "Literal['CHECK_TARGET']", "Literal['CHECK_STANDBY']", "Literal['ALLOCATED']"

@TeamSpen210
Copy link
Contributor

Should this also handle unions of literals? I notice if you narrow with is, it'll switch back to str again. Similarly if you explicitly annotate with Literal[SomeEnum.field], or a union of two enums.

@sobolevn
Copy link
Member Author

@TeamSpen210 please, share your examples, I would be happy to support them as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum .name property has str type
3 participants