Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex pattern bug in _default_cast #9

Closed
mpertierra opened this issue Mar 18, 2017 · 1 comment
Closed

Regex pattern bug in _default_cast #9

mpertierra opened this issue Mar 18, 2017 · 1 comment

Comments

@mpertierra
Copy link

mpertierra commented Mar 18, 2017

@goodmami The code for the function is shown below.

def _default_cast(x):
    if isinstance(x, basestring):
        if x.startswith('"'):
            x = x  # strip quotes?
        elif re.match(
                r'-?(0|[1-9]\d*)(\.\d+[eE][-+]?|\.|[eE][-+]?)\d+', x):
            x = float(x)
        elif re.match(r'-?\d+', x):
            x = int(x)
    return x

Here are some examples that cause unexpected behavior.

_default_cast("123a") # expected: "123a", actual: ValueError from int()
_default_cast("-3.14z") # expected: "-3.14z", actual: ValueError from float()

The problem arises from using regex patterns without ^ and $. Adding these characters as shown below fixes the problems.

def _default_cast(x):
    if isinstance(x, basestring):
        if x.startswith('"'):
            x = x  # strip quotes?
        elif re.match(
                r'^-?(0|[1-9]\d*)(\.\d+[eE][-+]?|\.|[eE][-+]?)\d+$', x):
            x = float(x)
        elif re.match(r'^-?\d+$', x):
            x = int(x)
    return x

Another alternative would be not to use regex at all here, and use try-except blocks instead, as shown below.

def _default_cast(x):
    if isinstance(x, basestring):
        try:
            x = int(x)
        except ValueError:
            try:
                x = float(x)
            except ValueError:
                pass
    return x
@goodmami
Copy link
Owner

Thanks! I've confirmed the problem.

Since I'm using re.match() and not re.search(), the initial ^ anchor is unnecessary, but you're right that the final $ is important.

I added some unit tests to model the issue, then tried both of your solutions. They're both equally capable, but when I benchmarked them the latter solution is a bit slower (except when the value is an integer), so I think I'll go with the first one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants