Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Fix #16, avoid false positive when bad builtin overwritten by import
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwager committed Aug 16, 2019
1 parent 3101c06 commit 70e0c8b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- False positive when bad builtin is overwritten by import (#16)

## [0.6.0] - 2019-08-12
### Added
Expand Down
12 changes: 12 additions & 0 deletions dlint/linters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import ast
import collections

from .. import namespace

Flake8Result = collections.namedtuple(
'Flake8Result',
['lineno', 'col_offset', 'message']
Expand All @@ -19,9 +21,19 @@
class BaseLinter(ast.NodeVisitor):
def __init__(self, *args, **kwargs):
self.results = []
self.namespace = None

super(BaseLinter, self).__init__(*args, **kwargs)

def get_results(self):

return self.results

def visit(self, node):
if not self.namespace:
# Assuming that 'visit' is always called the first time with 'node'
# as an ast.Module. If not, we should fail fast with a raised
# exception and investigate.
self.namespace = namespace.Namespace.from_module_node(node)

super(BaseLinter, self).visit(node)
3 changes: 2 additions & 1 deletion dlint/linters/helpers/bad_builtin_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def illegal_builtin(self):
"""

def visit_Name(self, node):
if node.id == self.illegal_builtin:
if (node.id == self.illegal_builtin
and not self.namespace.name_imported(node.id)):
self.results.append(
base.Flake8Result(
lineno=node.lineno,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_helpers/test_bad_builtin_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ def test_bad_builtin_usage(self):

assert result == expected

def test_bad_builtin_overwritten(self):
python_string = self.get_ast_node(
"""
from foo import bar
result = bar()
"""
)

linter = get_builtin_use_implementation('bar')
linter.visit(python_string)

result = linter.get_results()
expected = []

assert result == expected

def test_no_builtin_usage(self):
python_string = self.get_ast_node(
"""
Expand Down

0 comments on commit 70e0c8b

Please sign in to comment.