Skip to content

Commit

Permalink
Resolve lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Oct 14, 2024
1 parent dec545d commit 9c9266e
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 82 deletions.
5 changes: 3 additions & 2 deletions src/python_minifier/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from python_minifier import minify
from python_minifier.transforms.remove_annotations_options import RemoveAnnotationsOptions


if sys.version_info >= (3, 8):
from importlib import metadata

Expand Down Expand Up @@ -270,9 +271,9 @@ def error(os_error):

for path_arg in args.path:
if os.path.isdir(path_arg):
for root, dirs, files in os.walk(path_arg, onerror=error, followlinks=True):
for root, _dirs, files in os.walk(path_arg, onerror=error, followlinks=True):
for file in files:
if file.endswith('.py') or file.endswith('.pyw'):
if file.endswith(('.py', '.pyw')):
yield os.path.join(root, file)
else:
yield path_arg
Expand Down
1 change: 1 addition & 0 deletions src/python_minifier/ast_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ast import *


# Ideally we don't import anything else

if 'TypeAlias' in globals():
Expand Down
1 change: 1 addition & 0 deletions src/python_minifier/ast_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from python_minifier.util import is_ast_node


INDENT = ' '

# The field name that can be omitted for each node
Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/expression_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ def visit_arguments(self, node):
def visit_arg(self, node):
if isinstance(node, ast.Name):
# Python 2 uses Name nodes
return self.visit_Name(node)
self.visit_Name(node)
return

self.printer.identifier(node.arg)

Expand Down
11 changes: 5 additions & 6 deletions src/python_minifier/f_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def is_correct_ast(self, code):
c = ast.parse(code, 'FString candidate', mode='eval')
compare_ast(self.node, c.body)
return True
except Exception as e:
except Exception:
return False

def complete_debug_specifier(self, partial_specifier_candidates, value_node):
Expand Down Expand Up @@ -79,12 +79,12 @@ def candidates(self):
# Maybe!
try:
debug_specifier_candidates = [x + '{' + v.s for x in candidates]
except Exception as e:
except Exception:
continue

try:
candidates = [x + self.str_for(v.s, quote) for x in candidates]
except Exception as e:
except Exception:
continue
elif isinstance(v, ast.FormattedValue):
try:
Expand All @@ -93,15 +93,14 @@ def candidates(self):
x + y for x in candidates for y in FormattedValue(v, nested_allowed, self.pep701).get_candidates()
] + completed
debug_specifier_candidates = []
except Exception as e:
except Exception:
continue
else:
raise RuntimeError('Unexpected JoinedStr value')

actual_candidates += ['f' + quote + x + quote for x in candidates]

actual_candidates = filter(self.is_correct_ast, actual_candidates)
return actual_candidates
return filter(self.is_correct_ast, actual_candidates)

def str_for(self, s, quote):
return s.replace('{', '{{').replace('}', '}}')
Expand Down
6 changes: 3 additions & 3 deletions src/python_minifier/ministring.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __str__(self):

try:
eval(self.quote + s + self.quote)
except (UnicodeDecodeError, UnicodeEncodeError) as e:
except (UnicodeDecodeError, UnicodeEncodeError):
if self.safe_mode:
raise

Expand Down Expand Up @@ -63,7 +63,7 @@ def to_short(self):
}

for c in self._s:
if c in escaped.keys():
if c in escaped:
s += escaped[c]
else:
if self.safe_mode:
Expand Down Expand Up @@ -95,7 +95,7 @@ def to_long(self):
}

for c in self._s:
if c in escaped.keys():
if c in escaped:
s += escaped[c]
else:
if self.safe_mode:
Expand Down
2 changes: 1 addition & 1 deletion src/python_minifier/module_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def visit_ImportFrom(self, node):
assert isinstance(node, ast.ImportFrom)

