Raise over unrecognised kwargs, to help protect against typos?

Create issue
Issue #354 resolved
Mark Amery created an issue

Currently, all the public functions in this module take arbitrary **kwargs in order to support “Named lists” and won't raise an error if they receive keyword arguments they don’t need. A distasteful side effect of this is that if I forget, for example, whether I’m supposed to pass a flag or flags keyword argument to set flags (a mistake I’ve personally made multiple times), I get a silent failure; my argument is simply ignored and I get no clue what I did wrong:

>>> regex.findall('foo', 'FOOD', flag=regex.IGNORECASE)

To fix this, I suggest tweaking regex._compile to raise an exception if a kwarg is supplied that isn’t needed. I suppose this would be a slight backwards-compatibility break (since maybe some people were intentionally passing a dictionary of kwargs where only a subset of the keys were needed by any particular regex) but it seems worth it to me; my instinct is that many people have been caught out by the lack of typo resistance in the current API and will be again, while few if any are using kwargs in a way that this would break.

Thoughts? If you’d accept this in principle, I’ll put together a PR.

Comments (7)

  1. Matthew Barnett repo owner

    I’m a little undecided over this because I wasn’t planning on any more additions, but this isn’t really an addition. Also, should it be added for Python 2 too, given that Python 2 has already reached EOL?

  2. Mark Amery reporter

    I’d do it for Python 2 too; if we’re gonna change it, seems like we might as well keep it consistent between the two versions? Makes it less confusing for future readers of this codebase, at least!

  3. Mark Amery reporter

    I’ll wait for you to decide whether you approve of this, then implement it if you do.

  4. Log in to comment