pydantic/monty · error · TypeError

exclusive creation mode is not supported

Error message

exclusive creation mode is not supported

What it means

The file mode string passed to MontyFileHandle/open contained 'x', requesting exclusive creation (O_CREAT|O_EXCL). The JS bindings' file-mode subset does not implement exclusive creation, so canonicalFileMode rejects it with a TypeError before any host-side open happens.

Source

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

    )
  }
}

/** Canonicalizes the subset of Python file modes Monty supports. */
export function canonicalFileMode(mode: string): string {
  if (mode.length === 0) {
    throw new TypeError('Must have exactly one of create/read/write/append mode and at most one plus')
  }

  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' : ''}`
}

View on GitHub (pinned to adc986b362)

Solutions

  1. Remove 'x' from the mode string and use 'w', 'r', or 'a' instead
  2. Implement the existence check yourself: try opening/reading the file first and treat a file-not-found OsFunctionCall outcome as 'safe to create'
  3. Wrap the open in a try/catch for TypeError and fall back to a non-exclusive mode

Example fix

// before
const fh = new MontyFileHandle('/mnt/data/lock', 'x')
// after
const fh = new MontyFileHandle('/mnt/data/lock', 'w')
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { fh = new MontyFileHandle(path, mode) } catch (e) { if (e instanceof TypeError && /exclusive creation/.test(e.message)) fh = new MontyFileHandle(path, mode.replace('x', 'w')) ; else throw e }

Prevention

When it happens

Trigger: Calling MontyFileHandle's constructor or pushFileHandle with a mode string containing 'x', e.g. `new MontyFileHandle(path, 'x')`, `'wx'`, `'xb'`, or `'ax'`.

Common situations: Porting code that used Python's open(path, 'x') for create-if-not-exists semantics, or Node's fs open flag 'wx'; developers who want atomic file creation to avoid clobbering an existing file.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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