pstatix
pstatix

Reputation: 3848

Expected type Union[str, bytes, int] but got Sequence[Union[int, float, str]]

PyCharm is showing this warning and I cannot figure out why.

def record(self, *data: Sequence[Union[int, float, str]]) -> None:

    for field, value in zip(self.fields, data):
        if field.type in {1, 3}:
            try:
                value = int(value)  # warning is here
            except ValueError:
                pass

    # other logic...

Its saying that value from the unpacked zip is the same type as the argument data, but its not nor should it be. If it was an element of a Sequence that would mean it would be a Union[int, float, str] type.

Does PyCharm not realize that zip was unpacked?

Upvotes: 1

Views: 1083

Answers (1)

chepner
chepner

Reputation: 531145

Per PEP 484, the type hint applies to each element of *data, not the sequence itself. You don't need Sequence; that is already implied by the *.

def record(self, *data: Union[int, float, str]) -> None:

Upvotes: 2

Related Questions