Skip to content

Type hints for read_json() not updated for file-like input #419

@jmarshall

Description

@jmarshall

What happens?

When type checking the program below, which invokes read_json() on a StringIO, e.g. with pyright, errors like the following are produced:

/path/to/foobar.py:8:22 - error: Argument of type "StringIO" cannot be assigned to parameter "path_or_buffer" of type "str | bytes | PathLike[str]" in function "read_json"
    Type "StringIO" is not assignable to type "str | bytes | PathLike[str]"
      "StringIO" is not assignable to "str"
      "StringIO" is not assignable to "bytes"
      "StringIO" is incompatible with protocol "PathLike[str]"
        "__fspath__" is not present (reportArgumentType)
…

Since duckdb/duckdb#13040 the read_json() methods can also read from file-like objects. Akin to #279, the read_json() stubs in _duckdb-stubs/__init__.pyi also need to have … | typing.IO[bytes] or similar added to the acceptable types for their path_or_buffer parameters.

To Reproduce

import duckdb
from io import StringIO

def foo():
    text = StringIO()
    text.write('["hello", "world"]')

    duckdb.read_json(text)

    duck = duckdb.connect()
    duck.read_json(text)

Run this through a type-checker, e.g., pyright.

OS:

macOS arm64

DuckDB Package Version:

1.4.4 and the current main branch

Python Version:

3.14

Full Name:

John Marshall

Affiliation:

Centre for Population Genomics

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

Did you include all relevant data sets for reproducing the issue?

Yes

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant configuration to reproduce the issue?

  • Yes, I have

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