self.printer.keyword('from')
for i in range(node.level):
for _i in range(node.level):
self.printer.delimiter('.')
if node.module is not None:
self.printer.identifier(node.module)
Expand Down
4 changes: 2 additions & 2 deletions src/python_minifier/rename/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def should_rename(self, new_name):
"""

raise NotImplementedError()
raise NotImplementedError

def rename(self, new_name):
"""
Expand All @@ -283,7 +283,7 @@ def rename(self, new_name):
"""

raise NotImplementedError()
raise NotImplementedError


class NameBinding(Binding):
Expand Down
11 changes: 5 additions & 6 deletions src/python_minifier/rename/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ def add_parent(node, parent=None, namespace=None):
if is_ast_node(node, 'Nonlocal'):
namespace.nonlocal_names.update(node.names)

if isinstance(node, ast.Name):
if isinstance(namespace, ast.ClassDef):
if isinstance(node.ctx, ast.Load):
namespace.nonlocal_names.add(node.id)
elif isinstance(node.ctx, ast.Store) and isinstance(node.parent, ast.AugAssign):
namespace.nonlocal_names.add(node.id)
if isinstance(node, ast.Name) and isinstance(namespace, ast.ClassDef):
if isinstance(node.ctx, ast.Load):
namespace.nonlocal_names.add(node.id)
elif isinstance(node.ctx, ast.Store) and isinstance(node.parent, ast.AugAssign):
namespace.nonlocal_names.add(node.id)

for child in ast.iter_child_nodes(node):
add_parent(child, parent=node, namespace=namespace)
Expand Down
11 changes: 5 additions & 6 deletions src/python_minifier/rename/rename_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,18 @@ def namespace_path(self, node):
"""

l = []
path = []

while True:
namespace = self.nearest_function_namespace(node)
l.insert(0, namespace)
path.insert(0, namespace)

if isinstance(namespace, ast.Module):
break

node = namespace

return l
return path

def common_path(self, n1_path, n2_path):

Expand Down Expand Up @@ -217,8 +217,7 @@ def visit_JoinedStr(self, node):
if is_ast_node(v, ast.Str):
# Can't hoist this!
continue
else:
self.visit(v)
self.visit(v)

def visit_NameConstant(self, node):
self.get_binding(node.value, node).add_reference(node)
Expand All @@ -242,7 +241,7 @@ def visit_Assign(self, node):
for target in node.targets:
if is_ast_node(target, ast.Name) and target.id == '__slots__':
# This is a __slots__ assignment, don't hoist the literals
return
return None

return self.generic_visit(node)

