Skip to content

PYTHON-2883 Regex decoding error tests in top.json have unexpected, invalid syntax #721

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

Merged
merged 6 commits into from
Sep 10, 2021

Conversation

juliusgeo
Copy link
Contributor

@@ -634,6 +634,8 @@ def _parse_canonical_code(doc):
def _parse_canonical_regex(doc):
"""Decode a JSON regex to bson.regex.Regex."""
regex = doc['$regularExpression']
if not isinstance(regex['options'], str):
raise TypeError('Bad $regularExpression options, options must be string')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this check to below the len(doc) and len(regex) checks to ensure we give good error messages in all cases. For the same reason can you also add the type() of regex['options'] to the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -639,6 +639,8 @@ def _parse_canonical_regex(doc):
if len(regex) != 2:
raise TypeError('Bad $regularExpression must include only "pattern"'
'and "options" components: %s' % (doc,))
if not isinstance(regex['options'], str):
raise TypeError('Bad $regularExpression options, options must be string, was type %s' % (type(regex['options'])))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We limit our lines to 79 chars according to pep 8: https://www.python.org/dev/peps/pep-0008/#maximum-line-length

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment

@@ -639,6 +639,9 @@ def _parse_canonical_regex(doc):
if len(regex) != 2:
raise TypeError('Bad $regularExpression must include only "pattern"'
'and "options" components: %s' % (doc,))
if not isinstance(regex['options'], str):
raise TypeError('Bad $regularExpression options, options must be '
'string, was type %s' % (type(regex['options'])))
return Regex(regex['pattern'], regex['options'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest refactoring to regex['options'] to a local variable to avoid looking it up repeatedly and improve readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@juliusgeo juliusgeo merged commit 7a4b617 into mongodb:master Sep 10, 2021
juliusgeo added a commit to juliusgeo/mongo-python-driver that referenced this pull request Oct 1, 2021
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

Successfully merging this pull request may close these issues.

2 participants