Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Jul 24, 2025

Summary

I saw that this creates a lot of false positives in the ecosystem, and it seemed to be relatively easy to add basic support for this.

Some preliminary work on this was done by @InSyncWithFoo — thank you.

part of astral-sh/ty#111

Ecosystem analysis

The results look good.

Test Plan

New Markdown tests

@sharkdp sharkdp added ty Multi-file analysis & type inference ecosystem-analyzer labels Jul 24, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 24, 2025

mypy_primer results

Changes were detected when running on open source projects
anyio (https://github.com/agronholm/anyio)
- src/anyio/streams/text.py:45:5: error[invalid-assignment] Object of type `Literal["utf-8"]` is not assignable to `InitVar[str]`
- src/anyio/streams/text.py:46:5: error[invalid-assignment] Object of type `Literal["strict"]` is not assignable to `InitVar[str]`
- src/anyio/streams/text.py:85:5: error[invalid-assignment] Object of type `Literal["utf-8"]` is not assignable to `InitVar[str]`
- src/anyio/streams/text.py:124:5: error[invalid-assignment] Object of type `Literal["utf-8"]` is not assignable to `InitVar[str]`
- src/anyio/streams/text.py:125:5: error[invalid-assignment] Object of type `Literal["strict"]` is not assignable to `InitVar[str]`
- src/anyio/streams/text.py:131:36: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- src/anyio/streams/text.py:131:55: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- src/anyio/streams/text.py:134:36: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- Found 91 diagnostics
+ Found 83 diagnostics

kopf (https://github.com/nolar/kopf)
- kopf/_cogs/structs/references.py:269:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[None | str | Marker]`
- kopf/_cogs/structs/references.py:270:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[None | str | Marker]`
- kopf/_cogs/structs/references.py:271:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[None | str | Marker]`
- kopf/_cogs/structs/references.py:272:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[None]`
- kopf/_cogs/structs/references.py:394:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["apiextensions.k8s.io"]`
- kopf/_cogs/structs/references.py:394:41: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["customresourcedefinitions"]`
- kopf/_cogs/structs/references.py:395:19: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["v1"]`
- kopf/_cogs/structs/references.py:395:25: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["events"]`
- kopf/_cogs/structs/references.py:396:23: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["events.k8s.io"]`
- kopf/_cogs/structs/references.py:396:40: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["events"]`
- kopf/_cogs/structs/references.py:397:23: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["v1"]`
- kopf/_cogs/structs/references.py:397:29: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["namespaces"]`
- kopf/_cogs/structs/references.py:398:31: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["kopf.dev/v1"]`
- kopf/_cogs/structs/references.py:398:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["clusterkopfpeerings"]`
- kopf/_cogs/structs/references.py:399:31: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["zalando.org/v1"]`
- kopf/_cogs/structs/references.py:399:49: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["clusterkopfpeerings"]`
- kopf/_cogs/structs/references.py:400:34: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["kopf.dev/v1"]`
- kopf/_cogs/structs/references.py:400:49: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["kopfpeerings"]`
- kopf/_cogs/structs/references.py:401:34: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["zalando.org/v1"]`
- kopf/_cogs/structs/references.py:401:52: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["kopfpeerings"]`
- kopf/_cogs/structs/references.py:402:29: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["admissionregistration.k8s.io"]`
- kopf/_cogs/structs/references.py:402:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["mutatingwebhookconfigurations"]`
- kopf/_cogs/structs/references.py:403:31: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["admissionregistration.k8s.io"]`
- kopf/_cogs/structs/references.py:403:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `Literal["validatingwebhookconfigurations"]`
- kopf/on.py:184:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:184:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:184:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:244:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:244:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:244:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:301:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:301:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:301:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:357:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:357:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:357:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:415:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:415:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:415:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:472:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:472:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:472:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:530:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:530:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:530:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:586:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:586:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:586:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:636:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:636:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:636:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:694:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:694:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:694:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:756:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:756:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- kopf/on.py:756:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[None | str | Marker]`, found `str | Marker | None`
- Found 117 diagnostics
+ Found 60 diagnostics

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:665:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[ZenConvert | None]`
- Found 570 diagnostics
+ Found 569 diagnostics

strawberry (https://github.com/strawberry-graphql/strawberry)
- strawberry/schema/config.py:17:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[bool]`
+ strawberry/schema/config.py:17:5: error[invalid-assignment] Object of type `None` is not assignable to `bool`
- strawberry/schema/schema.py:433:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | None]`, found `str | None`
- strawberry/types/execution.py:46:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[str | None]`
- Found 365 diagnostics
+ Found 363 diagnostics

poetry (https://github.com/python-poetry/poetry)
- src/poetry/console/exceptions.py:101:5: error[invalid-assignment] Object of type `Literal[""]` is not assignable to `InitVar[str]`
- src/poetry/console/exceptions.py:211:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["    | "]`
- src/poetry/vcs/git/backend.py:212:33: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[Unknown | Path]`, found `Unknown | Path`
- Found 935 diagnostics
+ Found 932 diagnostics

pytest (https://github.com/pytest-dev/pytest)
- src/_pytest/fixtures.py:1219:5: error[invalid-assignment] Object of type `Literal[False]` is not assignable to `InitVar[bool]`
- src/_pytest/fixtures.py:1389:9: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- testing/example_scripts/dataclasses/test_compare_initvar.py:15:16: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[1]`
- testing/example_scripts/dataclasses/test_compare_initvar.py:15:29: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[1]`
- Found 496 diagnostics
+ Found 492 diagnostics

bokeh (https://github.com/bokeh/bokeh)
- src/bokeh/plotting/gmap.py:78:67: error[invalid-argument-type] Argument to function `_get_num_minor_ticks` is incorrect: Expected `int | Literal["auto"] | None`, found `InitVar[Unknown | int]`
- src/bokeh/plotting/gmap.py:84:67: error[invalid-argument-type] Argument to function `_get_num_minor_ticks` is incorrect: Expected `int | Literal["auto"] | None`, found `InitVar[Unknown | int]`
- src/bokeh/plotting/gmap.py:87:55: error[invalid-argument-type] Argument to function `process_tools_arg` is incorrect: Expected `Sequence[Tool | str]`, found `InitVar[Sequence[str | Tool]]`
- src/bokeh/plotting/gmap.py:87:67: error[invalid-argument-type] Argument to function `process_tools_arg` is incorrect: Expected `str | list[tuple[str, str]] | None`, found `InitVar[Template | str | list[tuple[str, str]] | None]`
+ src/bokeh/plotting/gmap.py:87:67: error[invalid-argument-type] Argument to function `process_tools_arg` is incorrect: Expected `str | list[tuple[str, str]] | None`, found `Template | str | list[tuple[str, str]] | None`
- Found 837 diagnostics
+ Found 834 diagnostics

meson (https://github.com/mesonbuild/meson)
- mesonbuild/cargo/interpreter.py:295:5: error[invalid-assignment] Object of type `None` is not assignable to `InitVar[str | None]`
- mesonbuild/compilers/c.py:132:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/c.py:264:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/c.py:415:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/cpp.py:245:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:252:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/compilers/cpp.py:258:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/compilers/cpp.py:265:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/cpp.py:418:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:469:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:476:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/compilers/cpp.py:482:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/compilers/cpp.py:489:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/cpp.py:606:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:613:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/compilers/cpp.py:695:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:702:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/compilers/cpp.py:708:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/compilers/cpp.py:804:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/compilers/cpp.py:811:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/compilers/cpp.py:817:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `Unknown | list[Unknown]`
- mesonbuild/compilers/cuda.py:643:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["none"]`
- mesonbuild/compilers/cuda.py:650:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/compilers/cython.py:77:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["3"]`
- mesonbuild/compilers/cython.py:84:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["c"]`
- mesonbuild/compilers/fortran.py:124:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["none"]`
- mesonbuild/compilers/mixins/emscripten.py:66:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[4]`
- mesonbuild/compilers/rust.py:241:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["none"]`
- mesonbuild/compilers/rust.py:248:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/compilers/swift.py:127:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["none"]`
- mesonbuild/coredata.py:387:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[0]`
- mesonbuild/coredata.py:393:17: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/msubprojects.py:747:33: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[Resolver]`, found `Resolver`
- mesonbuild/options.py:688:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:689:60: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["bin"]`
- mesonbuild/options.py:690:60: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:691:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:692:60: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:693:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:694:62: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/options.py:695:72: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:696:64: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:697:72: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["var"]`
- mesonbuild/options.py:698:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:699:68: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:700:87: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["com"]`
- mesonbuild/options.py:701:66: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `str`
- mesonbuild/options.py:707:85: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["auto"]`
- mesonbuild/options.py:708:54: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["ninja"]`
- mesonbuild/options.py:713:13: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["vs2022"]`
- mesonbuild/options.py:716:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["debug"]`
- mesonbuild/options.py:717:82: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:718:68: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["shared"]`
- mesonbuild/options.py:720:94: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["shared"]`
- mesonbuild/options.py:722:88: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:723:102: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[Literal["preserve"] | OctalInt]`, found `OctalInt`
- mesonbuild/options.py:724:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["mirror"]`
- mesonbuild/options.py:725:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- mesonbuild/options.py:726:99: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:727:79: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:728:64: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:729:49: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["off"]`
- mesonbuild/options.py:730:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[4]`
- mesonbuild/options.py:731:75: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["1"]`
- mesonbuild/options.py:733:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:734:51: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/options.py:735:93: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `list[Unknown]`
- mesonbuild/options.py:736:74: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:739:95: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:742:80: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[0]`
- mesonbuild/options.py:743:89: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["prefix"]`
- mesonbuild/options.py:745:104: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/options.py:746:108: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/options.py:747:105: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:755:103: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `list[Unknown]`
- mesonbuild/options.py:756:103: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `list[Unknown]`
- mesonbuild/options.py:774:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:775:66: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:776:95: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[int]`, found `Literal[0]`
- mesonbuild/options.py:777:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default"]`
- mesonbuild/options.py:778:104: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:779:93: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal[""]`
- mesonbuild/options.py:780:70: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str | list[str]]`, found `list[Unknown]`
- mesonbuild/options.py:781:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:782:77: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:784:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["off"]`
- mesonbuild/options.py:785:70: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:787:49: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["always"]`
- mesonbuild/options.py:789:44: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["false"]`
- mesonbuild/options.py:790:92: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- mesonbuild/options.py:791:81: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:792:92: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- mesonbuild/options.py:794:60: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["from_buildtype"]`
- unittests/optiontests.py:20:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["somevalue"]`
- unittests/optiontests.py:32:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["somevalue"]`
- unittests/optiontests.py:45:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:47:64: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["ninja"]`
- unittests/optiontests.py:64:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["false"]`
- unittests/optiontests.py:91:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["somevalue"]`
- unittests/optiontests.py:100:60: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["original"]`
- unittests/optiontests.py:104:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["reset"]`
- unittests/optiontests.py:114:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["top"]`
- unittests/optiontests.py:118:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["sub"]`
- unittests/optiontests.py:128:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["top"]`
- unittests/optiontests.py:137:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["top"]`
- unittests/optiontests.py:141:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["sub"]`
- unittests/optiontests.py:153:63: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["top"]`
- unittests/optiontests.py:157:65: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["sub"]`
- unittests/optiontests.py:171:59: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default1"]`
- unittests/optiontests.py:177:61: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["default2"]`
- unittests/optiontests.py:197:30: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["c++11"]`
- unittests/optiontests.py:240:30: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["c++11"]`
- unittests/optiontests.py:274:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:276:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[False]`
- unittests/optiontests.py:278:46: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- unittests/optiontests.py:295:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:297:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- unittests/optiontests.py:316:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:318:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- unittests/optiontests.py:336:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:338:57: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- unittests/optiontests.py:357:78: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:359:54: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["both"]`
- unittests/optiontests.py:382:82: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["/usr"]`
- unittests/optiontests.py:384:67: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["debug"]`
- unittests/optiontests.py:386:71: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- unittests/optiontests.py:388:90: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- unittests/optiontests.py:401:72: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[str]`, found `Literal["0"]`
- Found 885 diagnostics
+ Found 757 diagnostics

rotki (https://github.com/rotki/rotki)
- rotkehlchen/assets/asset.py:77:5: error[invalid-assignment] Object of type `Literal[False]` is not assignable to `InitVar[bool]`
- rotkehlchen/assets/asset.py:345:50: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- rotkehlchen/assets/asset.py:396:52: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- rotkehlchen/assets/asset.py:567:49: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- rotkehlchen/assets/asset.py:669:44: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- rotkehlchen/assets/asset.py:739:52: error[invalid-argument-type] Argument is incorrect: Expected `InitVar[bool]`, found `Literal[True]`
- Found 1570 diagnostics
+ Found 1564 diagnostics
No memory usage changes detected ✅

@github-actions
Copy link
Contributor

github-actions bot commented Jul 24, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 0 197 1
invalid-assignment 0 15 1
Total 0 212 2

Full report with detailed diff

@AlexWaygood
Copy link
Member

Here are some mypy issues regarding InitVar edge cases, which it might be useful to add tests for:

There are also quite a few dataclass/InitVar crashes mypy has had in the past, though I suspect most of them aren't something that could happen to us, so it may not be useful for us to add tests for most of them: https://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20InitVar

@sharkdp
Copy link
Contributor Author

sharkdp commented Jul 24, 2025

Error on instance property and init-only variable with the same name in a dataclass python/mypy#12046

Hm.. that would be difficult to support. And that example doesn't even appear to work at runtime for me (TypeError: non-default argument '_prop' follows default argument 'prop')

Assigning to an attribute with the same name as a dataclass InitVar raises has no attribute on 0.960 python/mypy#12877

Okay. Made some changes to support that scenario, and added a test.

@sharkdp sharkdp merged commit dc6be45 into main Jul 24, 2025
37 checks passed
@sharkdp sharkdp deleted the david/initvar branch July 24, 2025 14:33
@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 24, 2025

Assigning to an attribute with the same name as a dataclass InitVar raises has no attribute on 0.960 python/mypy#12877

Okay. Made some changes to support that scenario, and added a test.

Oh, thank you! Though I wasn't necessarily asking for any changes to the implementation here, just for an explicit test so it was obvious what our behaviour was. Sorry if I was unclear there.

Mypy decided that its behaviour here was a feature rather than a bug, so I think sticking with what you had before would have been defensible -- you could argue that creating an implicit instance variable by assigning to that attribute in an instance method contradicts the explicit declaration of the variable as InitVar (and InitVar only?) in the class body.

That said, I also think the new behaviour you've implemented is fine. It obviously works fine at runtime, and we're generally more permissive than other type checkers when it comes to our understanding of the set of implicit instance variables a class has -- being more permissive here feels in line with our general philosophy there.

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Nice!

UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request Jul 24, 2025
…hlight

* 'main' of https://github.com/astral-sh/ruff:
  [ty] Minor: fix incomplete docstring (astral-sh#19534)
  [ty] Move server tests as integration tests (astral-sh#19522)
  [`ruff`] Offer fixes for `RUF039` in more cases (astral-sh#19065)
  [ty] Support `dataclasses.InitVar` (astral-sh#19527)
  [`ruff`] Fix `RUF033` breaking with named default expressions (astral-sh#19115)
  Update pre-commit hook name (astral-sh#19530)
  Bump 0.12.5 (astral-sh#19528)
  [ty] Rename type_api => ty_extensions (astral-sh#19523)
  [ty] Added support for "go to references" in ty playground. (astral-sh#19516)
  [ty] Return a tuple spec from the iterator protocol (astral-sh#19496)
  [ty] Exhaustiveness checking & reachability for `match` statements (astral-sh#19508)
  [ty] Fix narrowing and reachability of class patterns with arguments (astral-sh#19512)
UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request Jul 24, 2025
* main:
  [ty] Added support for "document highlights" language server feature. (astral-sh#19515)
  Add support for specifying minimum dots in detected string imports (astral-sh#19538)
  [ty] Minor: fix incomplete docstring (astral-sh#19534)
  [ty] Move server tests as integration tests (astral-sh#19522)
  [`ruff`] Offer fixes for `RUF039` in more cases (astral-sh#19065)
  [ty] Support `dataclasses.InitVar` (astral-sh#19527)
  [`ruff`] Fix `RUF033` breaking with named default expressions (astral-sh#19115)
  Update pre-commit hook name (astral-sh#19530)
  Bump 0.12.5 (astral-sh#19528)
  [ty] Rename type_api => ty_extensions (astral-sh#19523)
  [ty] Added support for "go to references" in ty playground. (astral-sh#19516)

# Conflicts:
#	crates/ty_server/src/server/api/requests.rs
#	crates/ty_server/tests/e2e/snapshots/e2e__initialize__initialization.snap
#	crates/ty_server/tests/e2e/snapshots/e2e__initialize__initialization_with_workspace.snap
AlexWaygood added a commit that referenced this pull request Jul 25, 2025
## Summary

I saw that this creates a lot of false positives in the ecosystem, and
it seemed to be relatively easy to add basic support for this.

Some preliminary work on this was done by @InSyncWithFoo — thank you.

part of astral-sh/ty#111

## Ecosystem analysis

The results look good.

## Test Plan

New Markdown tests

---------

Co-authored-by: InSync <insyncwithfoo@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants