Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions personal_python_ast_optimizer/parser/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
from types import EllipsisType
from typing import Iterable, Iterator

# I tried to import this from ast, it worked on 3.12 but not 3.11?
# TODO: When minimum python becomes 3.12, add "type" before definition
ConstantValue = str | bytes | bool | int | float | complex | None | EllipsisType


class TokensToSkip(dict[str, int]):
__slots__ = ("token_type",)
Expand Down Expand Up @@ -126,15 +122,18 @@ class OptimizationsConfig(_Config):

def __init__(
self,
vars_to_fold: dict[str, ConstantValue] | None = None,
vars_to_fold: dict[
str, str | bytes | bool | int | float | complex | None | EllipsisType
]
| None = None,
enums_to_fold: Iterable[EnumType] | None = None,
fold_constants: bool = True,
remove_unused_imports: bool = True,
assume_this_machine: bool = False,
) -> None:
self.vars_to_fold: dict[str, ConstantValue] = (
{} if vars_to_fold is None else vars_to_fold
)
self.vars_to_fold: dict[
str, str | bytes | bool | int | float | complex | None | EllipsisType
] = {} if vars_to_fold is None else vars_to_fold
self.enums_to_fold: dict[str, dict[str, Enum]] = (
{}
if enums_to_fold is None
Expand All @@ -157,7 +156,6 @@ def _format_enums_to_fold_as_dict(
class SkipConfig(_Config):
__slots__ = (
"module_name",
"warn_unusual_code",
"target_python_version",
"token_types_config",
"tokens_config",
Expand All @@ -168,14 +166,12 @@ def __init__(
self,
module_name: str,
*,
warn_unusual_code: bool = True,
target_python_version: tuple[int, int] | None = None,
tokens_config: TokensConfig = TokensConfig(),
token_types_config: TokenTypesConfig = TokenTypesConfig(),
optimizations_config: OptimizationsConfig = OptimizationsConfig(),
) -> None:
self.module_name: str = module_name
self.warn_unusual_code: bool = warn_unusual_code
self.target_python_version: tuple[int, int] | None = target_python_version
self.tokens_config: TokensConfig = tokens_config
self.token_types_config: TokenTypesConfig = token_types_config
Expand Down
7 changes: 2 additions & 5 deletions personal_python_ast_optimizer/parser/skipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class AstNodeSkipper(ast.NodeTransformer):
"_within_class",
"_within_function",
"module_name",
"warn_unusual_code",
"target_python_version",
"optimizations_config",
"token_types_config",
Expand All @@ -42,7 +41,6 @@ class AstNodeSkipper(ast.NodeTransformer):

def __init__(self, config: SkipConfig) -> None:
self.module_name: str = config.module_name
self.warn_unusual_code: bool = config.warn_unusual_code
self.target_python_version: tuple[int, int] | None = (
config.target_python_version
)
Expand Down Expand Up @@ -271,7 +269,7 @@ def visit_Assign(self, node: ast.Assign) -> ast.AST | None:
and len(node.targets) == 1
and get_node_name(node.targets[0]) == "__slots__"
):
remove_duplicate_slots(node, self.warn_unusual_code)
remove_duplicate_slots(node)

node.targets = new_targets

Expand Down Expand Up @@ -322,7 +320,7 @@ def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.AST | None:
return None

if self._within_class and get_node_name(node.target) == "__slots__":
remove_duplicate_slots(node, self.warn_unusual_code)
remove_duplicate_slots(node)

parsed_node: ast.AnnAssign = self.generic_visit(node) # type: ignore

Expand Down Expand Up @@ -559,7 +557,6 @@ def _is_assign_of_folded_constant(
return (
isinstance(target, ast.Name)
and target.id in self.optimizations_config.vars_to_fold
and isinstance(value, ast.Constant)
)

def _use_version_optimization(self, min_version: tuple[int, int]) -> bool:
Expand Down
7 changes: 1 addition & 6 deletions personal_python_ast_optimizer/parser/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ast
import warnings
from typing import Iterable

from personal_python_ast_optimizer.parser.config import TokensToSkip
Expand Down Expand Up @@ -74,9 +73,7 @@ def skip_decorators(
]


def remove_duplicate_slots(
node: ast.Assign | ast.AnnAssign, warn_duplicates: bool = True
) -> None:
def remove_duplicate_slots(node: ast.Assign | ast.AnnAssign) -> None:
if isinstance(node.value, (ast.Tuple, ast.List, ast.Set)):
found_values: set[str] = set()
unique_objects: list[ast.expr] = []
Expand All @@ -92,8 +89,6 @@ def remove_duplicate_slots(
found_values.add(const_value.value)

if len(node.value.elts) != len(unique_objects):
if warn_duplicates:
warnings.warn(f"Duplicate entries found in __slots__: {found_values}")
node.value.elts = unique_objects


Expand Down
6 changes: 3 additions & 3 deletions tests/parser/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ def test_remove_dup_slots_tuple():
before_and_after = BeforeAndAfter(
"class A:__slots__ = ('a', 'b', 'a')", "class A:__slots__=('a','b')"
)
run_minifier_and_assert_correct(before_and_after, warn_unusual_code=False)
run_minifier_and_assert_correct(before_and_after)


def test_remove_dup_slots__list_annotation():
before_and_after = BeforeAndAfter(
"class A:__slots__: list = ['a', 'b', 'a']", "class A:__slots__=['a','b']"
)
run_minifier_and_assert_correct(before_and_after, warn_unusual_code=False)
run_minifier_and_assert_correct(before_and_after)


def test_remove_dup_slots_set():
before_and_after = BeforeAndAfter(
"class A:__slots__ = {'a', 'b', 'a'}", "class A:__slots__={'a','b'}"
)
run_minifier_and_assert_correct(before_and_after, warn_unusual_code=False)
run_minifier_and_assert_correct(before_and_after)
2 changes: 0 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, before: str, after: str) -> None:

def run_minifier_and_assert_correct(
before_and_after: BeforeAndAfter,
warn_unusual_code: bool = False,
target_python_version: tuple[int, int] | None = None,
token_types_config: TokenTypesConfig = TokenTypesConfig(),
tokens_config: TokensConfig = TokensConfig(),
Expand All @@ -35,7 +34,6 @@ def run_minifier_and_assert_correct(
before_and_after.before,
SkipConfig(
"",
warn_unusual_code=warn_unusual_code,
target_python_version=target_python_version,
tokens_config=tokens_config,
token_types_config=token_types_config,
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.3
5.3.0