Skip to content

Mypy Misinterprets __next__ Return Type in PcapReader Subclass #4728

Open
@carterchannui

Description

@carterchannui

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions