google / google/magika

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

Open Beginner friendly
#1,403 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
18.6k
Forks
1.2k
Avg merge
3d 12h
Merged PRs (30d)
20

Description

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:

```python
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.

Contributor guide

Open the contributing guide

Research direction

Start at Magika.identify_stream and reproduce the crash with the standard-library io.FileIO example from the issue. Check the existing stream capability checks around the BufferedIOBase isinstance check, then make runtime behavior consistent with the BinaryIO signature or update the signature to reflect the required type. Done means the supported behavior and type checking agree without breaking the existing seek, read, and tell validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.