pydantic/monty · error · TypeError

update modes ('+') are not yet supported

Error message

update modes ('+') are not yet supported

What it means

The mode string contained '+', requesting read/update (e.g. 'r+', 'w+'). Update modes are not yet implemented in the JS bindings' file handling, so they are rejected eagerly with a TypeError rather than silently misbehaving.

Source

Thrown at crates/monty-js/ts/types.ts:141

  }

  let action: string | undefined
  let binary = false
  let text = false
  for (const char of mode) {
    if (char === 'r' || char === 'w' || char === 'a') {
      if (action !== undefined) throw new TypeError('must have exactly one of create/read/write/append mode')
      action = char
    } else if (char === 'x') {
      throw new TypeError('exclusive creation mode is not supported')
    } else if (char === 'b') {
      if (binary) throw new TypeError('invalid mode: binary mode specified twice')
      binary = true
    } else if (char === 't') {
      if (text) throw new TypeError('invalid mode: text mode specified twice')
      text = true
    } else if (char === '+') {
      throw new TypeError("update modes ('+') are not yet supported")
    } else {
      throw new TypeError(`invalid mode: '${char}'`)
    }
  }
  if (binary && text) throw new TypeError("can't have text and binary mode at once")
  if (action === undefined) {
    throw new TypeError('Must have exactly one of create/read/write/append mode and at most one plus')
  }
  return `${action}${binary ? 'b' : ''}`
}

/** Validates that a file position can cross the JavaScript boundary exactly. */
export function validateFilePosition(position: unknown): asserts position is number {
  if (typeof position !== 'number' || !Number.isSafeInteger(position) || position < 0) {
    throw new TypeError('MontyFileHandle position must be a non-negative safe integer')
  }
}

View on GitHub (pinned to adc986b362)

Solutions

  1. Split into two operations: read the file (mode 'r'), then rewrite it (mode 'w' or 'a')
  2. If read-modify-write is needed, read full content, modify in memory, and write back with 'w'
  3. Use append ('a') if you only need to add to the file

Example fix

// before
const fh = new MontyFileHandle(path, 'r+')
// after
const data = new MontyFileHandle(path, 'r').read()
new MontyFileHandle(path, 'w').write(transform(data))
Defensive patterns

Strategy: validation

Validate before calling

if (typeof mode === 'string' && mode.includes('+')) throw new Error('update modes not supported by monty-js file handles')

Type guard

function isNonUpdateMode(mode) { return typeof mode === 'string' && /^[rwa][bt]?$/.test(mode) }

Try / catch

try { fh = new MontyFileHandle(path, mode) } catch (e) { if (e instanceof TypeError && e.message.includes("not yet supported")) { /* fall back to read-then-write */ } else throw e }

Prevention

When it happens

Trigger: Calling MontyFileHandle's constructor or pushFileHandle with any mode containing '+': 'r+', 'w+', 'a+', 'rb+', etc.

Common situations: Porting Python code that opens files read-write with 'r+' to patch bytes in place; developers expecting seek-and-write on an open handle.

Related errors


AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13). Data as JSON: /api/errors/382df5b7f22a89a7. Report an issue: GitHub.