mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Infer type var tuple contents in more situations

Open A5rocks opened this issue 8 months ago • 8 comments

Supersedes https://github.com/python/mypy/pull/17512 as I figured out how to handle this case in a more general manner.

This PR fixes https://github.com/python/mypy/issues/16522. It adds a mechanism for constraints to be discarded if they are less likely to be correct: for instance, if you pass f(x: int, y: str = "42") to Callable[[Unpack[Ts]], None] it's not clear whether y should be included in an unpacked TypeVarTuple. This PR makes it such that function_using_typevartuple(f, 42) discards constraints based on f.

Alternatively, I could make it such that TypeVarTuples can be linked to Parameters. I haven't done that since it's a larger and slightly strange change, but if that works better I would be happy to.

A5rocks avatar Apr 23 '25 17:04 A5rocks

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

vision (https://github.com/pytorch/vision)
+ torchvision/utils.py:270: error: Unused "type: ignore" comment  [unused-ignore]

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:58: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:58: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:58: note: Error code "arg-type" not covered by "type: ignore" comment
+ homeassistant/components/homekit/accessories.py:656: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[tuple[str, ...]]"; expected "SignalType[tuple[str]]"  [arg-type]

github-actions[bot] avatar Apr 23 '25 18:04 github-actions[bot]

The only bad change in mypy primer is:

+ homeassistant/components/homekit/accessories.py:656: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[tuple[str, ...]]"; expected "SignalType[tuple[str]]"  [arg-type]

... and I'm not quite sure if there's a good way to solve this. We could check whether constraints interfere with each other and only if they do prefer the higher quality ones? That sounds slow, though. (EDIT: I did that, but again I'm not confident it's right)

A5rocks avatar Apr 24 '25 00:04 A5rocks

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

vision (https://github.com/pytorch/vision)
- torchvision/utils.py:270: error: Unused "type: ignore" comment  [unused-ignore]

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:58: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:58: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:58: note: Error code "arg-type" not covered by "type: ignore" comment

github-actions[bot] avatar Apr 24 '25 01:04 github-actions[bot]

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

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:57: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:57: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:57: note: Error code "arg-type" not covered by "type: ignore" comment

github-actions[bot] avatar Oct 04 '25 18:10 github-actions[bot]

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

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:57: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:57: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:57: note: Error code "arg-type" not covered by "type: ignore" comment

github-actions[bot] avatar Oct 04 '25 18:10 github-actions[bot]

@ilevkivskyi this is my best guess at a mechanism that would handle TypeVarTuple inference when there's also kwargs that need to be discarded. Does it seem reasonable?

A5rocks avatar Nov 25 '25 16:11 A5rocks

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

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:57: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:57: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:57: note: Error code "arg-type" not covered by "type: ignore" comment

github-actions[bot] avatar Nov 25 '25 16:11 github-actions[bot]

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

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/layers.py:530: error: Unused "type: ignore" comment  [unused-ignore]
+ sockeye/layers.py:873: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
+ homeassistant/util/signal_type.pyi:57: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/util/signal_type.pyi:57: error: Argument 2 to "async_dispatcher_send" has incompatible type "SignalType[int]"; expected "SignalType[str]"  [arg-type]
+ homeassistant/util/signal_type.pyi:57: note: Error code "arg-type" not covered by "type: ignore" comment

github-actions[bot] avatar Nov 26 '25 02:11 github-actions[bot]