Skip to content

Commit

Permalink
Sort imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Oct 10, 2024
1 parent 0ada486 commit 6e80593
Show file tree
Hide file tree
Showing 61 changed files with 191 additions and 97 deletions.
Empty file added corpus_test/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion corpus_test/generate_report.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import argparse
import os
import sys

from dataclasses import dataclass, field
from typing import Iterable

from result import Result, ResultReader
from .result import Result, ResultReader

ENHANCED_REPORT = os.environ.get('ENHANCED_REPORT', True)

Expand Down
8 changes: 3 additions & 5 deletions corpus_test/generate_results.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import argparse
import datetime
import gzip
import logging
import os
import sys
import time


import logging


import python_minifier
from result import Result, ResultWriter

from .result import Result, ResultWriter

try:
RE = RecursionError
Expand Down
Empty file added hypo_test/__init__.py
Empty file.
18 changes: 17 additions & 1 deletion hypo_test/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
import unicodedata

from hypothesis import assume
from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries, booleans, SearchStrategy, text, composite, one_of, floats, complex_numbers, characters, none
from hypothesis.strategies import (
SearchStrategy,
binary,
booleans,
characters,
complex_numbers,
composite,
dictionaries,
floats,
integers,
lists,
none,
one_of,
recursive,
sampled_from,
text
)

comparison_operators = sampled_from([
ast.Eq(),
Expand Down
6 changes: 3 additions & 3 deletions hypo_test/folding.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from hypothesis import assume
from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries, booleans, SearchStrategy, text, composite, one_of, floats, complex_numbers, characters, none
import ast

from expressions import NameConstant, Num
from hypothesis.strategies import SearchStrategy, composite, lists, recursive, sampled_from

from .expressions import NameConstant, Num

leaves = NameConstant() | Num()

Expand Down
14 changes: 7 additions & 7 deletions hypo_test/module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ast

from hypothesis import assume
from hypothesis.strategies import integers, lists, sampled_from, recursive, none, booleans, SearchStrategy, composite, one_of
from hypothesis.strategies import SearchStrategy, booleans, composite, integers, lists, none, one_of, recursive, sampled_from

from .expressions import Name, arguments, expression, name

from hypo_test.expressions import Name, expression, name
from expressions import arguments

@composite
def Assign(draw) -> ast.Assign:
Expand Down Expand Up @@ -193,7 +193,7 @@ def FunctionDef(draw, statements) -> ast.FunctionDef:
args = draw(arguments())
body = draw(lists(statements, min_size=1, max_size=3))
decorator_list = draw(lists(Name(), min_size=0, max_size=2))
type_params = draw(none() | lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
type_params = draw(lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
returns = draw(none() | expression())
return ast.FunctionDef(n, args, body, decorator_list, returns, type_params=type_params)

Expand All @@ -203,7 +203,7 @@ def AsyncFunctionDef(draw, statements) -> ast.AsyncFunctionDef:
args = draw(arguments())
body = draw(lists(statements, min_size=1, max_size=3))
decorator_list = draw(lists(Name(), min_size=0, max_size=2))
type_params = draw(none() | lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
type_params = draw(lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
returns = draw(none() | expression())
return ast.AsyncFunctionDef(n, args, body, decorator_list, returns, type_params=type_params)

Expand All @@ -230,7 +230,7 @@ def ClassDef(draw, statements) -> ast.ClassDef:
keywords=keywords,
body=body,
decorator_list=decorator_list,
type_params=draw(none() | lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
type_params=draw(lists(one_of(TypeVar(), TypeVarTuple(), ParamSpec()), min_size=0, max_size=3))
)

if hasattr(ast, 'Print'):
Expand Down Expand Up @@ -286,7 +286,7 @@ def suite() -> SearchStrategy:
ClassDef(statements),
Try(statements)
),
max_leaves=150
max_leaves=100
)

@composite
Expand Down
3 changes: 2 additions & 1 deletion hypo_test/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import string

from hypothesis import assume
from hypothesis.strategies import lists, sampled_from, recursive, text, composite, one_of, none, booleans, integers
from hypothesis.strategies import booleans, composite, integers, lists, none, one_of, recursive, sampled_from, text


@composite
def name(draw):
Expand Down
22 changes: 12 additions & 10 deletions hypo_test/test_it.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import ast

from datetime import timedelta
from hypothesis import given, settings, Verbosity, note, HealthCheck

from folding import FoldableExpression
from patterns import Pattern
from python_minifier import ModulePrinter
from hypothesis import HealthCheck, Verbosity, given, note, settings

from python_minifier.ast_compare import compare_ast
from python_minifier.ast_printer import print_ast
from python_minifier.expression_printer import ExpressionPrinter
from expressions import Expression
from module import Module, TypeAlias
from python_minifier.module_printer import ModulePrinter
from python_minifier.rename.mapper import add_parent
from python_minifier.transforms.constant_folding import FoldConstants

from .expressions import Expression
from .folding import FoldableExpression
from .module import Module, TypeAlias
from .patterns import Pattern


@given(node=Expression())
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
Expand All @@ -28,7 +30,7 @@ def test_expression(node):


@given(node=Module())
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100, suppress_health_check=[HealthCheck.too_slow], verbosity=Verbosity.verbose)
def test_module(node):
assert isinstance(node, ast.Module)

Expand Down Expand Up @@ -61,7 +63,7 @@ def test_pattern(node):
compare_ast(module, ast.parse(code, 'test_pattern'))

@given(node=FoldableExpression())
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=100000, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=1), max_examples=1000, suppress_health_check=[HealthCheck.too_slow]) #verbosity=Verbosity.verbose
def test_folding(node):
assert isinstance(node, ast.AST)
note(print_ast(node))
Expand All @@ -74,7 +76,7 @@ def test_folding(node):
constant_folder(node)

@given(node=TypeAlias())
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=1000, verbosity=Verbosity.verbose)
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=100, verbosity=Verbosity.verbose)
def test_type_alias(node):

