Skip to content

identify_stream() crashes on valid BinaryIO streams (like io.FileIO) #1403

Description

@chenzhongxiang

Hi!
There seems to be a mismatch between the type hint and the runtime check in Magika.identify_stream().

The signature accepts BinaryIO, which makes static checkers (like mypy/pyright) perfectly happy with standard unbuffered streams like io.FileIO. However, it crashes at runtime because of the strict isinstance(stream, io.BufferedIOBase) check.

Here is a minimal reproduction using only the standard library:

import io
from typing import BinaryIO
from magika import Magika

stream: BinaryIO = io.FileIO('any_file.txt', 'rb') 

m = Magika()
# Crashes !!
m.identify_stream(stream)

Since the code already uses hasattr to check for seek, read, and tell right after the isinstance check, maybe the strict BufferedIOBase check could just be removed to fully support duck typing? Or if BufferedIOBase is strictly required, maybe update the type hint so static checkers can catch this early.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions