Skip to content

Narrow membership in statically known containers#21461

Merged
hauntsaninja merged 4 commits into
python:masterfrom
hauntsaninja:narrowmore
May 13, 2026
Merged

Narrow membership in statically known containers#21461
hauntsaninja merged 4 commits into
python:masterfrom
hauntsaninja:narrowmore

Conversation

@hauntsaninja
Copy link
Copy Markdown
Collaborator

@hauntsaninja hauntsaninja commented May 11, 2026

The negative narrowing here could be more aggressive, but we will need better literal handling to unblock it

Builds on #21456

Fixes #13684 in combination with previous PRs. There is one remaining diagnostic, but that one is desirable

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Nice, just one small question.

Comment thread mypy/checker.py Outdated
container_item_types = p_iterable_type.items
else:
container_expr = collapse_walrus(operands[right_index])
if isinstance(container_expr, (TupleExpr, ListExpr, SetExpr)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't the branch above always cover the TupleExpr case? Or do we sometimes infer non-tuple type for a tuple literal? If you know such situation it may be worth adding a specific test case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think it should be always covered. Code ended up this way due to some refactoring I did, updated. Also added some more tests

@github-actions
Copy link
Copy Markdown
Contributor

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

altair (https://github.com/vega/altair)
+ altair/utils/mimebundle.py:138: error: Redundant cast to "Literal['png', 'svg', 'pdf', 'vega']"  [redundant-cast]

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/history/events/structures/swap.py:176: error: Unused "type: ignore" comment  [unused-ignore]

manticore (https://github.com/trailofbits/manticore)
+ manticore/wasm/executor.py:402: error: Unused "type: ignore" comment  [unused-ignore]
+ manticore/wasm/executor.py:1226: error: Unused "type: ignore" comment  [unused-ignore]
+ manticore/wasm/structure.py:1105: error: Unused "type: ignore" comment  [unused-ignore]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/ext/autodoc/_dynamic/_loader.py:237: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/ext/autodoc/_dynamic/_loader.py:260: error: Unused "type: ignore" comment  [unused-ignore]

paasta (https://github.com/yelp/paasta)
+ paasta_tools/kubernetes_tools.py:693: error: Redundant cast to "NodeSelectorInNotIn"  [redundant-cast]
+ paasta_tools/kubernetes_tools.py:696: error: Redundant cast to "NodeSelectorExistsDoesNotExist"  [redundant-cast]
+ paasta_tools/kubernetes_tools.py:699: error: Redundant cast to "NodeSelectorGtLt"  [redundant-cast]

@hauntsaninja hauntsaninja merged commit d2b0234 into python:master May 13, 2026
25 checks passed
@hauntsaninja hauntsaninja deleted the narrowmore branch May 13, 2026 08:10
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.

Support enum type narrowing with in

2 participants