scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Mypy Misinterprets __next__ Return Type in PcapReader Subclass

Open carterchannui opened this issue 8 months ago • 1 comments

Brief description

When using mypy to type-check code involving the PcapReader class, mypy incorrectly infers the return type of the __next__ method. Specifically, it interprets PcapReader.__next__ as returning the type defined in RawPcapReader.__next__ (i.e., Tuple[bytes, RawPcapReader.PacketMetadata]), even though PcapReader overrides this method to return a Packet.

Scapy version

2.6.1

Python version

3.10.12

Operating system

Ubuntu 22.04.4 LTS

Additional environment information

No response

How to reproduce

  1. Define the PcapReader class.
  2. Run mypy on a script that uses PcapReader in an iteration context, such as:
for packet in PcapReader(path):
    print(packet.time)
  1. Observe that mypy reports the type of packet as Tuple[bytes, RawPcapReader.PacketMetadata] instead of Packet.

Actual result

error: "tuple[bytes, PacketMetadata]" has no attribute "time" [attr-defined]

Expected result

Mypy interprets packet, as returned by PcapReader's __next__, as of type Packet.

Related resources

No response

carterchannui avatar Apr 25 '25 01:04 carterchannui

That sure is annoying. We should check that it's not possible to force type this function :/ You can add a # type: ignore on the line that errors in the meantime.

gpotter2 avatar Apr 25 '25 11:04 gpotter2