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
1 change: 1 addition & 0 deletions personal_python_ast_optimizer/parser/machine_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
machine_dependent_attributes: dict[str, str] = {
"os.name": os.name,
"sys.byteorder": sys.byteorder,
"sys.platform": sys.platform,
}

machine_dependent_functions: dict[str, int | None] = {"os.cpu_count": os.cpu_count()}
24 changes: 20 additions & 4 deletions personal_python_ast_optimizer/parser/skipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ def visit_Assign(self, node: ast.Assign) -> ast.AST | None:

def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.AST | None:
"""Skips assign if it is an assignment to a constant that is being folded"""
target_name: str = get_node_name(node.target)
if (
self._should_skip_function_assign(node)
or get_node_name(node.target) in self.tokens_config.variables_to_skip
or target_name in self.tokens_config.variables_to_skip
or self._is_assign_of_folded_constant(node.target)
):
return None

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

parsed_node: ast.AnnAssign = self.generic_visit(node) # type: ignore
Expand Down Expand Up @@ -383,8 +384,10 @@ def visit_Dict(self, node: ast.Dict) -> ast.AST:
for k, v in zip(node.keys, node.values, strict=True)
if getattr(k, "value", "") not in self.tokens_config.dict_keys_to_skip
}
node.keys = list(new_dict.keys())
node.values = list(new_dict.values())

if len(new_dict) < len(node.keys):
node.keys = list(new_dict.keys())
node.values = list(new_dict.values())

return self.generic_visit(node)

Expand Down Expand Up @@ -687,3 +690,16 @@ def visit_Name(self, node: ast.Name) -> ast.Name:
def visit_Attribute(self, node: ast.Attribute) -> ast.AST:
self.names_and_attrs.add(node.attr)
return self.generic_visit(node)

# Nodes that do not need to be fully visited
def visit_alias(self, node: ast.alias) -> ast.alias:
return node

def visit_Pass(self, node: ast.Pass) -> ast.Pass:
return node

def visit_Break(self, node: ast.Break) -> ast.Break:
return node

def visit_Continue(self, node: ast.Continue) -> ast.Continue:
return node
14 changes: 11 additions & 3 deletions tests/parser/test_same_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def get_cpu_count():

_os_name_example: str = "print(os.name)"

_sys_example: str = f"N = '<' if sys.byteorder == '{sys.byteorder}' else '>'"
_sys_example: str = f"""
N = '<' if sys.byteorder == '{sys.byteorder}' else '>'
print(sys.platform == 'a')
"""


@pytest.mark.parametrize(
Expand All @@ -25,8 +28,13 @@ def get_cpu_count():
f"def get_cpu_count():return {os.cpu_count()}",
),
(False, _cpu_count_example, "def get_cpu_count():return os.cpu_count()or 1"),
(True, _sys_example, "N='<'"),
(False, _sys_example, f"N='<'if sys.byteorder=='{sys.byteorder}'else'>'"),
(True, _sys_example, "N='<'\nprint(False)"),
(
False,
_sys_example,
f"N='<'if sys.byteorder=='{sys.byteorder}'else'>'"
"\nprint(sys.platform=='a')",
),
(True, _os_name_example, f"print('{os.name}')"),
(False, _os_name_example, _os_name_example),
],
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.0
5.3.1