-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
base: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this 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.
This comment has been minimized.
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`.
I opened psycopg/psycopg#1023 to fix a "problem" is psycopg. |
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']"
|
Should this also handle unions of literals? I notice if you narrow with |
@TeamSpen210 please, share your examples, I would be happy to support them as well :) |
Closes #18786