mcocdawc
mcocdawc

Reputation: 1867

Confusion about type-checking nested Sequence

I have code that in a minimal example looks like this.

from ordered_set import OrderedSet
from collections.abc import Sequence

from typing import NewType, TypeAlias

SeqOverFrag: TypeAlias = Sequence
MotifIdx = NewType("MotifIdx", int)
AOIdx = NewType("AOIdx", int)

x = []
x.append({MotifIdx(1): OrderedSet([AOIdx(1), AOIdx(2)])})



y: Sequence[dict[MotifIdx, Sequence[AOIdx]]] = x

which raises the error

Incompatible types in assignment (expression has type "list[dict[MotifIdx, OrderedSet[AOIdx]]]", variable has type "Sequence[dict[MotifIdx, Sequence[AOIdx]]]")

If I explicitly declare x

x: list[dict[MotifIdx, Sequence[AOIdx]]] = []

or if I initialize it directly

x = [{MotifIdx(1): OrderedSet([AOIdx(1), AOIdx(2)])}]

then the error goes away.

The fixes suggest that there is something wrong with the type deduction, on the other hand the error message shows, that the type deduction worked (?). Is this a bug in mypy?

Upvotes: 0

Views: 53

Answers (0)

Related Questions