The problem is that when initializing a variable from a dictionary expression it treats the type as invariant, but in the case of initialialization, it is covariant.
Example:
CODES = {
"a": "b",
"c": "d",
"e": "f",
}
# MyPy rejects this
PATTERNS: dict[str, tuple[str | None, str | None]] = {
"1" + k: ((v + "1" if v else None), "x")
for (k, v) in CODES.items()
} | {
"2" + k: ((v + "2" if v else None), "x")
for (k, v) in CODES.items()
}
# But is OK with this:
X: str | None = "x"
PATTERNS2: dict[str, tuple[str | None, str | None]] = {
"1" + k: ((v + "1" if v else None), X)
for (k, v) in CODES.items()
} | {
"1" + k: ((v + "1" if v else None), X)
for (k, v) in CODES.items()
}
Both patterns should be OK.