module = ast.Module(
Expand All @@ -88,7 +90,7 @@ def test_type_alias(node):
compare_ast(module, ast.parse(code, 'test_type_alias'))

@given(node=TypeAlias())
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=1000, verbosity=Verbosity.verbose)
@settings(report_multiple_bugs=False, deadline=timedelta(seconds=2), max_examples=100, verbosity=Verbosity.verbose)
def test_function_type_param(node):

module = ast.Module(
Expand Down
16 changes: 8 additions & 8 deletions src/python_minifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
"""

import python_minifier.ast_compat as ast
import re

import python_minifier.ast_compat as ast

from python_minifier.ast_compare import CompareError, compare_ast
from python_minifier.module_printer import ModulePrinter
from python_minifier.rename import (
rename_literals,
bind_names,
resolve_names,
rename,
add_namespace,
allow_rename_globals,
allow_rename_locals,
add_namespace,
bind_names,
rename,
rename_literals,
resolve_names
)

from python_minifier.transforms.combine_imports import CombineImports
from python_minifier.transforms.constant_folding import FoldConstants
from python_minifier.transforms.remove_annotations import RemoveAnnotations
from python_minifier.transforms.remove_annotations_options import RemoveAnnotationsOptions
from python_minifier.transforms.remove_asserts import RemoveAsserts
from python_minifier.transforms.remove_debug import RemoveDebug
from python_minifier.transforms.remove_explicit_return_none import RemoveExplicitReturnNone
from python_minifier.transforms.remove_exception_brackets import remove_no_arg_exception_call
from python_minifier.transforms.remove_explicit_return_none import RemoveExplicitReturnNone
from python_minifier.transforms.remove_literal_statements import RemoveLiteralStatements
from python_minifier.transforms.remove_object_base import RemoveObject
from python_minifier.transforms.remove_pass import RemovePass
Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ast
from typing import List, Text, AnyStr, Optional, Any, Union

from typing import Any, AnyStr, List, Optional, Text, Union

from .transforms.remove_annotations_options import RemoveAnnotationsOptions as RemoveAnnotationsOptions

Expand Down
2 changes: 1 addition & 1 deletion src/python_minifier/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
except metadata.PackageNotFoundError:
version = '0.0.0'
else:
from pkg_resources import get_distribution, DistributionNotFound
from pkg_resources import DistributionNotFound, get_distribution
try:
version = get_distribution('python_minifier').version
except DistributionNotFound:
Expand Down
6 changes: 3 additions & 3 deletions src/python_minifier/expression_printer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import python_minifier.ast_compat as ast
import sys

from python_minifier.util import is_ast_node
import python_minifier.ast_compat as ast

from python_minifier.token_printer import TokenPrinter, Delimiter
from python_minifier.token_printer import Delimiter, TokenPrinter
from python_minifier.util import is_ast_node


class ExpressionPrinter(object):
Expand Down
6 changes: 3 additions & 3 deletions src/python_minifier/f_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"""

import python_minifier.ast_compat as ast
import copy
import re

import python_minifier.ast_compat as ast

from python_minifier import UnstableMinification
from python_minifier.ast_compare import CompareError
from python_minifier.ast_compare import compare_ast
from python_minifier.ast_compare import CompareError, compare_ast
from python_minifier.expression_printer import ExpressionPrinter
from python_minifier.ministring import MiniString
from python_minifier.token_printer import TokenTypes
Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/module_printer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from .expression_printer import ExpressionPrinter
from .token_printer import Delimiter
from .util import is_ast_node
Expand Down
2 changes: 1 addition & 1 deletion src/python_minifier/rename/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from python_minifier.rename.rename_literals import rename_literals
from python_minifier.rename.renamer import rename
from python_minifier.rename.resolve_names import resolve_names
from python_minifier.rename.util import allow_rename_locals, allow_rename_globals
from python_minifier.rename.util import allow_rename_globals, allow_rename_locals
2 changes: 1 addition & 1 deletion src/python_minifier/rename/bind_names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import python_minifier.ast_compat as ast

from python_minifier.rename.binding import NameBinding
from python_minifier.rename.util import arg_rename_in_place, get_global_namespace, get_nonlocal_namespace, builtins
from python_minifier.rename.util import arg_rename_in_place, builtins, get_global_namespace
from python_minifier.transforms.suite_transformer import NodeVisitor


Expand Down
2 changes: 1 addition & 1 deletion src/python_minifier/rename/resolve_names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import python_minifier.ast_compat as ast

from python_minifier.rename.binding import BuiltinBinding, NameBinding
from python_minifier.rename.util import get_global_namespace, get_nonlocal_namespace, builtins
from python_minifier.rename.util import builtins, get_global_namespace, get_nonlocal_namespace
from python_minifier.util import is_ast_node


Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/rename/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.util import is_ast_node


Expand Down
1 change: 1 addition & 0 deletions src/python_minifier/token_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys


class TokenTypes(object):
NoToken = 0
Identifier = 1
Expand Down
4 changes: 3 additions & 1 deletion src/python_minifier/transforms/constant_folding.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import python_minifier.ast_compat as ast
import math
import sys

import python_minifier.ast_compat as ast

from python_minifier.ast_compare import compare_ast
from python_minifier.expression_printer import ExpressionPrinter
from python_minifier.transforms.suite_transformer import SuiteTransformer
from python_minifier.util import is_ast_node


class FoldConstants(SuiteTransformer):
"""
Fold Constants if it would reduce the size of the source
Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/transforms/remove_annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.transforms.remove_annotations_options import RemoveAnnotationsOptions
from python_minifier.transforms.suite_transformer import SuiteTransformer

Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/transforms/remove_debug.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.transforms.suite_transformer import SuiteTransformer
from python_minifier.util import is_ast_node

Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/transforms/remove_exception_brackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
We can't generally know if a name refers to an exception, so we only do this for builtin exceptions
"""

import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.rename.binding import BuiltinBinding

# These are always exceptions, in every version of python
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.transforms.suite_transformer import SuiteTransformer
from python_minifier.util import is_ast_node

Expand Down
3 changes: 2 additions & 1 deletion src/python_minifier/transforms/remove_object_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python_minifier.ast_compat as ast
import sys

import python_minifier.ast_compat as ast

from python_minifier.transforms.suite_transformer import SuiteTransformer


Expand Down
1 change: 1 addition & 0 deletions src/python_minifier/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import python_minifier.ast_compat as ast


def is_ast_node(node, types):
"""
Is a node one of the specified node types
Expand Down
Loading

0 comments on commit 6e80593

Please sign in to comment.