pola-rs/polars · error

no read method found

Error message

no read method found

What it means

Internal expect() in PyFileLikeObject::to_buffer: calling `read()` on the wrapped Python file-like object raised an AttributeError, i.e. the object passed as a file does not implement the read method required by the file-like protocol.

Solutions

  1. Pass a file-like object with a `read` method (e.g. an opened binary file or BytesIO).
  2. Wrap the object so it exposes a read() interface before passing it to polars.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: This panic/expect fires when execution reaches an unguarded state described by: "no read method found". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.

Common situations: Encountered when input data or configuration does not meet the preconditions of the throwing code path ("no read method found"). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.


AI-assisted analysis of pola-rs/polars@5d8ebabf11 (2026-08-19). Data as JSON: /api/errors/7e7f1a6f57c2481a. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-python/src/file.rs:83

        object: Py<PyAny>,
        expects_str: bool,
        has_flush: bool,
        start_offset: u64,
    ) -> Self {
        PyFileLikeObject {
            inner: object,
            expects_str,
            has_flush,
            start_offset,
        }
    }

    pub(crate) fn to_buffer(&self) -> Buffer<u8> {
        Python::attach(|py| {
            let bytes = self
                .inner
                .call_method(py, "read", (), None)
                .expect("no read method found");

            if let Ok(b) = bytes.cast_bound::<PyBytes>(py) {
                // SAFETY: we keep the underlying python object alive.
                let slice = b.as_bytes();
                let owner = bytes.clone_ref(py);
                let ss = unsafe { SharedStorage::from_slice_with_owner(slice, owner) };
                return Buffer::from_storage(ss);
            }

            if let Ok(b) = bytes.cast_bound::<PyString>(py) {
                return match b.to_cow().expect("PyString is not valid UTF-8") {
                    Cow::Borrowed(v) => {
                        // SAFETY: we keep the underlying python object alive.
                        let slice = v.as_bytes();
                        let owner = bytes.clone_ref(py);
                        let ss = unsafe { SharedStorage::from_slice_with_owner(slice, owner) };
                        return Buffer::from_storage(ss);
                    },

View on GitHub (pinned to 5d8ebabf11)