Skip to content

Commit

Permalink
Enhance regexp about reference before assignment #20
Browse files Browse the repository at this point in the history
I stumbled upon an unusual NameError and thought I'd
wrote it down for later.

This test will most likely fail with other intepreters.
  • Loading branch information
SylvainDe committed Feb 27, 2016
1 parent a2d4e72 commit b8e7505
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion didyoumean/didyoumean_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def suggest_if_file_is_dir(value):


NAMEERRORS = {
re.UNBOUNDERROR_RE: suggest_name_not_defined,
re.VARREFBEFOREASSIGN_RE: suggest_name_not_defined,
re.NAMENOTDEFINED_RE: suggest_name_not_defined,
}

Expand Down
4 changes: 2 additions & 2 deletions didyoumean/didyoumean_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import re


UNBOUNDERROR_RE = r"^local variable '(?P<name>\w+)' " \
r"referenced before assignment$"
VARREFBEFOREASSIGN_RE = r"^(?:local|free) variable '(?P<name>\w+)' " \
r"referenced before assignment(?: in enclosing scope)?$"
NAMENOTDEFINED_RE = r"^(?:global )?name '(?P<name>\w+)' is not defined$"
ATTRIBUTEERROR_RE = r"^(?:class |type object )?'?([\w\.]+)'? " \
r"(?:object |instance )?has no attribute '(\w+)'$"
Expand Down
12 changes: 8 additions & 4 deletions didyoumean/didyoumean_re_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def re_matches(self, text, regexp, results):
self.assertEqual(named_groups, match.groupdict())

def test_unbound_assignment(self):
"""Test UNBOUNDERROR_RE."""
# Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
msg = "local variable 'some_var' referenced before assignment"
"""Test VARREFBEFOREASSIGN_RE."""
msgs = [
# Python 2.6/2.7/3.2/3.3/3.4/3.5/PyPy/PyPy3
"local variable 'some_var' referenced before assignment",
"free variable 'some_var' referenced before assignment in enclosing scope",
]
groups = ('some_var',)
named_groups = {'name': 'some_var'}
self.re_matches(msg, re.UNBOUNDERROR_RE, (groups, named_groups))
for msg in msgs:
self.re_matches(msg, re.VARREFBEFOREASSIGN_RE, (groups, named_groups))

def test_name_not_defined(self):
"""Test NAMENOTDEFINED_RE."""
Expand Down
8 changes: 7 additions & 1 deletion didyoumean/didyoumean_sugg_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ def get_exception(code):

# NameError for NameErrorTests
NAMEERROR = (NameError, re.NAMENOTDEFINED_RE)
NAMEERRORBEFOREREF = (NameError, re.VARREFBEFOREASSIGN_RE)
UNKNOWN_NAMEERROR = (NameError, None)
# UnboundLocalError for UnboundLocalErrorTests
UNBOUNDLOCAL = (UnboundLocalError, re.UNBOUNDERROR_RE)
UNBOUNDLOCAL = (UnboundLocalError, re.VARREFBEFOREASSIGN_RE)
UNKNOWN_UNBOUNDLOCAL = (UnboundLocalError, None)
# TypeError for TypeErrorTests
NBARGERROR = (TypeError, re.NB_ARG_RE)
Expand Down Expand Up @@ -378,6 +379,11 @@ def test_no_sugg(self):
"""No suggestion."""
self.throws('a = ldkjhfnvdlkjhvgfdhgf', NAMEERROR)

def test_free_var_before_assignment(self):
"""No suggestion but different error message."""
code = 'def f():\n\tdef g():\n\t\treturn free_var\n\tg()\n\tfree_var = 0\nf()'
self.throws(code, NAMEERRORBEFOREREF)

# For added/removed names, following functions with one name
# per functions were added in the early stages of the project.
# In the future, I'd like to have them replaced by something
Expand Down

0 comments on commit b8e7505

Please sign in to comment.