Expand Down
56 changes: 28 additions & 28 deletions src/python_minifier/rename/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reservation_scope(namespace, binding):
"""

namespaces = set([namespace])
namespaces = {namespace}

for node in binding.references:
while node is not namespace:
Expand Down Expand Up @@ -112,7 +112,7 @@ def available_name(self):
def __call__(self, module):
assert isinstance(module, ast.Module)

for namespace, binding in sorted_bindings(module):
for _namespace, binding in sorted_bindings(module):
if binding.allow_rename:
binding.new_name = self.available_name()

Expand Down Expand Up @@ -154,6 +154,8 @@ def available_name(self, reservation_scope, prefix=''):
if self.is_available(prefix + name, reservation_scope):
return prefix + name

return None

def is_available(self, name, reservation_scope):
"""
Is a name unreserved in a reservation scope
Expand All @@ -165,11 +167,7 @@ def is_available(self, name, reservation_scope):
"""

for namespace in reservation_scope:
if name in namespace.assigned_names:
return False

return True
return all(name not in namespace.assigned_names for namespace in reservation_scope)

def __call__(self, module, prefix_globals, reserved_globals=None):
assert isinstance(module, ast.Module)
Expand All @@ -184,6 +182,26 @@ def __call__(self, module, prefix_globals, reserved_globals=None):
for name in reserved_globals:
module.assigned_names.add(name)

def should_rename(binding, name, scope):
if binding.should_rename(name):
return True

# It's no longer efficient to do this rename

if isinstance(binding, NameBinding):
# Check that the original name is still available

if binding.reserved == binding.name:
# We already reserved it (this is probably an arg)
return False

if not self.is_available(binding.name, scope):
# The original name has already been assigned to another binding,
# so we need to rename this anyway.
return True

return False

for namespace, binding in sorted_bindings(module):
scope = reservation_scope(namespace, binding)

Expand All @@ -194,27 +212,7 @@ def __call__(self, module, prefix_globals, reserved_globals=None):
else:
name = self.available_name(scope)

def should_rename():
if binding.should_rename(name):
return True

# It's no longer efficient to do this rename

if isinstance(binding, NameBinding):
# Check that the original name is still available

if binding.reserved == binding.name:
# We already reserved it (this is probably an arg)
return False

if not self.is_available(binding.name, scope):
# The original name has already been assigned to another binding,
# so we need to rename this anyway.
return True

return False

if should_rename():
if should_rename(binding, name, scope):
binding.rename(name)
else:
# Any existing name will become reserved
Expand All @@ -228,3 +226,5 @@ def should_rename():

def rename(module, prefix_globals=False, preserved_globals=None):
NameAssigner()(module, prefix_globals, preserved_globals)


8 changes: 4 additions & 4 deletions src/python_minifier/transforms/constant_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def visit_BinOp(self, node):
try:
folded_expression = unparse_expression(new_node)
folded_value = safe_eval(folded_expression)
except Exception as e:
except Exception:
# This can happen if the value is too large to be represented as a literal
# or if the value is unparsed as nan, inf or -inf - which are not valid python literals
return node
Expand Down Expand Up @@ -107,11 +107,11 @@ def equal_value_and_type(a, b):


def safe_eval(expression):
globals = {}
locals = {}
empty_globals = {}
empty_locals = {}

# This will return the value, or could raise an exception
return eval(expression, globals, locals)
return eval(expression, empty_globals, empty_locals)


def unparse_expression(node):
Expand Down
16 changes: 8 additions & 8 deletions src/python_minifier/transforms/remove_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def is_dataclass_field(node):
if len(node.parent.decorator_list) == 0:
return False

for node in node.parent.decorator_list:
if isinstance(node, ast.Name) and node.id == 'dataclass':
for decorator_node in node.parent.decorator_list:
if isinstance(decorator_node, ast.Name) and decorator_node.id == 'dataclass':
return True
elif isinstance(node, ast.Attribute) and node.attr == 'dataclass':
elif isinstance(decorator_node, ast.Attribute) and decorator_node.attr == 'dataclass':
return True
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'dataclass':
elif isinstance(decorator_node, ast.Call) and isinstance(decorator_node.func, ast.Name) and decorator_node.func.id == 'dataclass':
return True
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == 'dataclass':
elif isinstance(decorator_node, ast.Call) and isinstance(decorator_node.func, ast.Attribute) and decorator_node.func.attr == 'dataclass':
return True

return False
Expand All @@ -102,10 +102,10 @@ def is_typing_sensitive(node):

tricky_types = ['NamedTuple', 'TypedDict']

for node in node.parent.bases:
if isinstance(node, ast.Name) and node.id in tricky_types:
for base_node in node.parent.bases:
if isinstance(base_node, ast.Name) and base_node.id in tricky_types:
return True
elif isinstance(node, ast.Attribute) and node.attr in tricky_types:
elif isinstance(base_node, ast.Attribute) and base_node.attr in tricky_types:
return True

return False
Expand Down
2 changes: 0 additions & 2 deletions src/python_minifier/transforms/remove_annotations_options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class RemoveAnnotationsOptions:
):
...

def __repr__(self) -> str: ...

def __nonzero__(self) -> bool: ...

def __bool__(self) -> bool: ...
4 changes: 3 additions & 1 deletion src/python_minifier/transforms/remove_exception_brackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from python_minifier.rename.binding import BuiltinBinding


# These are always exceptions, in every version of python
builtin_exceptions = [
'SyntaxError', 'Exception', 'ValueError', 'BaseException', 'MemoryError', 'RuntimeError', 'DeprecationWarning', 'UnicodeEncodeError', 'KeyError', 'LookupError', 'TypeError', 'BufferError',
Expand Down Expand Up @@ -80,7 +81,8 @@ def _remove_empty_call(binding):

for name_node in binding.references:
# For this to be a builtin, all references must be name nodes as it is not defined anywhere
assert isinstance(name_node, ast.Name) and isinstance(name_node.ctx, ast.Load)
assert isinstance(name_node, ast.Name)
assert isinstance(name_node.ctx, ast.Load)

if not isinstance(name_node.parent, ast.Call):
# This is not a call
Expand Down
Loading

0 comments on commit 9c9266e

Please sign in to comment.