arduino/Arduino · error · TypeError

invalid keyword arguments to print()

Error message

invalid keyword arguments to print()

What it means

six.print_ only accepts the sep and end keyword arguments (matching Python 2's print statement emulation); after popping sep and end, any leftover entries in **kwargs trigger TypeError('invalid keyword arguments to print()'). The built-in print() additionally supports file and flush, which this emulation does not.

Source

Thrown at arduino-core/src/processing/app/i18n/python/requests/packages/urllib3/packages/six.py:358

        def write(data):
            if not isinstance(data, basestring):
                data = str(data)
            fp.write(data)
        want_unicode = False
        sep = kwargs.pop("sep", None)
        if sep is not None:
            if isinstance(sep, unicode):
                want_unicode = True
            elif not isinstance(sep, str):
                raise TypeError("sep must be None or a string")
        end = kwargs.pop("end", None)
        if end is not None:
            if isinstance(end, unicode):
                want_unicode = True
            elif not isinstance(end, str):
                raise TypeError("end must be None or a string")
        if kwargs:
            raise TypeError("invalid keyword arguments to print()")
        if not want_unicode:
            for arg in args:
                if isinstance(arg, unicode):
                    want_unicode = True
                    break
        if want_unicode:
            newline = unicode("\n")
            space = unicode(" ")
        else:
            newline = "\n"
            space = " "
        if sep is None:
            sep = space
        if end is None:
            end = newline
        for i, arg in enumerate(args):
            if i:
                write(sep)

View on GitHub (pinned to a0df6e0e83)

Solutions

  1. Remove unsupported kwargs (file, flush) from the call.
  2. For stderr/file output, use sys.stderr.write('...\n') or print >> sys.stderr on Python 2.
  3. Use six.print_('x') followed by sys.stdout.flush() instead of flush=True.
  4. Filter kwargs: six.print_(*args, **{k: v for k, v in kw.items() if k in ('sep', 'end')}).

Example fix

// before
six.print_('error!', file=sys.stderr)
// after
sys.stderr.write('error!\n')
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = ('sep', 'end')
bad = [k for k in kwargs if k not in ALLOWED]
if bad:
    raise TypeError('unsupported kwargs for six.print_: %s' % bad)

Try / catch

try:
    six.print_(*args, **kwargs)
except TypeError as e:
    if 'invalid keyword arguments to print()' in str(e):
        six.print_(*args, **{k: v for k, v in kwargs.items() if k in ('sep', 'end')})
    else:
        raise

Prevention

When it happens

Trigger: six.print_('x', file=sys.stderr); six.print_('x', flush=True); passing **some_kwargs that contain keys other than sep/end.

Common situations: Mechanical Python 3 print() to six.print_() conversion without dropping file=/flush=; forwarding unknown kwargs dictionaries; environments (Python 2 era) where file redirection must use sys.stdout.write instead.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


AI-assisted analysis of arduino/Arduino@a0df6e0e83 (2026-09-06). Data as JSON: /api/errors/dbaa30a981985849. Report an issue: GitHub.