parse icon indicating copy to clipboard operation
parse copied to clipboard

Support matching empty strings for str-types

Open Alex-Muirhead opened this issue 1 month ago • 3 comments

This will close #138, and since there wasn't much discussion there I understand if this feature is ultimately not desired, but I thought I'd have a go at fixing it anyway.

When matching a format that will return a str (i.e. format specifiers l,w,W,s,S,D or no format specifier), it seems valid that an empty string "" can be matched. The same doesn't seem true for other formats, as they have no "empty" equivalent (other than None, which is already returned in these cases).

This also fits the (inverse) behaviour of .format(), which allows empty strings to be passed into the arguments an subsequently formatted.

name = ""
text = "Hello {}!".format(name)  # 'Hello !'
parse("Hello {}!", text)  # Previously returned None, now returns <Result ('',) {}>

I've run & updated the relevant tests, but I apologise in advance if I've missed anything!

Alex-Muirhead avatar Nov 19 '25 04:11 Alex-Muirhead

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 94.67%. Comparing base (d5bd49a) to head (a13e875). :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #217   +/-   ##
=======================================
  Coverage   94.67%   94.67%           
=======================================
  Files           1        1           
  Lines         545      545           
  Branches      128      128           
=======================================
  Hits          516      516           
  Misses         19       19           
  Partials       10       10           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 19 '25 05:11 codecov-commenter

This will definitely be backwards-incompatible: it would change behavior of existing code that checks if a parse result is None. That is not to say that it can't go in, just that it would require an explicit README / changelog mention and, likely, a major version number increment.

wimglenn avatar Nov 22 '25 20:11 wimglenn

That's definitely understandable, thank you for looking through it either way!

Another potential approach I tried (that might be better) was to use "{:0}" to specify zero-width minimum for the string. However the current implementation lets this fall through, and requires "{:00}" to get the zero-width string (which I believe isn't intended, given you can specify any other minimum length without the leading zero).

I'm happy to have a go at fixing that instead, if that approach is more amenable.

Alex-Muirhead avatar Nov 25 '25 10:11 Alex-Muirhead