Skip to content

TypedDict 'in' narrowing w/o @final#15698

Open
ikonst wants to merge 3 commits into
python:masterfrom
ikonst:issue-15697
Open

TypedDict 'in' narrowing w/o @final#15698
ikonst wants to merge 3 commits into
python:masterfrom
ikonst:issue-15697

Conversation

@ikonst

@ikonst ikonst commented Jul 18, 2023

Copy link
Copy Markdown
Contributor

We shouldn't require T1 and T2 to be final, to have 'foo' in Union[TD1, TD2] narrowing take place. We previously (#13838) thought we should, but @erictraut made a good point that TypedDicts are structured types (like interfaces in TypeScript) and we shouldn't consider class hierarchy when type-matching but rather just the structure.

Fixes #15697.

@github-actions

This comment has been minimized.

@ikonst

ikonst commented Jul 18, 2023

Copy link
Copy Markdown
Contributor Author

@ikonst

ikonst commented Jul 31, 2023

Copy link
Copy Markdown
Contributor Author

@hauntsaninja since you approved #13838, can take a look?

d_final: DFinal

if 'spam' in d_final:
spam = 'ham' # E: Statement is unreachable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this test really useless? E.g. this seems useful and I'm not sure where else it is tested

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, brought it back in a68b87a (and also removed more @finals).

@github-actions

This comment has been minimized.

@ikonst

ikonst commented Aug 18, 2023

Copy link
Copy Markdown
Contributor Author

@hauntsaninja another review?

@github-actions

Copy link
Copy Markdown
Contributor

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

discord.py (https://github.com/Rapptz/discord.py)
- discord/audit_logs.py:251: error: Argument "allow_list" to "AutoModTrigger" has incompatible type "object"; expected "list[str] | None"  [arg-type]
- discord/audit_logs.py:257: error: Argument "allow_list" to "AutoModTrigger" has incompatible type "object"; expected "list[str] | None"  [arg-type]
- discord/audit_logs.py:258: error: Argument "regex_patterns" to "AutoModTrigger" has incompatible type "object"; expected "list[str] | None"  [arg-type]

@gandhis1

Copy link
Copy Markdown

This issue recently came up on another forum, and it seems the fix is stuck in limbo. The diff seems fairly innocuous, the build/tests pass, and the discussion elsewhere seemed to conclude that this is the correct fix. Further, pyright merged the equivalent fix. Is it possible for this to be revisited?

@ikonst

ikonst commented Jan 12, 2024

Copy link
Copy Markdown
Contributor Author

@hauntsaninja Can we revisit this?

@hauntsaninja

Copy link
Copy Markdown
Collaborator

I think it would be good to try and get whatever behaviour is desired here into the typing spec. Would you or gandhis1 be willing to follow the steps here? https://github.com/python/typing-council#decisions

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.

TypedDict 'in' narrowing w/o @final

3 participants