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
2 changes: 1 addition & 1 deletion personal_python_ast_optimizer/parser/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TypeHintsToSkip(Enum):
NONE = 0
# ALL might be unsafe, NamedTuple or TypedDict for example
# ALL might be unsafe, NamedTuple for example
ALL = 1
# Should be safe in most cases
ALL_BUT_CLASS_VARS = 2
Expand Down
19 changes: 8 additions & 11 deletions personal_python_ast_optimizer/parser/skipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,18 +422,15 @@ def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.AST | None:

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

if self.token_types_config.skip_type_hints:
if (
not parsed_node.value
and self._node_context == _NodeContext.CLASS
and self.token_types_config.skip_type_hints
== TypeHintsToSkip.ALL_BUT_CLASS_VARS
):
parsed_node.annotation = ast.Name("int")
elif parsed_node.value is None:
if self.token_types_config.skip_type_hints == TypeHintsToSkip.ALL or (
self.token_types_config.skip_type_hints
== TypeHintsToSkip.ALL_BUT_CLASS_VARS
and self._node_context != _NodeContext.CLASS
):
if parsed_node.value is None:
return None
else:
return ast.Assign([parsed_node.target], parsed_node.value)

return ast.Assign([parsed_node.target], parsed_node.value)

return parsed_node

Expand Down
10 changes: 6 additions & 4 deletions tests/parser/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ class Foo():
def test_class_preserves_type_hints():
before_and_after = BeforeAndAfter(
"""
import some_type
class SomeTuple():
'''A tuple, wow!'''
thing1: str
thing1: some_type
thing2: int

def a():
class B:
thing3: None
return B""",
"""class SomeTuple:
\tthing1:int;thing2:int
"""import some_type
class SomeTuple:
\tthing1:some_type;thing2:int
\tdef a():
\t\tclass B:thing3:int
\t\tclass B:thing3:None
\t\treturn B""",
)
run_minifier_and_assert_correct(
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def test_remove_dup_slots_tuple():
run_minifier_and_assert_correct(before_and_after)


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

Expand Down
74 changes: 74 additions & 0 deletions tests/parser/test_type_hints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from personal_python_ast_optimizer.parser.config import (
TokenTypesConfig,
TypeHintsToSkip,
)
from tests.utils import BeforeAndAfter, run_minifier_and_assert_correct


def test_removes_type_hints_all_but_class_var():
before_and_after = BeforeAndAfter(
"""
import some_type

a: some_type

def b():
c: int = 3
print(c)

class C:
a: str
""",
"def b():c=3;print(c)\nclass C:a:str",
)

run_minifier_and_assert_correct(before_and_after)


def test_remove_no_type_hints():
before_and_after = BeforeAndAfter(
"""
import some_type

a: some_type

def b():
c: int = 3
print(c)

class C:
a: str
""",
"""import some_type
a:some_type
def b():c:int=3;print(c)
class C:a:str""",
)

run_minifier_and_assert_correct(
before_and_after,
token_types_config=TokenTypesConfig(skip_type_hints=TypeHintsToSkip.NONE),
)


def test_remove_all_type_hints():
before_and_after = BeforeAndAfter(
"""
import some_type

a: some_type

def b():
c: int = 3
print(c)

class C:
a: str
""",
"def b():c=3;print(c)\nclass C:pass",
)

run_minifier_and_assert_correct(
before_and_after,
token_types_config=TokenTypesConfig(skip_type_hints=TypeHintsToSkip.ALL),
)
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.1
6.1.2